polymorphism different methods

I have a database of Users (Customers and Admins) with different tasks. I want to solve this with polymorphism:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class User
{
   protected:
   string username;
   string password;

   public:
   string getUsername() = 0;
};

class Admin : public User
{
   public:
   void viewEvaluation();
   string getUsername();
};

class Admin : public User
{
   public:
   string getUsername();
};


But if I try polymorphism and tried to call viewEvaluation() than I got problems with the compiler. Any solutions?

rumpfi88
Last edited on
For a pointer of the base class type to be able to access a member function of a derived class, you must define virtual functions in the bass class. (check out http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.1 for an explanation)
So in this case you would want something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class User
{
    protected:
    string username;
    string password;
    public:
    virtual string getUsername();
    virtual void viewEvaluation();
};

class Admin : public User
{
    public:
    void viewEvaluation();
    string getUsername();
};

class Customer : public User
{
    public:
    string getUsername(); 
};


In this example, getUsername() would probably not need to be overridden since (presumably) it would just return the private member 'username' which exists in the base class anyway. Also don't forget to define the virtual methods in the base class. For viewEvaluation() you need to add something like:

1
2
3
4
void User::viewEvaluation()
{
    //maybe include some output that says that this method has been called by a non-admin object
}


In your program then if you have a pointer of type User* that actually points to an Admin object, when you call User->viewEvaluation() it will actually call the method defined in the Admin class. If it points to a Customer object, it will just use the definition from the base User class, since it is not overridden in Customer.
So is it possible to programm it like this (assuming that there is a constructor vor User)?

1
2
3
4
User* admin = new Admin(username, password);
admin->viewEvaluation();

delete admin;


So this really works without problems?
This will work but you'll have to write a constructor for the Admin class, that passes the arguments to the constructor for the User class.
You can do that like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class User
{
    User(std::string p_username, std::string p_password); //i'm assuming the constructor for the User class looks like this
};

class Admin : public User
{
    Admin(std::string p_username, std::string p_password);
};

Admin::Admin(std::string p_username, std::string p_password) : User(p_username, p_password) //this line just passes args from the Admin constructor to the User constructor
{
                                                                                                                                                                            
}
Topic archived. No new replies allowed.