I’m having difficulties trying to get a child function to work:
.h file
class Parent {
int id;
public:
virtual int getid();
}
class Child : public Parent {
int id;
public:
int getid();
}
.cc file
Parent::Parent( int num ) {
id = num;
}
int Parent::getid() {
cout << "Parent!";
return id;
}
Child::Child( int num ) : Parent(num) {
id = num;
}
int Child::getid() {
cout << "Child!";
return id;
}
When I make Child kid = Child(0); and call kid.getid();, I would get Parent! instead of Child!.
What is wrong with my implementation?
Your code has a lot of syntax errors in it. I couldn’t even get it to compile. Here it is fixed with a few notes. This code does in fact output “Child!”