Where to put attribute when using Inheritance

T

Tony Johansson

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
 
V

Victor Bazarov

Tony said:
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?
 
W

wittempj

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;
-}
 
C

codigo

Tony Johansson said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top