473,383 Members | 1,997 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,383 software developers and data experts.

Where to put attribute when using Inheritance

Hello!!

Assume we have one base class called Vehicle and two derived classes called
Car and Bus.
I would be able to call method getName on an object of class Car or Bus and
return back the name that is set for this class. Asking getName on an object
of class Vehicle is of no interest. Assume we have an attribute called name
of type string.

Now to my question do you think it's right to put the name attribute in each
of class Car and Bus. I think so.
I think putting the name attribute in class Vehicle is wrong because the
name is different in class Car and Bus.

The name attribute is set in the c-tor for class Car and Bus.
This getName is made pure virtual so polymorfism can be used to call the
right getName depending of the object type

I'm I right in my thoughts.

Many thanks
//Tony


Jul 23 '05 #1
3 1951
Tony Johansson wrote:
Assume we have one base class called Vehicle and two derived classes called
Car and Bus.
Why are you starting another thread instead of continuing in your previous
"Weapon / Winchester / .." one? You're asking same questions. Did you
read the replies to your first post?
[...]

Jul 23 '05 #2
In my opinion you want three things:
- Have Vehicle objects with a private member 'name'
- Have derived objects from Vehicle - which have a name
- No objects of Vehicle can be created

I would solve this by making the constructor of vehicle not accesible
to the outside world and have the name attribute at the highest
possible level, see a quick example below. If you try to add a line
like 'Vehicle v;' to the main() you get a compilation error.

-#include <iostream>
-#include <string>
-
-using namespace std;
-
-class Vehicle
-{
- string m_name;
-protected:
- Vehicle() {};
-public:
- virtual ~Vehicle() {};
- const string& name() {return m_name;}
- void name(const string& n) { m_name = n; }
-};
-
-class Car:public Vehicle
-{
-public:
- Car(const string& n):Vehicle(){ name(n); }
- ~Car() {}
-};
-
-int main()
-{
- Car c("Fiat");
- cout << c.name() << endl;
-
- return 0;
-}

Jul 23 '05 #3

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:lJ*******************@newsb.telia.net...
Hello!!

Assume we have one base class called Vehicle and two derived classes called Car and Bus.
I would be able to call method getName on an object of class Car or Bus and return back the name that is set for this class. Asking getName on an object of class Vehicle is of no interest. Assume we have an attribute called name of type string.

Now to my question do you think it's right to put the name attribute in each of class Car and Bus. I think so.
I think putting the name attribute in class Vehicle is wrong because the
name is different in class Car and Bus.
Nothing prevents you to support a common attribute and a specialized
attribute.

The name attribute is set in the c-tor for class Car and Bus.
This getName is made pure virtual so polymorfism can be used to call the
right getName depending of the object type

I'm I right in my thoughts.

Many thanks
//Tony


Yes, your choice to place the name attribute in each derived class is an
option. Placing a common attribute in the abstract class is another option.
Same goes for an Engine class. Its a different Engine but its still an
Engine.

Since name isn't a clear attribute, lets consider a common license number
attribute for all vehicles. Let Cars have a car-type attribute and Trucks a
truck-classification attribute. I can implement the pure-virtual display()
member function, overload display() in both Car and truck and still call the
abstract display() through an access specifier.

// CarTest.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;

class Vehicle
{
std::string m_license;
protected:
Vehicle(std::string s) : m_license(s) { }
public:
virtual ~Vehicle() { }
virtual void display() const = 0
{
cout << "vehicle license: " << m_license;
cout << "\t";
}
};

class Car : public Vehicle
{
std::string m_type;
public:
Car(std::string s, std::string t) : Vehicle(s), m_type(t) { }
~Car() { }
void display() const
{
Vehicle::display();
cout << "car type: " << m_type;
cout << endl;
}
};

class Truck : public Vehicle
{
std::string m_classification;
public:
Truck(std::string s, std::string t) : Vehicle(s), m_classification(t)
{ }
~Truck() { }
void display() const
{
Vehicle::display();
cout << "truck classification: " << m_classification;
cout << endl;
}
};

int main()
{
std::vector<Vehicle*> v_vehicles;

v_vehicles.push_back(new Car("M1111", "sportscar"));
v_vehicles.push_back(new Truck("G2222", "2 Tons"));
v_vehicles.push_back(new Car("L3333", "coupe"));
v_vehicles.push_back(new Truck("D4444", "4 Tons"));

typedef std::vector<Vehicle*>::iterator ITER;
ITER it = v_vehicles.begin();
for ( it = v_vehicles.begin(); it != v_vehicles.end(); ++it)
{
(*it)->display();
}

for ( it = v_vehicles.begin(); it != v_vehicles.end(); ++it)
{
delete *it;
}

return 0;
}

Jul 23 '05 #4

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

Similar topics

4
by: Maarten van Reeuwijk | last post by:
Hello, Maybe I was a little too detailed in my previous post . I can boil down my problem to this: say I have a class A that I encapsulate with a class Proxy. Now I just want to override and add...
1
by: Skip Montanaro | last post by:
I just stumbled upon a bug in some group-written code. We have this sort of class hierarchy: class X(object): ... class A(X): def __init__(...): self.attr = 0.0
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
3
by: TruongLapVi | last post by:
Hi everybody, I have write customize attribute TestAttribute public class TestAttribute : Attribute { private string name; public TestAttribute(string name) {
2
by: belgie | last post by:
Whereas in Asp I could submit my form contents to any other Asp page, it appears that in Asp.NET the primary Asp form will only submit to itself. I have tried changing the Action attribute in the...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
5
by: crystalattice | last post by:
I've finally figured out the basics of OOP; I've created a basic character creation class for my game and it works reasonably well. Now that I'm trying to build a subclass that has methods to...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
5
by: Rafe | last post by:
Hi, I've been thinking in circles about these aspects of Pythonic design and I'm curious what everyone else is doing and thinks. There are 3 issues here: 1) 'Declaring' attributes - I...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.