473,324 Members | 2,548 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,324 software developers and data experts.

Quick inheritance question

Hey, just a quick question about inheritance.

Heres some basic code to try explain...

class base
{
public:
object1 anObj;
void setName(void);
};

void base::setName(void) { anObj.giveName("name"); }

class derived : base
{
public:
object2 anObj;
};

Ok so object1 and object2 both have the giveName(string) method. The global object anObj changes type, but the giveName() method is still valid. So i guess im trying to ask if its possible for object2 anObj to override object1 anObj so that derived::setName() will set the name of object2 anObj?
Apr 14 '07 #1
6 1222
Ganon11
3,652 Expert 2GB
Yes. In your derived class, add the method setName(void), which can call base::setName(), and then call anObj::giveName("whatever"). You should also declare setName as virtual in base and derived, so that if you ever pass a derived to a function that takes a base, it will call the proper setName function.
Apr 15 '07 #2
Thanks for the speedy reply.

If I add the setName method to derived, won't that kind of defeat the point of having a derived class?

I want to use the setName function in base to set the name of the object2 in derived. Adding the same method to derived will just be duplicating the code, which is what i'm trying to avoid.
Apr 15 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This is not a yes or no question.

True, adding a setName() method to your derived class would hide the setName() method in the base class but there are times when you want this behavior.

Maybe the base class is a Door with an Open() method and things go along OK until the boss wants a Door that can be opened only with a magic spell. So you derived EnchantedDoor friom Door and implement the Open() in EnchantedDoor to reauire a magic spell:

class Door
{
public:
Open();
};

class EnchantedDoor :public Door
{
public:
Open(char* thespell);
};

In your application:

Door d;
d.Open(); //opens the door
EnchantedDoor d1;
d1.Open("Sesame"); //uses magic spell to open the door


This type of situation is common when writing factory classes.
Apr 15 '07 #4
I don't think I'm explaining my problem properly. Sorry Ill try again with more relevant names.

Say I'm making an MP3 player simulator. Ill have an Track object and a Player object. I also have Mp3Player which is derived from Player and and Mp3Track which is derived from Track...

class Track
{
public:
string name, artist;

string getName(void) { return name; }
sting getArtist(void) { return artist; }
};

class Mp3 : public Track
{
public:
string album;

string getAlbum(void) { return album; }
};

class Player
{
public:
vector *playList;
Track *currentTrack;

void setCurrentTrack(int num) { currentTrack = playList.at(num); }
void showTrackName(int trackNum)
{
setCurrentTrack(trackNum);
cout << currentTrack->getName();
}
};

class Mp3Player : Public Player
{
Mp3Track *currentTrack;

void showTrackAlbum(trackNum)
{
setCurrentTrack(trackNum);
cout << currentTrack->getAlbum();
};

So when an Mp3Player is created, it overrides currentTrack so it becomes a Mp3Track, so when Player::setCurrentTrack() sets the track, it is setting an Mp3Track. Also if the showTrackName() method were to be called, it would be getting the name of an Mp3Track. Ive tried implementing this and instead of showTrackName() returning the name of an Mp3Track, it tries to return the name of a Track, which isn't there.

Am i trying something that cant be done, or am i just goin about it the complete wrong way?
Apr 15 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
This statement is not correct:

So when an Mp3Player is created, it overrides currentTrack so it becomes a Mp3Track,

When an Mp3Player is created is has a member variable:

Mp3Track *currentTrack;

which is used by the Mp3Player::setCurrentTrack(). Except that there is no such method. All there is is this: void Player::setCurrentTrack() and this method uses the:

Track *currentTrack;

of the Player class.

Remember a compund object contains an instance of each of its base classes and data members do not override other data members. Only member functions can override member fuinctions.

It's starting to look like you want to implement the "Mp3Player" IS A KIND OF "Player". If that's the case, you need to use polymorphism:

class Player
{
Track* currentTrack;
public:
virtual void setCurrentTrack(int num);
};

class Mp3Player : public Player
{
public:
virtual void setCurrentTrack(int num);

}


void AFunction(Player* p)
{
p->setCurrentTrack(25);
}

Now you create an Mp3Player object and use it as a Player object:

Mp3Player* obj = new Mp3Player;

AFunction(obj); //inside it calls Mp3Player::setCurrentTrack

Player obj1;
AFunction(obj1); //inside it calls Player::setCurrentTrack


Is this what you have in mind?? If so, read up on virtual functions.
Apr 15 '07 #6
Yeah, thats what I'm trying to do. Thanks for pointing me in the right direction. Much appreciated.
Apr 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
5
by: Hypnotik | last post by:
Hello everyone. I'm working on multiple inheritance program. I have a pretty specific problem that I'm not sure how to overcome. I'm going to try to explain this as simple as possible. There is a...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.