Why's this happening?
Hello
I was programming something when I noticed that I can do this:
1 2 3 4 5 6 7 8 9 10
|
.
.
int main()
{
blaseClass *baseObjPtr = nullptr;
childClass childObject(12, 'a');
baseObjPtr = &childObject;
.
.
}
|
where baseClass is:
1 2 3 4 5 6 7 8 9 10
|
class baseClass{
private:
int base_value;
public:
void putValue(int VAL)
{
base_value = VAL;
std::cout << "base_value = " << base_value << std::endl;
}
};
|
and childClass is:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class childClass : public baseClass{
private:
int child_value;
char child_char;
public:
childClass(int VAL, char CH)
{
child_value = VAL;
child_char = CH;
std::cout << "child_value = " << child_value;
std::cout << ", child_char = " << child_char << std::endl;
}
};
|
Why's this happening? Why am I able to use a pointer of some type with an object of another type?
Thanks
Look up polymorphism. It's one of the foundations of Object-Oriented Programming.
Ok, I'll look it up. Thanks.
EDIT:
Though I've done OOP, neither my course materials nor my teacher ever mentioned anything related to polymorphism.
Last edited on
Topic archived. No new replies allowed.