derived class constructor
Mar 21, 2014 at 3:28am UTC
I need some help with my constructor in my gaspowered constructor.
GasPowered(double e, double f):Car(mak,mod)
for some reason the mak,mod is red saying it is not declared. can some show me how to fix it? thank you
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
#include <iostream>
#include <string>
using namespace std;
//class Car
class Car{
protected :
string make;
string model;
public :
//constructor for car
Car(string mak, string mod){
make = mak;
model = mod;}
string getMake(){return make;}
string getModel(){return model;}
virtual void print_description(){cout <<"Car maker: " << getMake() << ", Model:" << getModel() << endl;}
};
class GasPowered: public Car{
private :
double engine_size;
double fuel_tank_size;
public :
GasPowered(double e, double f):Car(mak,mod){engine_size = e; fuel_tank_size = f;}
virtual void print_description (){cout <<"Car maker: " << getMake() << ", Model:" << getModel() << endl;}
};
Last edited on Mar 21, 2014 at 3:30am UTC
Mar 21, 2014 at 3:44am UTC
1 2 3 4 5 6 7 8 9 10 11 12
class GasPowered: public Car{
private :
double engine_size;
double fuel_tank_size;
public :
//GasPowered(double e, double f):Car(mak,mod){engine_size = e; fuel_tank_size = f;}
GasPowered( string mak, string mod, double e, double f ) : Car(mak,mod)
{engine_size = e; fuel_tank_size = f;}
virtual void print_description () const // *** const
{cout <<"Car maker: " << getMake() << ", Model:" << getModel() << endl;}
};
And make
virtual void print_description()
in the base class
virtual void print_description() const
1 2 3
virtual void print_description() const { /* ... */ }
string getMake() const {return make;} // *** const
string getModel() const {return model;} // *** const
Mar 21, 2014 at 4:04am UTC
Thank you. I fixed the problem but i ran into another one. It is not outputting anything to the screen when i try to call the class.
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
#include <iostream>
#include <string>
using namespace std;
//class Car
class Car{
protected :
string make;
string model;
public :
//constructor for car
Car(string mak, string mod){
make = mak;
model = mod;}
string getMake()const {return make;}
string getModel()const {return model;}
virtual void print_description() const {cout <<"Car maker: " << getMake() << ", Model:" << getModel() << endl;}
};
//classs Gaspowered
class GasPowered: public Car{
private :
double engine_size;
double fuel_tank_size;
public :
GasPowered( string mak, string mod, double e, double f ) : Car(mak,mod)
{engine_size = e; fuel_tank_size = f;}
double getEngine_size() const {return engine_size;}
double getFuel_tank_size()const {return fuel_tank_size;}
virtual void print_description ()const
{cout <<"Car maker: " << getMake() << ", Model:" << getModel() << "Engine size" << getEngine_size() << "Fuel tank size: " <<getFuel_tank_size() <<endl;}
};
int main()
{
Car car1("Toyota" , "Camry" );
GasPowered car2("Honda" , "Accord" ,2.4, 15);
return 0;
}
Mar 21, 2014 at 4:16am UTC
You aren't calling print_description() anywhere.
Mar 21, 2014 at 4:18am UTC
aww ok thanks. i tried calling it with a global function. any help to fix it will be appreciated. I am trying to pass by reference
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 44 45 46 47 48 49
#include <iostream>
#include <string>
using namespace std;
//class Car
class Car{
protected :
string make;
string model;
public :
//constructor for car
Car(string mak, string mod){
make = mak;
model = mod;}
string getMake()const {return make;}
string getModel()const {return model;}
virtual void print_description() const {cout <<"Car maker: " << getMake() << ", Model:" << getModel() << endl;}
};
//classs Gaspowered
class GasPowered: public Car{
private :
double engine_size;
double fuel_tank_size;
public :
GasPowered( string mak, string mod, double e, double f ) : Car(mak,mod)
{engine_size = e; fuel_tank_size = f;}
double getEngine_size() const {return engine_size;}
double getFuel_tank_size()const {return fuel_tank_size;}
virtual void print_description ()const
{cout <<"Car maker: " << getMake() << ", Model:" << getModel() << "Engine size" << getEngine_size() << "Fuel tank size: " <<getFuel_tank_size() <<endl;}
};
int main()
{
Car car1("Toyota" , "Camry" );
GasPowered car2("Honda" , "Accord" ,2.4, 15);
cout << get_specification(car1);
cout << get_specification(car2);
return 0;
}
string get_specification(string &car)
{
cout << car.print_description() << endl;
}
Last edited on Mar 21, 2014 at 4:29am UTC
Mar 21, 2014 at 5:05am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// declaration
/*string*/ void /*get_specification*/ print_specification( /*string &*/ const Car& car ) ;
int main()
{
Car car1("Toyota" , "Camry" );
GasPowered car2("Honda" , "Accord" ,2.4, 15);
// cout << get_specification(car1);
// cout << get_specification(car2);
// return 0;
print_specification(car1) ;
print_specification(car2) ;
}
// definition
/*string*/ void /*get_specification*/ print_specification( /*string &*/ const Car& car )
{
/*cout << */ car.print_description() /*<< endl */ ;
}
Topic archived. No new replies allowed.