inherent and composition

hello,

i'd like to know is this code consider inherent or not? i read about inherent but i'm not sure about my code

this is the class header for Vehicle and Driver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef VEHICLE_H
#define VEHICLE_H
#include<string>


class Vehicle{
 protected: 
    int id; 
  public:
    Vehicle(int& VehicleId);
    int PrintVehicleId();
    virtual std::string CarName(); 
    virtual void PrintCarInfo();
	virtual void PrintBusInfo();
    virtual ~Vehicle();
};

class Car : public Vehicle{
private:
int id;
std::string name; //Vehicle number, type, capacity, driver assigned
std::string type;
std::string color;
int capacity;
public:
	Car(int &VehicleId, const std::string &CarName, const std::string &CarColor ,const std::string &CarType,int CarCapacity);
	 std::string CarName();
	  void PrintCarInfo();
};

class Bus : public Vehicle{
private:
int id;
std::string name; //Vehicle number, type, capacity, driver assigned
std::string color;
int capacity;
public:
	Bus(int &VehicleId, const std::string &BusName, const std::string &BusColor ,int BusCapacity);
	 std::string BusName();
	  void PrintBusInfo();

};
#endif 



this is for the Driver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef DRIVER_H
#define DRIVER_H
#include<string>

class Driver{
protected:
int id;
public:
	//Vehicle * vehicles;
	Driver(int& DriverId);
	int PrintDriverId();
	virtual std::string DriverName();
	virtual void PrintDriverInfo();
	virtual ~Driver();
};
	
class DriverInfo : public Driver{
std::string name;
std::string vehicle_assign;
int salary;
public:
	DriverInfo(int & DriverId, std::string &DriverName,int& salary, std::string & VehicleAssigned);
	std::string DriverName();
	void PrintDriverInfo();
};
#endif 


for the Driver i will change it to only superclass because i don't need inherent here

anyway if the Vehcile class is inherent so how to compine them togather?

it'll be something like this

1
2
3
4
5
6
7
8
9
10
11
class Vehicle {
   ...
   Driver* driver;
   ...
};
 
class Driver {
   ...
   Vehicle* vehicle;
   ...
};


then what should i include more?

need some help

