马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
main.cpp
- #include <iostream>
- #include "Vehicle.h"
- #include "Sedan.h"
- #include "Truck.h"
- using namespace std;
- int main()
- {
- Vehicle vehicle("Generic", 100, "Gray", Date(2005, 6, 6));
- Sedan sedan1("Toyota", 200, "Red", Date(2020, 1, 15), 5, 4);
- Sedan sedan2("Honda", 201, "Blue", Date(2021, 5, 20), 4, 6);
- Truck truck1("Ford", 300, "Black", Date(2019, 10, 5), 18, 40.0);
- Truck truck2("Chevy", 301, "Silver", Date(2022, 3, 10), 20, 50.0);
- Vehicle* vehicles[] = {&vehicle, &sedan1, &sedan2, &truck1, &truck2};
- for (const auto& vehiclePtr : vehicles)
- {
- vehiclePtr->print();
- }
- return 0;
- }
复制代码 Date.h
- #ifndef DATE_H
- #define DATE_H
- #include <iostream>
- using namespace std;
- class Date
- {
- friend class Vehicle;
- public:
- Date();
- Date(int y, int m, int d);
- ~Date();
- void setYear(int y);
- void setMonth(int m);
- void setDay(int d);
- int getYear();
- int getMonth();
- int getDay();
- void print();
- private:
- int Year;
- int Month;
- int Day;
- };
- #endif // DATE_H
复制代码 Date.cpp
- #include "Date.h"
- #include <iostream>
- using namespace std;
- Date::Date()
- {
- Year = 0;
- Month = 0;
- Day = 0;
- }
- Date::Date(int year, int month, int day)
- {
- setYear(year);
- setMonth(month);
- setDay(day);
- }
- Date::~Date()
- {
- //cout << "object Destructor is called" << endl;
- }
- void Date::setYear(int year)
- {
- Year = year;
- }
- void Date::setMonth(int month)
- {
- Month = month;
- }
- void Date::setDay(int day)
- {
- Day = day;
- }
- int Date::getYear()
- {
- return Year;
- }
- int Date::getMonth()
- {
- return Month;
- }
- int Date::getDay()
- {
- return Day;
- }
- void Date::print()
- {
- cout << endl;
- cout << "Year :" << getYear() << endl;
- cout << "Month :" << getMonth() << endl;
- cout << "Day :" << getDay() << endl;
- }
复制代码 Sedan.h
- #ifndef SEDAN_H
- #define SEDAN_H
- #include <iostream>
- #include "Vehicle.h"
- using namespace std;
- class Sedan :public Vehicle
- {
- public:
- Sedan();
- Sedan(string brand, int model, string color, Date MFD, int NOP, int NOA);
- ~Sedan();
- void setNumberOfPassenger(int P);
- int getNumberOfPassenger();
- void setNumberOfAirbag(int A);
- int getNumberOfAirbag();
- void print();
- private:
- int NumberOfPassenger;
- int NumberOfAirbag;
- };
- #endif // SEDAN_H
复制代码 Sedan.cpp
- #include "Sedan.h"
- #include <iostream>
- using namespace std;
- Sedan::Sedan()
- {
- NumberOfPassenger = 0;
- NumberOfAirbag = 0;
- }
- Sedan::Sedan(string brand, int model, string color, Date MFD, int NOP, int NOA)
- {
- setBrand(brand);
- setModel(model);
- setColor(color);
- setManufactureDate(MFD);
- setNumberOfPassenger(NOP);
- setNumberOfAirbag(NOA);
- }
- Sedan::~Sedan()
- {
- }
- void Sedan::setNumberOfPassenger(int P)
- {
- NumberOfPassenger = P;
- }
- int Sedan::getNumberOfPassenger()
- {
- return NumberOfPassenger;
- }
- void Sedan::setNumberOfAirbag(int A)
- {
- NumberOfAirbag = A;
- }
- int Sedan::getNumberOfAirbag()
- {
- return NumberOfAirbag;
- }
- void Sedan::print()
- {
- Vehicle::print();
- cout << "Number of Passengers: " << NumberOfPassenger << ", Number of Airbags: " << NumberOfAirbag << endl;
- }
复制代码 Truck.h
- #ifndef TRUCK_H
- #define TRUCK_H
- #include "Vehicle.h"
- using namespace std;
- class Truck : public Vehicle
- {
- public:
- Truck();
- Truck(string brand, int model, string color, Date MFD, int NOW, double TC);
- ~Truck();
- void setNumberOfWheel(int NOW);
- int getNumberOfWheel();
- void setTowingCapacity(double TC);
- double getTowingCapacity();
- void print();
- private:
- int NumberOfWheel;
- double TowingCapacity;
- };
- #endif // TRUCK_H
复制代码 Truck.cpp
- #include "Truck.h"
- #include <iostream>
- using namespace std;
- Truck::Truck()
- {
- NumberOfWheel = 0;
- TowingCapacity = 0;
- }
- Truck::Truck(string brand, int model, string color, Date MFD, int NOW, double TC)
- {
- setBrand(brand);
- setModel(model);
- setColor(color);
- setManufactureDate(MFD);
- setNumberOfWheel(NOW);
- setTowingCapacity(TC);
- }
- Truck::~Truck()
- {
- }
- void Truck::setNumberOfWheel(int NOW)
- {
- NumberOfWheel = NOW;
- }
- int Truck::getNumberOfWheel()
- {
- return NumberOfWheel;
- }
- void Truck::setTowingCapacity(double TC)
- {
- TowingCapacity = TC;
- }
- double Truck::getTowingCapacity()
- {
- return TowingCapacity;
- }
- void Truck::print()
- {
- Vehicle::print();
- cout << "Number of Wheels: " << NumberOfWheel << ", Towing Capacity: " << TowingCapacity << " tons" << endl;
- }
复制代码 Vehicle.h
- #ifndef VEHICLE_H
- #define VEHICLE_H
- #include <iostream>
- #include "Date.h"
- using namespace std;
- class Vehicle
- {
- public:
- Vehicle();
- Vehicle(string brand, int model, string color, Date MFD);
- ~Vehicle();
- void setBrand(string brand);
- string getBrand();
- void setModel(int id);
- int getModel();
- void setColor(string color);
- string getColor();
- void setManufactureDate(Date d);
- Date getManufactureDate();
- virtual void print();
- private:
- string Brand;
- int Model;
- string Color;
- Date ManufactureDate;
- };
- #endif // VEHICLE_H
复制代码 Vehicle.cpp
- #include "Vehicle.h"
- #include <iostream>
- using namespace std;
- Vehicle::Vehicle()
- {
- Brand = "XXX";
- Model = 10086;
- Color = "Black";
- }
- Vehicle::Vehicle(string brand, int model, string color, Date MFD)
- {
- setBrand(brand);
- setModel(model);
- setColor(color);
- setManufactureDate(MFD);
- }
- Vehicle::~Vehicle()
- {
- }
- void Vehicle::setBrand(string brand)
- {
- Brand = brand;
- }
- string Vehicle::getBrand()
- {
- return Brand;
- }
- void Vehicle::setModel(int id)
- {
- Model = id;
- }
- int Vehicle::getModel()
- {
- return Model;
- }
- void Vehicle::setColor(string color)
- {
- Color = color;
- }
- string Vehicle::getColor()
- {
- return Color;
- }
- void Vehicle::setManufactureDate(Date d)
- {
- ManufactureDate = d;
- }
- Date Vehicle::getManufactureDate()
- {
- return ManufactureDate;
- }
- void Vehicle::print()
- {
- cout << "Brand: " << Brand << ", Model: " << Model << ", Color: " << Color << ", ManufactureDate: ";
- ManufactureDate.print();
- cout << endl;
- }
复制代码 Result:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |