if i try to access functions of base class like getType,getModel() etc i get no data intiliazed in them i think their is something else wrong, you can look into last Class bus display option from this i call base function objects which are inherited in it. so these two classes are important base and bus. if anyone can help me out and guide what im doing wrong, i would be really grateful. Edit: Picture has been attached, that how inheritance should work in this program
#include<iostream>
using namespace std;
class Vehicle{
private:
string Type,Make,Model,Color;
int year;
float MilesDriven;
public:
Vehicle(){}
Vehicle(string Type,string Make,string Model,string Color)
{
this->Type=Type;
this->Make=Make;
this->Model=Model;
this->Color=Color;
}
string getType(){
return Type;
}
string getModel(){
return Model;
}
string getMake(){
return Make;
};
string getColor(){
return Color;
}
int getYear(void){
return year;
}
};
class GasVehicle : virtual public Vehicle
{
private:
double FuelTankSize;
public:
GasVehilce(){}
GasVehicle(double FuelTankSize,string Type,string Make,string Model,string Color) : Vehicle(Type,Make,Model,Color){
this->FuelTankSize=FuelTankSize;
}
double getFuelTankSize(){
return FuelTankSize;
}
};
class ElectricVehicle : virtual public Vehicle{
private:
string EnergyStorage;
public:
ElectricVehicle(string EnergyStorage,string Type,string Make,string Model,string Color) : Vehicle(Type,Make,Model,Color){
this->EnergyStorage=EnergyStorage;
}
string getEnergy(void){
return EnergyStorage;
}
};
class HighPerformance : public GasVehicle
{
private:
int horsesPower;
int TopSpeed;
public:
HighPerformance(int horsesPower,int Topspeed,int FuelTankSize,string Type,string Make,string Model,string Color) : GasVehicle(FuelTankSize,Type,Make,Model,Color){
this->horsesPower=horsesPower;
this->TopSpeed=Topspeed;
}
};
////
class SportsCar : HighPerformance {
private:
string GearBox;
string DriveSystem;
string RunWheel;
public:
// SportsCar(){}
SportsCar(string GearBox,string DriveSystem,string RunWheel,int horsesPower,int Topspeed,int FuelTankSize,string Type,string Make,string Model,string Color) : HighPerformance(horsesPower,Topspeed,FuelTankSize,Type,Make,Model,Color){
this->GearBox=GearBox;
this->DriveSystem=DriveSystem;
this->RunWheel=RunWheel;
}
};
class HeavyVehicle : public GasVehicle , public ElectricVehicle
{
private:
double MaxiWeight;
int NumberOfWheels;
int lenght;
public:
HeavyVehicle(int MaxiWeight,int NumberOfWheels,int lenght,double FuelTankSize,string EnergyStorage,string Type,string Make,string Model,string Color) : GasVehicle(FuelTankSize,Type,Make,Model,Color),ElectricVehicle(EnergyStorage,Type,Make,Model,Color){
this->MaxiWeight=MaxiWeight;
this->NumberOfWheels=NumberOfWheels;
this->lenght=lenght;
}
double getMaxiWeight(){
return MaxiWeight;
}
int getNumberOfWheels(){
return NumberOfWheels;
}
int getLenght(){
return lenght;
}
};//
//
//class ConstractionTrucks : public HeavyVehicle
//{
// private:
// string cargo,cemet,gravel,sand;
// public:
// ConstractionTrucks(string cargo,string cemet,string gravel,string sand,int MaxiWeight,int NumberOfWheels,int lenght,double FuelTankSize,string EnergyStorage,string Type,string Make,string Model,string Color) : HeavyVehicle(MaxiWeight,NumberOfWheels,lenght,FuelTankSize,EnergyStorage,Type,Make,Model,Color)
// {
// this->cargo=cargo;
// this->cemet=cemet;
// this->gravel=gravel;
// this->sand=sand;
// }
//};
class Bus : public HeavyVehicle {
private:
int NumberOfseats;
public:
Bus(int NumberOfSeats,int MaxiWeight,int NumberOfWheels,string EnergyStorage,int lenght,double FuelTankSize,string Type,string Make,string Model,string Color) : HeavyVehicle(MaxiWeight,NumberOfWheels,lenght,FuelTankSize,EnergyStorage,Type,Make,Model,Color){
this->NumberOfseats=NumberOfSeats;
}
void SetNumberOfseats(int seats){
this->NumberOfseats =seats;
}
int getNumberOfseats(void){
return NumberOfseats;
}
void Display(void){
cout<<"NumbeR Of Seats: "<<getNumberOfseats()<<endl;
cout<<"MaxiWeight: "<<getMaxiWeight()<<endl;
cout<<"Number Of wheels: "<<getNumberOfWheels()<<endl;
cout<<"Lenght: "<<getLenght()<<endl;
cout<<"FuelTankSize: "<<getFuelTankSize()<<endl;
cout<<"Type: "<<getType()<<endl;
cout<<"Model: "<<getModel()<<endl;
}
};
int main(){
Bus Obj(5,1200.0,8,"Storage",102,304.0,"Metalic","MXS","2001-Suzuzki","White");
Obj.Display();
}
CodePudding user response:
The most derived class must initialize a virtual base. Your issue can be illustrated with this:
#include <iostream>
struct virtbase {
int x;
virtbase(int x) : x(x) {}
virtbase() : x(42) {}
};
struct middle : virtual virtbase {
middle(int x) : virtbase(x) {}
};
struct derived : middle {
derived(int x) : middle(x) {}
};
int main() {
derived d(1);
std::cout << d.x;
}
42
I suggest you to try what happens when you remove the default constructor of virtbase
. Then there will be an error for missing default constructor of virtbase
in derived(int x) : middle(x) {}
. That is becasue the most derived class must initialize the virtual base:
#include <iostream>
struct virtbase {
int x;
virtbase(int x) : x(x) {}
virtbase() : x(42) {}
};
struct middle : virtual virtbase {
middle(int x) : virtbase(x) {}
middle() {}
};
struct derived : middle {
derived(int x) : virtbase(x) {}
};
int main() {
derived d(1);
std::cout << d.x;
}
CodePudding user response:
Your first try was good. Here is an easy solution. You should remove the virtual
inheritance from GasVehicle
and ElectricVehicle
. Now you will get a ambiguous references for getModel()
and getType()
. The reason is that GasVehicle
and ElectricVehicle
both derive from Vehicle
. Bus
now derives from GasVehicle
and ElectricVehicle
-- it cannot decide which method should be called. Now it is up to you to tell Bus
which method it should use. You can do this by resolving the scope.
The Display
method will look like this:
void Display(void)
{
cout << "NumbeR Of Seats: " << getNumberOfseats() << endl;
cout << "MaxiWeight: " << getMaxiWeight() << endl;
cout << "Number Of wheels: " << getNumberOfWheels() << endl;
cout << "Lenght: " << getLenght() << endl;
cout << "FuelTankSize: " << getFuelTankSize() << endl;
cout << "Type: " << GasVehicle::getType() << endl; //resolve scope here
cout << "Model: " << GasVehicle::getModel() << endl; //resolve scope here
}