用c加加写一个基类:过境人员,派生类:游客、卡车司机、商务旅客的长代码,具体要求1.从一个抽象基类和几个非抽象派生类创建一个层次结构。 2.在类描述中包含一个在创建对象后不会改变的唯一数字标识符: 3. 实现所有构造函数、必要的设置和获取方法、析构函数。 4.重载赋值运算符,比较运算符 == 根据你的准则,流输出运算符<<。5.创建一个指向基类的指针数组,填充并对该数组进行一定的操作,以及统计每个类的对象个数。6.基类和派生类的字段的其余组成,以及可执行的对数组的操作要自己想
时间: 2023-05-13 11:05:12 浏览: 104
出来。
以下是一个可能的实现:
```c++
#include <iostream>
#include <string>
class TransitPerson {
public:
TransitPerson(int id) : id_(id) {}
virtual ~TransitPerson() {}
int get_id() const { return id_; }
virtual std::string get_type() const = 0;
virtual void print_info() const {
std::cout << "ID: " << id_ << ", Type: " << get_type() << std::endl;
}
private:
int id_;
};
class Tourist : public TransitPerson {
public:
Tourist(int id, std::string country) : TransitPerson(id), country_(country) {}
std::string get_type() const override { return "Tourist"; }
std::string get_country() const { return country_; }
void print_info() const override {
std::cout << "ID: " << get_id() << ", Type: " << get_type()
<< ", Country: " << country_ << std::endl;
}
private:
std::string country_;
};
class TruckDriver : public TransitPerson {
public:
TruckDriver(int id, std::string license) : TransitPerson(id), license_(license) {}
std::string get_type() const override { return "TruckDriver"; }
std::string get_license() const { return license_; }
void print_info() const override {
std::cout << "ID: " << get_id() << ", Type: " << get_type()
<< ", License: " << license_ << std::endl;
}
private:
std::string license_;
};
class BusinessTraveler : public TransitPerson {
public:
BusinessTraveler(int id, std::string company) : TransitPerson(id), company_(company) {}
std::string get_type() const override { return "BusinessTraveler"; }
std::string get_company() const { return company_; }
void print_info() const override {
std::cout << "ID: " << get_id() << ", Type: " << get_type()
<< ", Company: " << company_ << std::endl;
}
private:
std::string company_;
};
int main() {
const int kNumPersons = 6;
TransitPerson* persons[kNumPersons] = {
new Tourist(1, "USA"),
new Tourist(2, "Japan"),
new TruckDriver(3, "ABC123"),
new TruckDriver(4, "XYZ789"),
new BusinessTraveler(5, "Google"),
new BusinessTraveler(6, "Microsoft")
};
int num_tourists = 0;
int num_truck_drivers = 0;
int num_business_travelers = 0;
for (int i = 0; i < kNumPersons; ++i) {
persons[i]->print_info();
if (Tourist* tourist = dynamic_cast<Tourist*>(persons[i])) {
++num_tourists;
std::cout << "Country: " << tourist->get_country() << std::endl;
} else if (TruckDriver* truck_driver = dynamic_cast<TruckDriver*>(persons[i])) {
++num_truck_drivers;
std::cout << "License: " << truck_driver->get_license() << std::endl;
} else if (BusinessTraveler* business_traveler = dynamic_cast<BusinessTraveler*>(persons[i])) {
++num_business_travelers;
std::cout << "Company: " << business_traveler->get_company() << std::endl;
}
}
std::cout << "Num tourists: " << num_tourists << std::endl;
std::cout << "Num truck drivers: " << num_truck_drivers << std::endl;
std::cout << "Num business travelers: " << num_business_travelers << std::endl;
for (int i = 0; i < kNumPersons; ++i) {
delete persons[i];
}
return 0;
}
```
这个程序定义了一个抽象基类 `TransitPerson`,包含一个唯一的数字标识符 `id_` 和一个纯虚函数 `get_type()`,表示该对象的类型。它还定义了三个派生类 `Tourist`、`TruckDriver` 和 `BusinessTraveler`,它们分别表示游客、卡车司机和商务旅客,每个类都重载了 `get_type()` 函数,返回相应的字符串,并添加了一些特定的字段和方法。
在 `main()` 函数中,我们创建了一个指向 `TransitPerson` 的指针数组 `persons`,并用几个不同的对象填充它。然后我们遍历该数组,对每个对象调用 `print_info()` 函数,打印出该对象的信息,并根据对象的类型统计对象的个数。最后,我们释放了所有的对象。
这个程序演示了如何使用抽象基类和派生类创建一个层次结构,以及如何使用动态类型转换和虚函数来处理不同类型的对象。
阅读全文