Toy.h:
#pragma once
#include <iostream>
#include <windows.h>
#define DISCOUNT 5 //折扣,打5折
using namespace std;
class Toy {
public:
Toy(); //构造函数
Toy(string name,int price,string origin);
string getName()const;//获取玩具的名字
int getPrice()const;//获取玩具的价格
string getOrigin()const;//获取玩具的产地
void description()const;//写出具体描述
private:
string name;//名字
int price;//价格
string origin;//产地
};
Toy::Toy() {
name = "没有玩具";
price = 0;
origin = "无";
}
Toy::Toy(string name, int price, string origin)
{
this->name = name;
this->price = price;
this->origin = origin;
}
string Toy::getName() const
{
return name;
}
int Toy::getPrice() const
{
return (int)(price * DISCOUNT * 0.1);
}
string Toy::getOrigin() const
{
return origin;
}
void Toy::description() const
{
cout << name<< ":" << price << "[mode in " << origin << "]" << endl
<< "打折大酬宾:"<< DISCOUNT <<"折" << endl <<
getName()<<":" << getPrice() << "[mode in " << getOrigin() << "]" << endl;
}
Toy.cpp:
#include <iostream>
#include <windows.h>
#include "源.h"
using namespace std;
int main() {
Toy toy1("变形金刚", 5600, "China");
toy1.description();
system("pause");
return 0;
}
分类:
C++基础第一卷
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探