Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Classes, inheritance, and operator overloading. Please show as many comments as

ID: 3725388 • Letter: C

Question

Classes, inheritance, and operator overloading. Please show as many comments as possible, I want to COMPLETELY understand the code. Will give a great rating if given correct solution and ton of comments.

1 - Objective Enhance the understanding of classes, inheritance, and operator overloading 2 - Problem 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 -

Explanation / Answer

Solution:

vehicles.h file is same as that given in the question. Still I will add it here

6 files: vehicles.cpp, watercraft.h,watercraft.cpp,automobile.h,automobile.cpp,main.cpp

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;
};

#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";

}
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);


};

*******

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);

};

*******

automobile.cpp

#include "automobile.h"
#include <stdio.h>
automobile::automobile(){
   engine="V6";
   wheels=4;
   weight=4079;
   seats=2;
   make="Toytoa";
   model="Solara";
}
automobile::automobile(string m1,string m2,int w){
   engine="V6";
   weight=4079;
   seats=2;
   make=m1;
   model=m2;
   wheels=w;
}
automobile::automobile(string m1,string m2){
   weight=4079;
   seats=2;
   engine="V6";
  
   make=m1;
   model=m2;
   wheels=4;
}

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 vehicles::count=0; //accessing static variables of all classes
int automobile::count=0;
int watercraft::count=0;


int main(){
watercraft boat1; //create object with default constructor
watercraft::count++;

automobile car1; //create object with default constructor
automobile::count++;

watercraft boat2; //create object with default constructor
watercraft::count++;  

automobile car2; //create object with default constructor
automobile::count++;

//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
automobile::count++;

watercraft boat3;
boat3=boat2; //equating objects
watercraft::count++;

automobile car4;
car4=car2;//equating objects
automobile::count++;


//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");


cout<<"Vehicles created "<<automobile::count+watercraft::count<<endl;
cout<<"Automobile created "<<automobile::count<<endl;
cout<<"Watercraft created "<<watercraft::count<<endl;


return 0;
}

****************

run command

g++ main.cpp automobile.cpp watercraft.cpp vehicles.cpp