thank you
Last edited on
That sounds about right. If a Car is a Vehicle, and a Bus is a Vehicle, then you have inheritance. But a Driver is not a Vehicle, so Driver doesn't inherit from Vehicle. However, a Vehicle has a Driver so:
1
2
3
4
class Vehicle {
    Driver driver; //,
    Driver *driver; //, or
    Driver &driver;
any of those would make sense.

I'm still not quite should what your question was... The other things you might put in class Driver could be the driver's name, license, age, etc...
Hi empror9,

Having driverInfo derived from driver is a little strange. When a class is derived from another, think of the relationship between them as a 'is a' relationship. A car 'is a' vehicle, so it makes sense for it to inherit some of the Vehicle class' functionality. Clearly though there is not an 'is a' relationship between driverInfo and driver, driverInfo is not a driver. It's fine to have a class for driverInfo but it doesn't need to inherit from driver just have a driverInfo object in the driver class.

A few other pointers:

1
2
3
4
5
6
7
8
9
10
11
class Vehicle{
 protected: 
    int id; 
  public:
    Vehicle(int& VehicleId);
    int PrintVehicleId();
    virtual std::string CarName(); 
    virtual void PrintCarInfo();
	virtual void PrintBusInfo();
    virtual ~Vehicle();
};


The idea of virtual functions is for derived classes to overload them so that each type of vehicle can have its own behaviour but be treated in the same way, be treated as a generic vehicle. You have base class methods called CarName(), PrintCarInfo() and PrintBusInfo(), and all classes derived from Vehicle inherit these methods. It doesn't make sense for a car to have a method PrintBusInfo(), or likewise for a bus to have methods CarName() and PrintCarInfo(). A virtual function PrintVehicleInfo() makes sense for any kind of vehicle and can be overloaded in bus, car and whatever other vehicles you're planning so that it does what you want it to in each. Each vehicle can have a name field and a getName() method perhaps which returns the name field, and this makes sense for any kind of vehicle so put it in the base class, it probably doesn't need to be virtual either as you probably don't want the functionality of getting the vechicles name to vary in a car or a bus or whatever. Your base class' destructor is virtual, this is correct. One last thing,

Driver(int& DriverId);

there's no real need to pass in basic types by reference, it is however good practice to pass large objects by reference to avoid copying.

I'm not sure I understood your question, but those are my thoughts on your code.
Last edited on
okay i'm sorry if my question not clear enough.

ths first question is about the inherent, from your answers above i understand that the vehicle class is inherent. so i just have to link with the subclasess by the VehicleId. (( this is only to ensure! ))

anyway, when i print the id for vehicles it shows me strange numbers. it should start form 1 and increase by one in each time

here is the code in the main programe.

1
2
vector<Vehicle*> vehicles;
   int VehicleId = 1;


for the driver class, the id works fine.

the second problem, i want to assign one driver to specific vehicle, and one vehicle to specific driver, so i need composition. could you show me only simple code for this according to my code above? i need to understand how to do.

also there is another problem, i want to print "there is no vehicle in the list" in case the vehicle class is empty.

i use this code

if(vehicles[i]->PrintVehicleId() == 0) cout<<"there is no vehicles in the list"<<endl;
but it shows me errors

thank you and i appreciate your help
Last edited on
any idea guys?

i understand that the vehicle class is inherent. so i just have to link with the subclasess by the VehicleId
¿link? ¿what link?
when i print the id for vehicles it shows me strange numbers. it should start form 1 and increase by one in each time
You need an static member for that. At least show the code that makes that assignment.

empror9 wrote:
i want to assign one driver to specific vehicle, and one vehicle to specific driver,
Mathhead200 wrote:
However, a Vehicle has a Driver
1
2
class Vehicle {
    Driver driver;
Be aware of what you are doing there. The lifetime of the driver is attached to the lifetime of the vehicle. Maybe it will be better to say that a driver owns a vehicle.
1
2
3
class Driver{
  Vehicle vehicle; //This should be a "pointer" (for polymorphism), but I want to make a point about the lifetime
};
However if you need links in both sides, then they probably shouldn't be separated classes.

i want to print "there is no vehicle in the list" in case the vehicle class is empty.
¿list? ¿empty?
¿a vehicle has a list of vehicles?
If you have errors, tell us what they are.
hi,

where exactly to put the static? i put static in the vehicle header

1
2
protected:
static int id;


but this is error in compiler, can you show me where exactly?


for the composition, in the assignment i must use the inherent for this. but my problem is what to do after this code?

1
2
class Vehicle {
    Driver driver;


can you show me example in my code to understand?



for this code, if(vehicles[i]->PrintVehicleId() == 0) cout<<"there is no vehicles in the list"<<endl;

sorry there is no compile error but this dosen't work brcuase the id for vehicles is not correct


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//vehicle.h (header guarded)
class Vehicle{
private: //note that they are private, the derived classes don't need to access them
	static int count;
	const int id;
public:
	//int PrintVehicleId(); //this getter is not needed
	Vehicle();
};

//vehicle.cpp
#include "vehicle.h"
int Vehicle::count=1;
Vehicle::Vehicle():id(count++){}
That will give you an increasing id number, for every object Vehicle (or from derived class)
The id will start in 1, so I don't understand what do you want to do here. if(vehicles[i]->PrintVehicleId() == 0) cout<<"there is no vehicles in the list"<<endl;

for the composition, in the assignment i must use the inherent for this. but my problem is what to do after this code?
¿what do you want to do? ¿how do you want to link the objects?
By instance, a driver may have a lot of vehicles, a vehicle could not have any drivers ...
Those are different relationships, and they are coded different.
Topic archived. No new replies allowed.