Hi, my name is Matt Barker and I am c++ beginner. So far I have been learning C++ programming for about a month. I am kind of confused with how classes and inheritance work and was hoping you guys could help me this program.
I am given 3 classes:
The following is a specification of three classes:
Class Vehicle:
Attributes:
Age, an integer, private The age of the vehicle
Price, a float, protected The price of the vehicle
Behaviors:
Vehicle(), public // default constructor sets age=0, and price=0.0
~Vehicle(), public // destructor sets age=0, and price=0.0
setAge(), public // Takes an integer parameter, returns nothing
setPrice(), public // Takes a float parameter, returns nothing
getAge(), public // Takes no parameters, returns the vehicle’s age
getPrice(), public // Takes no parameters, returns the vehicle’s price
End Class Vehicle
Class Car:
Attributes:
An object of type Car has all the attributes of an
object of type Vehicle
Additionally, Cars have attributes that Vehicles do not:
RaceCarStatus, a Boolean, public yes or no
Behaviors:
An object of type Car has all the behaviors of an object of
type Vehicle
Additionally, Cars have behaviors that Vehicles do not:
Car() // default constructor sets RaceCarStatus=false
Car() // copy constructor takes one parameter of class Car
~Car() // destructor sets RaceCarStatus=false
setRaceCarStatus(), public // Takes an boolean parameter, returns nothing
getRaceCarStatus(), public // Takes no parameters, returns the car’s race car status
End Class Car
Class Truck:
Attributes:
An object of type Truck has all the attributes of an
object of type Vehicle
Additionally, Trucks have attributes that Vehicles do not:
DieselTypeStatus, a Boolean, private yes or no
Behaviors:
An object of type Truck has all the behaviors of an object of
type Vehicle
Additionally, Cars have behaviors that Vehicles do not:
Truck(), public // default constructor sets DieselTypeStatus=false
Truck(), public // parametric constructor takes a boolean parameter and sets DieselTypeStatus=the boolean parameter
~Truck(), public // destructor sets DieselTypeStatus=false
setDieselTypeStatus(), public //Takes a boolean parameter, returns nothing
getDieselTypeStatus(), public // Takes no parameters, returns the Truck’s diesel type status
End Class Truck
I am supposed to implement the three classes described earlier (Vehicle, Car and Truck) and write a driver function (main) in order to test them.
So far here is the code I have:
#include <iostream>
using namespace std;
Class Vehicle
{
private:
int Age; //The age of the vehicle
protected:
float Price; //The price of the vehicle
public:
Vehicle(): {} //default constructor sets age=0, and price=0.0
~Vehicle(); //destructor sets age=0, and price=0.0
setAge(); //Takes an integer parameter, returns nothing
setPrice(); //Takes a float parameter, returns nothing
int getAge(){return Age;};// Takes no parameters, returns the vehicle’s age
float getPrice(){return Price;}; // Takes no parameters, returns the vehicle’s price
};
Class Car:
{
public:
Car();
~Car();
bool RaceCarStatus;
setRaceCarStatus();
bool getRaceCarStatus(){return(RaceCarStatus;};
}
Class Truck:
{
Could you guys tell me if I am doing this program right so far or correct me if I am making any errors. I am really confused on how to implement the constructors. Thanks
What do you mean implement the constructors? You mean implementing the base class constructor through the inherted class?
If thats what you mean. Here is what you do.
1 2 3 4 5 6 7 8 9 10 11 12
Class Truck{
public:
Truck( bool x)
:private-variable-from-base-class( SetValue ), //as many as you need to private variables of base class you need to assign through Truck's constructor.... make sure to ommit the ";"
{
~~~~~Trucks regular constructor code~~~~ for example:
y = x;
}