A question about design of using virtual or not

Discussion in 'C++' started by Tony Johansson, Aug 16, 2005.

  1. Hello Experts!!

    Here we use multiple inheritance from two classes.We have a class named
    Person at the very top
    and below this class we have a Student class and an Employee class at the
    same level.
    There is a class TeachingAssistent that use multiple inheritance from both
    Student and Employee.
    There is a method named getName is class Person.

    Now to my question which is a question about design.

    Would it be any point to define this method getName as virtual so the
    derived classes could override this method which would result to a possible
    use of polymorfism.

    Here are all the class definitions
    *********************
    #include <string>
    using namespace std;
    class Person
    {
    public:
    Person(string nn = "default") : name(nn) {}
    string getName() const
    {
    return name;
    }
    private:
    string name;
    };

    class Student : public virtual Person
    {
    public:
    Student(string nn="default") : Person(nn) {}
    };

    class Employee : public virtual Person
    {
    public:
    Employee(string nn="default") : Person(nn) {}
    };

    class TeachingAssistent : public Student, public Employee
    {
    public:
    TeachingAssistent(string nn="default") : Person(nn) {}
    };

    Here is main program
    ****************
    #include <vector>
    #include "person.h"
    #include <iostream>
    using namespace std;

    int main()
    {
    vector<Person *> p;
    p.push_back(new Student);
    p.push_back(new Employee);
    p.push_back(new TeachingAssistent);

    for(int i=0; i < p.size(); i++)
    cout << p->getName() << endl;

    return 0;
    }

    Many thanks

    //Tony
    Tony Johansson, Aug 16, 2005
    #1
    1. Advertising

  2. Tony Johansson wrote:
    > Here we use multiple inheritance from two classes.We have a class named
    > Person at the very top
    > and below this class we have a Student class and an Employee class at the
    > same level.
    > There is a class TeachingAssistent that use multiple inheritance from both
    > Student and Employee.
    > There is a method named getName is class Person.
    >
    > Now to my question which is a question about design.
    >
    > Would it be any point to define this method getName as virtual so the
    > derived classes could override this method which would result to a possible
    > use of polymorfism.


    Yes, there might be some point to it. But there is no sense in trying to
    invent a reason to use polymorphism where it's not needed.

    Do not make the mistake of confusing the desire to use some fad with the
    proper design. Polymorphism is good when it is called for. If it is not
    needed (if your model, whatever that is, does not call for it), do not use
    it just for the sake of using it.

    > [...]


    V
    Victor Bazarov, Aug 16, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Ben Pope Guest

    Tony Johansson wrote:
    > Hello Experts!!
    >
    > Here we use multiple inheritance from two classes.We have a class named
    > Person at the very top
    > and below this class we have a Student class and an Employee class at the
    > same level.
    > There is a class TeachingAssistent that use multiple inheritance from both
    > Student and Employee.
    > There is a method named getName is class Person.
    >
    > Now to my question which is a question about design.
    >
    > Would it be any point to define this method getName as virtual so the
    > derived classes could override this method which would result to a possible
    > use of polymorfism.


    The overriding does not allow you to use polymorphism. It allows you to override the GetName function if you need to change the implementation.

    The problem you have is that in TeachingAssistant, you actually have two GetNames... inherited from the two Persons.

    You must be careful of the diamond-shaped inheritance diagram. Consider making Person a Pure ABC.

    Don't use inheritance for code-reuse, use it if you require the flexibility of polymorphism.

    And read the FAQ on Multiple Inheritance:
    http://www.parashift.com/c -faq-lite/multiple-inheritance.html

    Ben
    --
    I'm not just a number. To many, I'm known as a String...
    Ben Pope, Aug 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Simmons
    Replies:
    0
    Views:
    794
    Derek Simmons
    Aug 1, 2004
  2. Jéjé
    Replies:
    2
    Views:
    389
    Jéjé
    Nov 30, 2005
  3. qazmlp
    Replies:
    19
    Views:
    778
    Daniel T.
    Feb 4, 2004
  4. qazmlp
    Replies:
    7
    Views:
    485
    Howard
    Jul 27, 2004
  5. archimed7592
    Replies:
    4
    Views:
    729
    =?UTF-8?B?UGF3ZcWC?=
    May 30, 2007
Loading...

Share This Page