使用c++的类建立一个简单的卖玩具程序。类内必须具有玩具单价,售出数量以及每种玩具售出的总金额等数据,并为该类建立一些必要的函数,并在主程序中使用对象数组建立若干带有单价和售出数量的对象,显示每种玩具售出的总金额。
时间: 2024-05-19 07:16:44 浏览: 31
好的,我知道了。以下是基于您的要求编写的 C++ 代码:
#include <iostream>
using namespace std;
class Toy {
private:
double unit_price;
int sold_quantity;
public:
Toy(double price, int quantity) {
unit_price = price;
sold_quantity = quantity;
}
double get_total_amount() {
return unit_price * sold_quantity;
}
};
int main() {
Toy toys[3] = { Toy(12.5, 5), Toy(8.8, 3), Toy(6.3, 7) };
for (int i = 0; i < 3; i++) {
cout << "Toy #" << i + 1 << " sold for a total of $" << toys[i].get_total_amount() << endl;
}
return 0;
}
在这个程序中,我们定义了一个名为 Toy 的类,该类具有单价和售出数量,以及计算每种玩具总销售额的函数。然后,我们在主函数中创建一个对象数组,每个对象都有单价和售出数量,然后我们遍历数组并显示每种玩具售出的总金额。
相关问题
使用 C ++的类建立一个简单的卖玩具程序。类内必须具有玩具单价、售出数量以及每种玩具售出的总金额等数据,并为该类建立一些必要的函数,并在主程序中使用对象数组建立若干带有单价和售出数量的对象,显示每种玩具售出的总金额。
好的,这个问题可以回答。以下是一个 C++ 类建立简单卖玩具程序的示例代码:
```cpp
#include <iostream>
using namespace std;
class Toy {
public:
float price;
int quantity;
float total;
void setPrice(float p) {
price = p;
}
void setQuantity(int q) {
quantity = q;
}
void calcTotal() {
total = price * quantity;
}
void display() {
cout << "单价:" << price << endl;
cout << "售出数量:" << quantity << endl;
cout << "总金额:" << total << endl;
}
};
int main() {
Toy toys[3];
toys[0].setPrice(10.5);
toys[0].setQuantity(100);
toys[0].calcTotal();
toys[1].setPrice(15);
toys[1].setQuantity(50);
toys[1].calcTotal();
toys[2].setPrice(20);
toys[2].setQuantity(80);
toys[2].calcTotal();
for (int i = 0; i < 3; i++) {
toys[i].display();
}
return 0;
}
```
这个程序使用一个 Toy 类来模拟卖玩具的过程。类中包含了玩具单价、售出数量以及每种玩具售出的总金额等数据,并为该类建立了 setPrice、setQuantity、calcTotal、display 等必要的函数。在主程序中,使用对象数组建立了三个带有单价和售出数量的对象,在循环中显示了每种玩具售出的总金额。
希望能够帮到你,如果你还有其他问题,可以继续提出。
使用c++的类建立一个简单的卖玩具程序
好的,下面是使用C++的类建立一个简单的卖玩具程序的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Toy {
private:
string name;
double price;
public:
Toy(string n, double p) {
name = n;
price = p;
}
string getName() {
return name;
}
double getPrice() {
return price;
}
};
int main() {
Toy toy1("小熊", 29.99);
Toy toy2("小汽车", 19.99);
Toy toy3("娃娃", 39.99);
cout << "欢迎来到玩具店!" << endl;
cout << "我们有以下玩具出售:" << endl;
cout << toy1.getName() << ",售价:" << toy1.getPrice() << "元" << endl;
cout << toy2.getName() << ",售价:" << toy2.getPrice() << "元" << endl;
cout << toy3.getName() << ",售价:" << toy3.getPrice() << "元" << endl;
return ;
}
```
这个程序定义了一个Toy类,包含了玩具的名称和价格。在main函数中,我们创建了三个Toy对象,分别代表小熊、小汽车和娃娃。然后输出了欢迎语和玩具的信息。这只是一个简单的示例,你可以根据自己的需求扩展这个程序。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)