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;
}