Using the given class header file (vehicles.h), you will need to implement vehic
ID: 3727533 • Letter: U
Question
Using the given class header file (vehicles.h), you will need to implement vehicles.cpp. Do not make any changes to vehicles.h .
From the vehicles class you will need to make two child classes, watercraft and automobile. These should also be implemented using separate header and cpp files (e.g., watercraft.h, watercraft.cpp, automobile.h, automobile.cpp)
Your watercraft class must have the following:
- Two additional attributes, hull and manufacture , in the private section. They should be of
type string
- A default constructor that takes no arguments and sets the attributes to:
- engine to "Minn Kota Max 70"
- weight to 1860
- hull to "V - Hull"
- seats to 12
- manufacture to "Sea Ark Boats"
- Member functions to access and modify these attributes (i.e., seats, engine, hull, etc.
must all be able to be changed by calling a member function)
- Print to file should take a string as an argument and be a member function. This should
be the name of the file to be printed to, your default output file should be out.txt
- A static int counter that will be used to count how many watercraft objects were
created.
- An operator overloaded function that is a member function of the class for the “=” sign
that will allow you to copy the information from one watercraft object to another.
Your automobile class must have the following:
- Two additional attributes of type string , make and model , and one additional attribute of
type int : wheels . All of them should be defined in the private section.
A default constructor that takes no arguments and sets the attributes to:
- engine to "V6"
- wheels to 4
- weight to 4079
- seats to 2
- make to "Toytoa"
- model to "Solara"
- A constructor that takes two string arguments that correspond to make and model in that order and one integer argument setting the number of wheels.
- Member functions to access and modify the attributes (seats, engine, wheels etc. must all be able to be changed by calling a member function)
- Print to file should take a string as an argument and be a member function. This should be the name of the file to be printed to, your default output file should be out.txt
- A static int counter that will be used to count how many automobile objects were created.
- An operator overload function that is a member function for the class for the = sign that will allow you to copy the information from one automobile object to another.
In your main.cpp you will need to create 3 objects of type watercraft, and 4 of type automobile. The first two watercraft objects will be created with the default constructor. Two objects of the type automobile will be created with the default constructor. The third automobile object should be created with the constructor that accepts three arguments, make, model, and wheels. Wheels should have a default value of 4 set if it is not provided in the constructor. Please refer to the example output files for values to use for make and model.
Watercraft object 3 and automobile object 4 should use the overloaded ‘=’ to assign the information from the second object of each type. (ie boat3 = boat2 and car4 = car2)
The first object of each type contains the information for the default constructor. The second object of each has been modified via the mutators, and the third automobile has been created with the constructor that takes arguments for the make and model attributes.
The last two objects in the file are the result of using the overloaded ‘=’ and should be identical to earlier objects.
You will also need to use three static member counters to keep track of how many objects are made. The last three lines of the output file reflect this. HINT: The static member variables need to be initialized outside of the main function before any objects are created.
that needs to be in file "out.txt"
Vehicles.hExplanation / Answer
Solution:
Now the static variables are incremented only in their respective files. Also now they are printed to output file instead of console. Kindly rate this solition positively if you find the anwer satisfactory.
vehicles.h
#ifndef GA2_VEHICLES_H
#define GA2_VEHICLES_H
#include <string>
using namespace std;
class vehicles{
protected:
string engine;
int seats;
int weight;
public:
vehicles();
static int count;
};
int vehicles::count=0;
#endif //GA2_VEHICLES_H
*********************
vehicles.cpp:
#include "vehicles.h"
vehicles::vehicles(){
engine="";
seats=0;
weight=0;
}
**********
watercraft.cpp
#include "watercraft.h"
#include <stdio.h>
#include <iostream>
watercraft::watercraft(){
engine="Minn Kota Max 70";
weight=1860;
hull="V-hull";
seats=12;
manufacture="Sea Ark Boats";
count++;
}
void watercraft::setEngine(string e){
engine=e;
}
void watercraft::setSeats(int s){
seats=s;
}
void watercraft::setWeight(int w){
weight=w;
}
void watercraft::setHull(string h){
hull=h;
}
void watercraft::setManufacture(string m){
manufacture=m;
}
string watercraft::getEngine(){
return engine;
}
int watercraft::getSeats(){
return seats;
}
int watercraft::getWeight(){
return weight;
}
string watercraft::getHull(){
return hull;
}
string watercraft::getManufacture(){
return manufacture;
}
void watercraft::printToFile(string name){
FILE *fp=fopen(name.c_str(),"a+");
fprintf(fp,"Watercraft ");
fprintf(fp, "Engine: %s ",engine.c_str());
fprintf(fp, "Weight: %d ",weight);
fprintf(fp, "Seats: %d ",seats);
fprintf(fp, "Hull: %s ",hull.c_str());
fprintf(fp, "Manufacture: %s ",manufacture.c_str());
fclose(fp);
}
void watercraft::operator = (watercraft w){ //copy all the contents from w object to the called object
engine=w.engine;
seats=w.seats;
weight=w.weight;
hull=w.hull;
manufacture=w.manufacture;
}
**********
watercraft.h
#include <string>
#include "vehicles.h"
using namespace std;
class watercraft:public vehicles{ //watercraft inherits vehicles
private:
string hull;
string manufacture;
public:
watercraft();
//setter methods
void setEngine(string e);
void setSeats(int s);
void setWeight(int w);
void setHull(string h);
void setManufacture(string m);
//getter methods
string getEngine();
int getSeats();
int getWeight();
string getHull();
string getManufacture();
void printToFile(string name);
static int count;
void operator = (watercraft w);
};
int watercraft::count=0;
*******
automobile.h
#include <string>
#include "vehicles.h"
using namespace std;
class automobile:public vehicles{//inheritence
private:
string make;
string model;
int wheels;
public:
automobile();
automobile(string m1,string m2,int w);
automobile(string m1,string m2);
//setter methods
void setEngine(string e);
void setSeats(int s);
void setWeight(int w);
void setMake(string m);
void setModel(string m);
void setWheels(int w);
//getter methods
string getEngine();
int getSeats();
int getWeight();
string getMake();
string getModel();
int getWheels();
void printToFile(string name);
static int count;
void operator = (automobile w);
};
int automobile::count=0;
*******
automobile.cpp
#include "automobile.h"
#include <stdio.h>
automobile::automobile(){
engine="V6";
wheels=4;
weight=4079;
seats=2;
make="Toytoa";
model="Solara";
count++;
}
automobile::automobile(string m1,string m2,int w){
engine="V6";
weight=4079;
seats=2;
make=m1;
model=m2;
wheels=w;
count++;
}
automobile::automobile(string m1,string m2){
weight=4079;
seats=2;
engine="V6";
make=m1;
model=m2;
wheels=4;
count++;
}
void automobile::setEngine(string e){
engine=e;
}
void automobile::setSeats(int s){
seats=s;
}
void automobile::setWeight(int w){
weight=w;
}
void automobile::setMake(string m){
make=m;
}
void automobile::setModel(string m){
model=m;
}
void automobile::setWheels(int w){
wheels=w;
}
string automobile::getEngine(){
return engine;
}
int automobile::getSeats(){
return seats;
}
int automobile::getWeight(){
return weight;
}
string automobile::getMake(){
return make;
}
string automobile::getModel(){
return model;
}
int automobile::getWheels(){
return wheels;
}
void automobile::printToFile(string name){
FILE* fp=fopen(name.c_str(),"a+");
fprintf(fp,"Automobile ");
fprintf(fp, "Engine: %s ",engine.c_str());
fprintf(fp, "Wheels: %d ",wheels);
fprintf(fp, "Weight: %d ",weight);
fprintf(fp, "Seats: %d ",seats);
fprintf(fp, "Make: %s ",make.c_str());
fprintf(fp, "Model: %s ",model.c_str());
fclose(fp);
}
void automobile::operator = (automobile w){ //copy all the contents from w object to the called object
engine=w.engine;
seats=w.seats;
weight=w.weight;
wheels=w.wheels;
make=w.make;
model=w.model;
}
**************
main.cpp
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "watercraft.h"
#include "automobile.h"
#include "vehicles.h"
using namespace std;
int main(){
watercraft boat1; //create object with default constructor
automobile car1; //create object with default constructor
watercraft boat2; //create object with default constructor
automobile car2; //create object with default constructor
//mutations
boat2.setEngine("Yamaha 115 HP");
boat2.setWeight(5200);
boat2.setSeats(10);
boat2.setHull("Fiberglass");
//mutations
car2.setEngine("3A90 I3");
car2.setWeight(2072);
car2.setSeats(5);
car2.setMake("Mirage");
car2.setModel("Mitsubishi");
automobile car3("Honda","Accord"); //parameterized constructor
watercraft boat3;
boat3=boat2; //equating objects
automobile car4;
car4=car2;//equating objects
//print to file
boat1.printToFile("out.txt");
boat2.printToFile("out.txt");
car1.printToFile("out.txt");
car2.printToFile("out.txt");
car3.printToFile("out.txt");
boat3.printToFile("out.txt");
car4.printToFile("out.txt");
FILE* fp=fopen("out.txt","a+");
fprintf(fp,"Vehicles created %d ",automobile::count+watercraft::count);
fprintf(fp,"Automobile created %d ",automobile::count);
fprintf(fp,"Watercraft created %d ",watercraft::count);
fclose(fp);
return 0;
}
****************
run command
g++ main.cpp automobile.cpp watercraft.cpp vehicles.cpp
./a.out
Sample output
Watercraft
Engine: Minn Kota Max 70
Weight: 1860
Seats: 12
Hull: V-hull
Manufacture: Sea Ark Boats
Watercraft
Engine: Yamaha 115 HP
Weight: 5200
Seats: 10
Hull: Fiberglass
Manufacture: Sea Ark Boats
Automobile
Engine: V6
Wheels: 4
Weight: 4079
Seats: 2
Make: Toytoa
Model: Solara
Automobile
Engine: 3A90 I3
Wheels: 4
Weight: 2072
Seats: 5
Make: Mirage
Model: Mitsubishi
Automobile
Engine: V6
Wheels: 4
Weight: 4079
Seats: 2
Make: Honda
Model: Accord
Watercraft
Engine: Yamaha 115 HP
Weight: 5200
Seats: 10
Hull: Fiberglass
Manufacture: Sea Ark Boats
Automobile
Engine: 3A90 I3
Wheels: 4
Weight: 2072
Seats: 5
Make: Mirage
Model: Mitsubishi
Vehicles created 7
Automobile created 4
Watercraft created 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.