1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Vehicle
{
public:
Vehicle(double Price = 0, int Mpg = 0);
void setPrice(double Price);
void setMpg(int Mpg);
double getPrice() const;
int getMpg() const;
void print() const;
private:
double price;
int mpg;
};
class Loan
{
public:
Loan(string Bank = "", double Amount = 0);
void setBank(string Bank);
void setAmount(double Amount);
string getBank() const;
double getAmount() const;
void print() const;
private:
string bank;
double amount;
};
class Car : public Vehicle
{
friend ostream& operator<<(ostream&, const Car&);
public:
Car(int nbrOfLoans = 2,
double Price = 0,
int Mpg = 0,
string Bank = "",
double Amount = 0,
string Name = ""
);
void setName(string Name);
string getName() const;
void setLoan();
Loan* getpLoan() const;
void print() const;
~Car();
bool operator== (const Car&) const;
private:
string name;
Loan *pLoan;
int nbrOfLoans;
};
Vehicle::Vehicle(double Price, int Mpg)
{
price = Price;
mpg = Mpg;
}
void Vehicle::setPrice(double Price)
{
price = Price;
}
void Vehicle::setMpg(int Mpg)
{
mpg = Mpg;
}
double Vehicle::getPrice() const
{
return price;
}
int Vehicle::getMpg() const
{
return mpg;
}
void Vehicle::print() const
{
cout << "Price: " << price << endl;
cout << "MPG: " << mpg << endl;
}
Loan::Loan(string Bank, double Amount)
{
bank = Bank;
amount = Amount;
}
void Loan::setBank(string Bank)
{
bank = Bank;
}
void Loan::setAmount(double Amount)
{
amount = Amount;
}
string Loan::getBank() const
{
return bank;
}
double Loan::getAmount() const
{
return amount;
}
void Loan::print() const
{
cout << "Bank: " << bank << endl;
cout << "Amount: " << amount << endl;
}
Car::Car(int nbrOfLoans,
double Price, int Mpg,
string Bank, double Amount,
string Name)
:Vehicle(Price, Mpg)
{
name = Name;
Car::nbrOfLoans = nbrOfLoans;
pLoan = new Loan[nbrOfLoans];
}
Car::~Car()
{
delete[]pLoan;
}
void Car::setName(string Name)
{
name = Name;
}
string Car::getName() const
{
return name;
}
void Car::setLoan()
{
double Amount;
string Bank;
for (int i = 0; i < nbrOfLoans; i++)
{
cout << "Enter bank and amount for loan " << i + 1 << ": ";
cin >> Bank >> Amount;
pLoan[i].setBank(Bank);
pLoan[i].setAmount(Amount);
}
}
Loan* Car::getpLoan() const
{
return pLoan;
}
void Car::print() const
{
Vehicle::print();
for (int i = 0; i < nbrOfLoans; i++)
{
cout << endl << "Loan " << i + 1 << ": " << endl;
pLoan[i].print();
cout << endl;
}
cout << "Name: " << name << endl << endl;
cout << fixed << showpoint << setprecision(1) << endl;
}
ostream& operator<<(ostream& os, const Car& car)
{
Loan* pLoan = car.getpLoan();
os << "Car name: " << car.name << endl
<< "MPG: " << car.getMpg() << endl
<< "Bank: " << pLoan[0].getBank();
return os;
}
bool Car::operator== (const Car& car) const
{
if (
name == car.name
&& pLoan[0].getAmount() == car.pLoan[0].getAmount()
&& pLoan[0].getBank() == car.pLoan[0].getBank()
&& getMpg() == car.getMpg()
&& getPrice() == car.getPrice()
)
return true;
}
int main()
{
string name, bank;
double price, amount;
int mpg;
Car car1(1, 24800.0, 22, "Citi", 21600, "Mustang");
Car car2;
Car *pCar1;
Car *pCar2;
pCar1 = &car1;
pCar2 = &car2;
pCar1->print();
cout << endl;
cout << "Enter name: ";
cin >> name;
cout << "Enter price: ";
cin >> price;
cout << "Enter MPG: ";
cin >> mpg;
pCar2->setLoan();
pCar2->setPrice(price);
pCar2->setMpg(mpg);
pCar2->setName(name);
pCar2->print();
system("pause");
return 0;
}
|