| Hovo Menejyan (6) | |
|
Hi, this is the problem i am having with my assignment(the code that i am going to show here is not my assignment,i just wrote this code because i am experiancing simmular problem in my assignment) //person.h #include <string> #include <iostream> using namespace std; class Person { private: string name; public: Person(string nam){name=nam;} void setName(string nam){name=nam;} string getName(){return name;} }; class Account:public Person { private: int money; public: Account(int mon,string nam):Person(nam) {money=mon;} int getMoney(){return money;} }; //test.cpp #include "training.h" int main() { Person *p; p=new Account(500,"Hovo"); p->getMoney(); //Here p does not have access to getMoney function,i knowi can call static cast //and it works but i need to live the main just like this. return 0; } so my question is what I am missing, why person instace which was declared to point to account does not have access to account functions, how to fix it ? thank you. | |
|
|
|
| vlad from moscow (3662) | |
| p has type of pointer to Person. Class Person has no function getMoney. | |
|
|
|
| Hovo Menejyan (6) | |
| so what can i change (not from main.cpp) to fix this problm ? | |
|
|
|
| vlad from moscow (3662) | |
| To declare this function as virtual in the base class. | |
|
|
|