polymorphism

why compiler keeps telling me "no addFunction member in Acccount class"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Account 
{
  public:
     virtual std::string getUsername() = 0; 
};

class Register : public Account
{
public:
  void addAccount();
  std::string getUsername();
}

//definition
int main() {
    Account *ptr = new Register;
    ptr->addAccount();
}


so abstract base class only works for overwridden function?

Last edited on
I don't think you meant:
"no addFunction member in Acccount class"


I think you meant:
"no addAccount member in Acccount class"


And it's a fairly understandable error. It looks like you need to adda virtual addAccount into your account class. polymorphism doesn't work in the way you think by the looks of it.
Last edited on
I think you meant:

my bad.


 It looks like you need to adda virtual addAccount into your account class

i have another derive class from Account class that dont need addAccount function.


thanks for the answer.
I'm not entirely sure what you're trying to do with this program but it looks like you're getting the is a/has a relationship messed up. It sounds to me like register isn't an account, but should hold several accounts, in which case inheritance isn't the right thing. Also, if you're inheriting several things from a base class which has functionality that not all of them need, then your hierarchy is flawed.
Topic archived. No new replies allowed.