C++ Programming Assignment: Define a class named Vehicle that has Private member
ID: 3691879 • Letter: C
Question
C++ Programming Assignment:
Define a class named Vehicle that has
Private members:
• double price
• int mpg
Public getter and setter functions and a print function to display price and maker.
A constructor with default values of 0 for mpg and 0 for price.
Define a class named Loan that has
Private members:
• string bank
• double amount
Public getter and setter functions, and a print function to display bank and amount.
A constructor with default values of 0 for amount, and empty string for bank.
Define a class named Car that is a child of Vehicle that has
Private members:
• string name
• Loan loan
Public getter and setter functions, and a print function that displays the name, and also calls the parent print function and the loan print function.
A constructor with default values of an empty string for name, as well as default values for the parent price and mpg and default values for loan bank and amount.
Do not write any inline functions or constructors. Prototype your functions and constructors in your classes, then separately define your functions and constructors.
In the main function instantiate a Car named car1 with the values:
24800 // price
22 // mpg
Mustang // name
Citi // bank
21600 // amount
Instantiate another Car named car2 without any default values.
Create 2 pointers pCar1, and pCar2, and point pCar1 to car1, and pCar2 to car2.
Use your pointers for the rest of the main function instead of car1 or car2.
Have the user enter values to provide data for pCar2 and populate pCar2 with those data.
Finally, print both cars.
Sample print output for pCar1:
Name: Mustang, MPG: 22, Price: 24800, Loan amount: 21600, Bank: Citi
Explanation / Answer
class Vehicle{
private:
//variable decalarations
double price;
int mpg;
public:
//setter and getter methods
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return price;
}
public void setMpg(int mpg){
this.mpg = mpg;
}
public double getMpg(){
return mpg;
}
//default constructor
Vehicle(){
price =0;
mpg=0;
}
};
class Loan{
private:
//variable decalration
string bank;
double amount;
public:
//setter and getter methods
public void setBank(string bank){
this.bank = bank;
}
public string getBank(){
return bank;
}
public void setAmount(double amount){
this.amount = amount;
}
public double getAmount(){
return amount;
}
//print function
public void print(){
cout<<"bank details"<<bank<<endl;
cout<<"amount details"<<amount<<endl;
}
//default constructor
Loan(){
amount =0;
bank='';
}
};
class Car:public Vehicle{
private:
string name;
Loan loan;
//setter and getter
public void setName(string name){
this.name = name;
}
public string getName(){
return name;
}
public void setLoan(){
this.loan = loan
}
public Loan getLoan(){
return loan;
}
//print function
public void print(){
cout<<name;
Loan::print();
}
//default constructor
Car() {
name ='';
Loan();
Vehicle();
}
};
int main(){
Car c1;
Car c2;
Car *pCar1 ,*pCar2;
pCar1 = &c1;
pCar2 = &c2;
pCar1.setPrice(30);
pCar1.getPrice();
pCar1.setPrice(10);
pCar1.getPrice();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.