C Program There will be the following classes each with its
C++ Program
There will be the following classes each with its own implementation and header files:
varray
vehicle
car
truck
The vehicle class is to be an abstract base class and the parent of the car and truck classes. Each class has a virtual draw() function to draw the proper diagram. Both cars and trucks have a position on the display. This position is the column number where to start the diagram and is entered by the user for each object created. Car objects have a model which is implemented through the C++ string class. Trucks have a gross and a net tonnage which are real numbers. The vehicle, car, and truck classes all have only default constructors. The member functions that can set instance variables for all classes all have to be called set. There can be no other functions that change instance variable values. The member functions that return instance values must be called get. There is, for example, no setName or getName function, just set and get. Each set and get function can only handle a single instance variable, with the exception of the get and set functions for the truck tonnages. Therefore there must be multiple set functions and multiple get functions. The set and get functions should be virtual and that it is the car or truck class which checks the values. Then the set functions can return true or false to indicate whether the value(s) were correctly set. Your classes should check to make sure that the diagram has enough room to be drawn starting from the column given by the user.
In the main program in file TopicE.cpp all objects will be stored in a varray object. This class has a dynamic array of vehicle base class pointers. The user is asked how many items will be entered. This varray class has a constructor that takes only one parameter – the size of the array to allocate. This class will overload the [] operator. Thus, for example if v is a varray object then v[ 0 ] is the first base class vehicle pointer element in the array, v[ 1 ] the second, etc. The << should also be overloaded for the class. That is cout << v should display all car and truck objects through the v object. The varray class should also prevent copies being made of any varray objects. The car and truck objects must be created in the client and each assigned to an array element in the client. That is, as the user inputs the data for a single car or truck, that vehicle must be created when all the data for it has been entered and this must be initiated in the client. No composite creation of all vehicles in any of the classes.
Note: Since the input will be of different data types, there is the problem of getting rid of the newline character.
Solution
#include <iostream>
using namespace std;
// base and derived class declarations
// vehicle base class
class vehicle
{
protected:
int wheels;
double weight;
public:
vehicle(void)
{
wheels = 7; weight = 11111.0;
cout<<\"Constructor #1, own by base class\"<<endl;
}
vehicle(int input_wheels, double input_weight)
{
wheels = input_wheels; weight = input_weight;
cout<<\"Constructor #2, own by base class\"<<endl;
}
void initialize(int input_wheels, double input_weight);
int get_wheels(void) {return wheels;}
double get_weight(void) {return weight;}
double wheel_load(void) {return (weight/wheels);}
};
// car derived class
class car : public vehicle
{
int passenger_load;
public:
car(void)
{
passenger_load = 4; cout<<\"Constructor #3, derived class, car\"<<endl<<endl;
}
car(int people, int input_wheels, double input_weight):vehicle(input_wheels, input_weight),passenger_load(people)
{
cout<<\"Constructor #4 derived class, car\"<<endl;
}
void initialize(int input_wheels, double input_weight, int people = 4);
int passengers(void) {return passenger_load;}
};
// truck derived class
class truck : public vehicle
{
int passenger_load;
double payload;
public:
truck(void)
{
passenger_load = 3;
payload = 22222.0;
}
truck(int people, double load, int input_wheels, double input_weight):vehicle(input_wheels, input_weight),passenger_load(people), payload(load)
{ }
void init_truck(int how_many = 4, double max_load = 24000.0);
double efficiency(void);
int passengers(void) {return passenger_load;}
};
// main program
int main(void)
{
// instantiate the base class object
vehicle unicycle;
// used for counter
int index;
unicycle.initialize(1, 12.5);
cout<<\"The unicycle has \" <<unicycle.get_wheels()<<\" wheel.\"<<endl;
cout<<\"The unicycle\'s wheel load is \"<<unicycle.wheel_load()<<\" kg on the single tire.\"<<endl;
cout<<\"The unicycle\'s weight \"<<unicycle.get_weight()<<\" kg.\"<<endl<<endl;
// an array of object with 3 elements
car sedan_car[3];
// count and execute
for (index = 0 ; index < 3 ; index++)
{
sedan_car[index].initialize(4, 3500.0, 5);
cout<<\"Count no. #\" <<index<<endl;
cout<<\"The sedan car carries \"<<sedan_car[index].passengers()<<\" passengers.\"<<endl;
cout<<\"The sedan car weighs \"<<sedan_car[index].get_weight()<<\" kg.\"<<endl;
cout<<\"The sedan car\'s wheel load is \"<<sedan_car[index].wheel_load()<<\" kg per tire.\"<<endl<<endl;
}
// instantiate the derived class object
truck *trailer;
// initialize to point to something, pointing to an object (allocate memory)
trailer = new truck;
if (trailer == NULL)
{
cout<<\"Memory allocation failed!\"<<endl;
exit(EXIT_FAILURE);
}
else
{
cout<<\"Memory allocation for object is OK!\"<<endl;
trailer->initialize(18, 12500.0);
trailer->init_truck(1, 33675.0);
cout<<\"The trailer\'s weight \" <<trailer->get_weight()<<\" kg.\"<<endl;
cout<<\"The trailer\'s efficiency is \"<<trailer->efficiency()<<\" %.\"<<endl;
//deallocate the object
delete trailer;
}
return 0;
}
// base and derived class implementation
// initialize to any desired data
void vehicle::initialize(int input_wheels, double input_weight)
{
wheels = input_wheels;
weight = input_weight;
}
void car::initialize(int input_wheels, double input_weight, int people)
{
passenger_load = people;
wheels = input_wheels;
weight = input_weight;
}
void truck::init_truck(int how_many, double max_load)
{
passenger_load = how_many;
payload = max_load;
}
double truck::efficiency(void)
{
return (payload / (payload + weight));
}





