Q2: Continue of previous question 1. Copy the previous program to a new file. 2. Create a class named Car derived from Vehicle. a. Private data member: weight — dynamic member int b. Implement default and two argument constructors c. Redefine output function d. Mutator and accessor function for weight 3. Create a class named Boat derived from Vehicle. a. Private data member: hullLength — dynamic member int b. Implement default and two argument constructors c. Redefine output function d. Mutator and accessor function for hullLength
Hello, I need help with this C++ homework question
Q2: Continue of previous question
1. Copy the previous program to a new file.
2. Create a class named Car derived from Vehicle.
a. Private data member: weight — dynamic member int
b. Implement default and two argument constructors
c. Redefine output function d. Mutator and accessor function for weight
3. Create a class named Boat derived from Vehicle.
a. Private data member: hullLength — dynamic member int
b. Implement default and two argument constructors
c. Redefine output function
d. Mutator and accessor function for hullLength
Use following main() to test your function (DO NOT CHANGE THE INT MAIN. Any alteration will not be accepted)
int main(){
Car a,b("Honda",2000);
a.output(); // Brand:TBD Weight: 0
b.output(); // Brand:Honda Weight: 2000 b.setBrand("Tesla");
b.setWeight(3000);
b.output(); // Brand:Tesla Weight: 3000
Boat c,d("Baja",100);
c.output(); // Brand:TBD hullLength: 0
d.output(); // Brand:Baja hullLength: 100 d.setBrand("Bertram");
d.sethullLength(60);
d.output(); // Brand:Bertram hullLength: 60
}
Output from main:
Brand:TBD Weight: 0
Brand:Honda Weight: 2000
Brand:Tesla Weight: 3000
Brand:TBD hullLength: 0
Brand:Baja hullLength: 100
Brand:Bertram hullLength: 60
This is the code have from what is being asked
#include<iostream>
#include<string>
using namespace std;
class Vehicle{
private:
string *brand; //declaring dynamic variable
public:
Vehicle(){brand=new string;*brand="TBD";}
Vehicle(string b){brand=new string;*brand=b;}
void output(){cout<<"Brand: "<<*brand << "\n";}
void setBrand(string b){*brand=b;}
string getBrand(){return *brand;}
};
int main(){
Car a,b("Honda",2000);
a.output(); // Brand:TBD Weight: 0
b.output(); // Brand:Honda Weight: 2000
b.setBrand("Tesla");
b.setWeight(3000);
b.output(); // Brand:Tesla Weight: 3000
Boat c,d("Baja",100);
c.output(); // Brand:TBD hullLength: 0
d.output(); // Brand:Baja hullLength: 100
d.setBrand("Bertram");
d.sethullLength(60);
d.output(); // Brand:Bertram hullLength: 60
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images