Vehicle友元Date多态Sedan和Truck

main.cpp

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

cpp 复制代码
#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

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

cpp 复制代码
#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

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

cpp 复制代码
#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

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

cpp 复制代码
#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

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:

相关推荐
翀哥~2 小时前
python VS c++
开发语言·c++·python
HABuo4 小时前
【数据结构与算法】合并链表、链表分割、链表回文结构
c语言·开发语言·数据结构·c++·学习·算法·链表
努力学习编程的伍大侠4 小时前
C++中的移动语义
开发语言·c++
奇妙之二进制4 小时前
2025年春招修订版《C/C++笔面试系列》(1) C语言经典笔面试题(上)
c语言·c++·面试
萱仔学习自我记录4 小时前
C++ 类和对象的定义涉及到的关键字记录
开发语言·c++
我不是程序猿儿4 小时前
【C++】友元friend的含义和用法
开发语言·c++
დ旧言~5 小时前
实战项目 Boost 搜索引擎
服务器·c语言·前端·网络·汇编·c++
疯狂吧小飞牛7 小时前
无锁编程–C语言
c语言·数据结构·c++·算法
cccccc语言我来了7 小时前
详解 【AVL树】
数据结构·c++
wqyc++7 小时前
C++ 中的 Lambda 表达式
开发语言·c++