473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

A question about design of using virtual or not

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[i]->getName() << endl;

return 0;
}

Many thanks

//Tony

Aug 16 '05 #1
2 1552
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
Aug 16 '05 #2
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-lit...heritance.html

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
7
by: Steve Long | last post by:
Hello, I have a design question that I'm hoping someone can chime in on. (I still using VS 2003 .NET 1.1 as our company has not upgraded XP to sp2 yet. duh I know, I know) I have a class I wrote...
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.