#include <iostream>
#include <string>
using namespace std;
class Box {
?public:
? //构造函数
? Box() {
? ?cout << "Box" << " " << "无参构造函数..." << endl;
? ?width = -1;
? ?height = -1;
? };
? Box(int a, int b) {
? ?cout << "Box" << " " << "有参构造函数..." << endl;
? ?width = a;
? ?height = b;
? };
? //析构函数
? ~Box() {
? ?cout << "Box" << " " << "析构函数..." << endl;
? };
? //成员函数
? void display() const;
?protected:
? int width;
? int height;
};
//TODO Box类成员函数实现--编写代码
class ColoredBox: public Box {
?public:
? //构造函数
? ColoredBox() {
? ?cout << "ColoredBox" << " " << "无参构造函数..." << endl;
? ?color = "黑色";
? };
? ColoredBox(int a, int b, string c): Box(a, b) {
? ?cout << "ColoredBox" << " " << "有参构造函数..." << endl;
? ?color = c;
? };
? //析构函数
? ~ColoredBox() {
? ?cout << "ColoredBox" << " " << "析构函数..." << endl;
? };
? //成员函数
? void display() const;
?private:
? string color;
};
//TODOColoredBox类成员函数实现--编写代码
void Box::display() const {
?cout << "Box" << " " << "width=" << width << ',' << "height=" << height << endl;
}
void ColoredBox::display() const {
?cout << "ColoredBox" << " " << "width=" << width << ',' << "height=" << height << ',' << "color=" << color << endl;
}
int main() {
?Box box1;
?ColoredBox box2;
?int width, height;
?string color;
?cin >> width >> height >> color;
?ColoredBox box3(width, height, color);
?box1.display();
?box2.display();
?box3.display();
?Box(box3).display();
?return 0;
}
#include <iostream>
using namespace std;
class Vehicle {
?public:
? //构造函数
? Vehicle() {
? ?cout << "Vehicle " << "无参构造函数..." << endl;
? ?wheels = 4;
? ?weight = 2;
? };
? Vehicle(int a, int b) {
? ?cout << "Vehicle " << "有参构造函数..." << endl;
? ?wheels = a;
? ?weight = b;
? };
? //析构函数
? ~Vehicle() {
? ?cout << "Vehicle " << "析构函数..." << endl;
? };
? //成员函数
? void show() const;
?protected:
? int wheels;
? //车轮数
? int weight;
? //车重
};
//TODO Vehicle类成员函数实现
class Car: public Vehicle {
?public:
? //构造函数
? Car() {
? ?cout << "Car " << "无参构造函数..." << endl;
? ?passenger = 4;
? };
? Car(int a, int b, int c): Vehicle(a, b) {
? ?cout << "Car " << "有参构造函数..." << endl;
? ?passenger = c;
? };
? //析构函数
? ~Car() {
? ?cout << "Car " << "析构函数..." << endl;
? };
? //成员函数
? void show() const;
?private:
? int passenger;
? //载人数
};
//TODO Car类成员函数实现
class Truck: public Vehicle {
?public:
? //构造函数
? Truck() {
? ?cout << "Truck " << "无参构造函数..." << endl;
? ?passenger = 2;
? ?payload = 10;
? };
? Truck(int a, int b, int c, int d): Vehicle(a, b) {
? ?cout << "Truck " << "有参构造函数..." << endl;
? ?passenger = c;
? ?payload = d;
? };
? //析构函数
? ~Truck() {
? ?cout << "Truck " << "析构函数..." << endl;
? };
? //成员函数
? void show() const;
?private:
? int passenger;
? //载人数
? int payload;
? //载重量
};
//TODO Truck类成员函数实现
//主函数
void Vehicle::show() const {
?cout << "Vehicle " << "wheels=" << wheels << ',' << "weight=" << weight << endl;
}
void Car::show() const {
?cout << "Car " << "wheels=" << wheels << ',' << "weight=" << weight << ',' << "passenger=" << passenger << endl;
}
void Truck::show() const {
?cout << "Truck " << "wheels=" << wheels << ',' << "weight=" << weight << ',' << "passenger=" << passenger << "," <<
? ? ? "payload=" << payload << endl;
}int main() {
?int wheels, weight, passenger, payload;
?Vehicle vc1;
?vc1.show();
?Car car1;
?car1.show();
?cin >> wheels >> weight >> passenger;
?Car car2(wheels, weight, passenger);
?car2.show();
?Truck trk1;
?trk1.show();
?cin >> wheels >> weight >> passenger >> payload;
?Truck trk2(wheels, weight, passenger, payload);
?trk2.show();
?return 0;
}
?
|