问题背景1
在学习面向对象的程序设计(C++语言描述)过程中,为了加深对构造函数、析构函数的理解,在VS2022平台上实现类的各项操作。 有以下题目: 构建一个汽车Car类,要求在类中设置:m_Factory,m_Model, m_Price, 三个成员变量,分别代表生产厂家,型号,价格。如生产厂家为“丰田”,型号为:“凯美瑞2021款”, 价格:“19.8”。添加默认构造函数及其它重载的构造函数,重载的构造函数由你自己酌情决定。添加析构函数,添加显示汽车信息的函数。在构造函数和析构函数中添加相应的输出显示信息以方便跟踪程序的流程。添加你认为必要的任何成员函数。 要求在main函数中
- (1)构造单个Car类的对象
- (2)用运行时通过键盘输入方式输入车辆数量,根据车辆数量利用new动态构造Car对象数组。
在这两种情况下,通过对象调用函数以显示信息。分析输出的信息,观察和说明构造函数和析构函数调用的情况。 ## 操作步骤
- 1.导入标准库头文件与String库。
- 2.创建一个Car类。
- 3.在类中写好变量的定义,以及成员函数的声明与实现。
- 4.写构造函数,并且在其中加入提示语句
- 5.写析构函数,并且在其中加入提示语句
- 6.写主函数,定义类的对象,调用成员函数
- 7.new方法定义一个对象数组。
- 8.for循环实现数组循环输入输出。
代码实现1
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
| #include<iostream> #include<string> using namespace std; class Car { private: string m_Factory; string m_Model; float m_Price; public: Car() { cout << "构造函数被调用" << endl; } ~Car() { cout << m_Factory << endl; cout << "析构函数被调用" << endl; } void Disinfo() { cout << "-------------" << endl; cout << "厂家:" << m_Factory << endl; cout << "型号:" << m_Model << endl; cout << "价格:" << m_Price << endl; cout << "-------------" << endl; } void setinfo() { cin >> m_Factory >> m_Model >> m_Price; }
}; int main() { string factory, model; float price=0.0; Car ca; cout << "输入厂家、型号、价格" << endl; ca.setinfo(); ca.Disinfo(); int count; cout<<"请输入车辆数:" << endl; cin >> count; Car* car; car = new Car[count]; for (int i = 0;i < count;i++) { cout << "请输入第" << i + 1 << "辆车的厂家、型号和价格:" << endl; car[i].setinfo();
} for (int i = 0;i < count;i++) { car[i].Disinfo(); } delete [] car; return 1; }
|
问题背景2
在学习面向对象的程序设计(C++语言描述)过程中,为了加深对构造函数、析构函数的理解,在VS2022平台上实现类的各项操作。 有以下题目: 在第一题基础上,在类中添加拷贝构造函数,并在其中添加相应的输出用以显示信息。添加一个外部函数,向这个函数传入某个汽车的对象和折扣(如7.5折),通过这个函数计算折扣后的价格,这个函数的返回值类型也为Car类型,折扣后的价格由这个返回值传递出去,然后在main函数中调用这个函数并显示折扣后的价格。分析程序的输出,特别观察和说明拷贝构造函数的调用情况。
操作步骤
- 1.导入标准库头文件与String库。
- 2.创建一个Car类。
- 3.在类中写好变量的定义,以及成员函数的声明与实现。
- 4.写构造函数,并且在其中加入提示语句
- 5.写析构函数,并且在其中加入提示语句
- 6.写好拷贝构造函数
- 7.new方法定义一个对象数组。
- 8.for循环实现数组循环输入输出。
代码实现2
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
| #include<iostream> #include<string> #include<iomanip> using namespace std; class Car { private: string m_Factory; string m_Model; double m_Price; public: void setinfo(string factory,string model,double price) { m_Factory = factory; m_Model = model; m_Price = price; } Car() { double m_Price = 0.0; cout << "构造函数被调用" << endl; } Car(const Car& c) { m_Factory = c.m_Factory; m_Model = c.m_Model; m_Price = c.m_Price; cout << "拷贝构造函数被调用" << endl; } ~Car() { cout << m_Factory << endl; cout << "析构函数被调用" << endl; }
void Disinfo() { cout << "-------------" << endl; cout << "厂家:" << m_Factory << endl; cout << "型号:" << m_Model << endl; cout << "价格:" << m_Price << endl; cout << "-------------" << endl; } void setinfo() { cout << "输入厂家、型号、价格" << endl; cin >> m_Factory >> m_Model >> m_Price; }
string get_f(); string get_m(); double get_p(); void set_f(string factory); void set_m(string model); void set_p(double price);
};
void Car::set_f(string factory) { m_Factory = factory; } void Car::set_m(string model) { m_Model = model; } void Car::set_p(double price) { m_Price = price; } string Car::get_f() { return m_Factory; } string Car::get_m() { return m_Model; } double Car::get_p() { return m_Price; }
Car discont(Car ca) { Car temstd(ca); double temcarprice = 0.0; cout << "temcarpice:" <<temcarprice<< endl; temcarprice = temstd.get_p(); cout << "temcarpice:" << temcarprice<<endl; temcarprice = temcarprice * 0.75; cout << "temcarpice:" << temcarprice<<endl; temstd.set_p(temcarprice); cout << "打75折之后" << endl; return temstd; }
int main() {
Car cara; cara.setinfo(); cara.Disinfo(); cara = discont(cara); cara.Disinfo();
}
|