0

masters of C++.

I am trying to implement polymorphism in C++. I want to write a base class with a virtual function and then redefine that function in the child class. then demonstrate dynamic binding in my driver program. But I just couldn't get it to work.

I know how to do it in C#, so I figured that I might have made some syntactical mistakes where I had used C#'s syntax in my C++ code, but these mistakes are not obvious to me at all. So I'd greatly appreciate it if you would correct my mistakes.

#ifndef POLYTEST_H #define POLYTEST_H class polyTest { public: polyTest(); virtual void type(); virtual ~polyTest(); }; #endif #include "polyTest.h" #include <iostream> using namespace std; void polyTest::type() { cout << "first gen"; } #ifndef POLYCHILD_H #define POLYCHILD_H #include "polyTest.h" using namespace std; class polyChild: public polyTest { public: void type(); }; #endif #include "polyChild.h" #include <iostream> void polyChild::type() { cout << "second gen"; } #include <iostream> #include "polyChild.h" #include "polyTest.h" int main() { polyTest * ptr1; polyTest * ptr2; ptr1 = new polyTest(); ptr2 = new polyChild(); ptr1 -> type(); ptr2 -> type(); return 0; } 

I realized that I didn't implement the constructor or the destructor, because this is just a test class, they don't have to do anything, and that the compiler would provide with a default constructor/destructor. Would that be why I'm getting compilation errors? And why would that be the case?

7
  • Ptr1 waits for a polyChild, where you give him a polyTest. You can say it's a parent type but give him the child one. The reverse is not possible. Commented Nov 3, 2013 at 21:52
  • 2
    Please provide real code. That code should not compile by various reasons. Starting with you have declared the polyTest default ctor but you have not defined it. Commented Nov 3, 2013 at 21:53
  • 3
    You haven't described the error you encountered. "I just couldn't get it to work," isn't good enough. Commented Nov 3, 2013 at 21:57
  • @Manu343726, you are right, my code indeed didn't compile, could you point out why that is? I'll post all my code right now Commented Nov 3, 2013 at 22:00
  • The code shown contains a memory leak! C++ does not clean-up after you! Use, e.g., std::unique_ptr<polyTest> ptr1(new PolyTest()); if you want automated clean-up. Commented Nov 3, 2013 at 22:06

2 Answers 2

6

Your pointers should be to the base type:

polyTest * ptr1; polyTest * ptr2; 

polyChild is-a polyTest, but polyTest is not a polyChild.

Sign up to request clarification or add additional context in comments.

3 Comments

wow, just realize I made that dumb of a mistake. thank you, do you why my code still wouldn't compile?
@user2950788 You will have to give more details. There are other errors, pointed out in other comments.
sorry I forgot to update that part in my post. I did fix the pointers, but it still wouldn't compile. do you know why that might be?
1

juanchopanza gave you the right answer about what you asked: you can assign a pointer of the child type into a pointer of the parent type, because the child class is also of the type of the parent class.

A derived object is a base class object (as its a sub class), so can be pointed to by a base class pointer. however a base class object is not a derived class object so can't be assigned to a derived class pointer.

I will use an analogy: a dog is an animal, a cat is an animal. You cannot say that an animal is always a cat or dog.

In C++, run-time type checking is implemented through dynamic_cast, this allows you to check if the parent class if of a certain type of children class (in my analogy this allows you to check if an Animal is a Cat). Compile-time downcasting is implemented by static_cast, but this operation performs no type check. If it is used improperly, it could produce undefined behavior.

In general abuse of downcasting shows bad design of interfaces unless you are implementing things like a double dispatch pattern, you should not have to resort to downcasts all the time in your program.

On another note, please capitalize your classes, this is fairly standard across most if not all C++ conventions. I fixed all the other stuff in your program and it should compile.

#include <iostream> using namespace std; class PolyTest { public: virtual ~PolyTest() {} // your example had no body virtual void type(); }; void PolyTest::type() { cout << "first gen"; } class PolyChild: public PolyTest { public: void PolyChild::type(); }; void PolyChild::type() { cout << "second gen"; } int main() { PolyTest* ptr1 = new PolyTest(); PolyTest* ptr2 = new PolyChild(); ptr1->type(); ptr2->type(); cout << std::endl; PolyChild* pChild1 = dynamic_cast<PolyChild*>(ptr1); if (pChild1) cout << "ptr1 is a PolyChild" << std::endl; else cout << "ptr1 is NOT a PolyChild" << std::endl; PolyChild* pChild2 = dynamic_cast<PolyChild*>(ptr2); if (pChild2) cout << "ptr2 is a PolyChild" << std::endl; else cout << "ptr2 is NOT a PolyChild" << std::endl; cin.ignore(1); return 0; } 

I also added a bit of code to demonstrate dynamic_cast<>. Again, you should not abuse it as abusing it, means that you did not design your interfaces properly.

4 Comments

@user2950788 seriously? "you have declared the polyTest default ctor but you have not defined it.", that is straight out of one of the first comments you got.
@juanchopanza I think he's new to SO and C++
@BlueTrin, Thank you so much for your explanation!! and you are right about my first time using SO. Could you explain to me why my codes would not compile until I implemented my constructors and destructors? Based on my understanding, if I don't implement constructors and destructors, the compiler would provide me with default constructors/destructors that do absolutely nothing. In this case, I didn't need the constr/destru to do anything. so I didn't even bother writing them out, but why would that give me an compilation error?
@user2950788 the problem is that you declared a default constructor. Once you do that, you have to define it too, which you didn't do. That was the source of that particular error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.