0
class A {
public:
    void show() {
        cout << "Class A Show"<<'\n';
    }
};

class C : public A {
public:
    void show() {
        cout << "Class C Show" << '\n';
    }
};

int main()  //first int main()
{
    C* a = new C;  //line 1
    a->show();     //line 2
    a = new A(); //line 3
    a->show();   //line 4
}

int main()  //2nd int main()
{
    A* a = new C;  //line 1
    a->show();     //line 2
    a = new A(); //line 3
    a->show();   //line 4
}

Question :

Output of each line in both int main () when rum separately

Error A value of Type "A*" cannot be assigned to a value of Type "C*"

I came across a interview question where the panel asked the outputs of the mentioned 2 int mains() and asked if it produces an error than how can we omit that error. I tried this code on compiler and the error came on line 3 of First int main() while 2nd int main() ran without an error

  • 2
    What is your question? – NathanOliver Sep 13 '19 at 18:43
  • In the first main type of `a` is `C*` since you cannot store there `A*`, because `C` is derived from `A`, not vice versa – fas Sep 13 '19 at 18:47
  • O how can i remove this error – Shiva Chandel Sep 13 '19 at 18:49
  • 1
    If the question is *how to remove the error* the answer is *fix the code* – SergeyA Sep 13 '19 at 18:49
  • @ShivaChandel You can't remove this error because what that `main` is trying to do is conceptually flawed. – François Andrieux Sep 13 '19 at 18:49
  • Note that there is no polymorphism here because `show` is not `virtual`. – François Andrieux Sep 13 '19 at 18:51
  • @FrançoisAndrieux It reminds me of an old doctor joke, when patient comes to doctors office and asks the good doctor: *Doctor, it pains me when I bend like that (and does really bizzare and obviously painful twist), what's your advice on how to avoid such a pain?* And the good doctor replies: *"Just don't bend like that, my dear!"* – SergeyA Sep 13 '19 at 18:51
  • I think he’s asking for help in deciphering what errors go wrong and why, and how we should prevent it. – Enthus3d Sep 13 '19 at 18:52
  • 1
    We know that every herring is a fish, but not every fish is a herring. You have two classes, class Fish and class Herring. (1) What would be a natural relationship between them? (a) Fish inherits Herring (b) Herring inherits Fish (c) both a and b (d) neither a nor b? Why? (2) Given the answer to (1) and these two statements `Fish* fish = new Herring; Herring* herring = new Fish;` which is correct? (a) first (b) second (c) both (d) neither? Why? – n. m. could be an AI Sep 13 '19 at 18:59
  • @n.m. a class RedHerring would have templated type cast operator to any type :P – Swift - Friday Pie Sep 13 '19 at 19:10

1 Answers1

1

C++ is a strictly typed language, so you cannot assign value of one type to value of another. Pointers to different types are different types. There is concept of compatible types which can be converted implicitly, but pointers to different types aren't one of them.

Now assigning pointer to derived class to pointer of base class is allowed. From abstact point of view, an object if class-child of type A is an object of class-type A. Such assignment is part of the type erasure paradigm. But opposite is not possible, because type A is not type C.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42