20191228 C++面向对象的作业

需求:
创建一个类, 用来表示“玩具”
文具, 有以下数据:
名称,价格,产地。
在使用中,需要获取它的名称, 价格, 产地。
注意:根据自己当前的优惠情况,有一个对外的价格。
定义的toy.h类的头文件

//toy.h

#pragma once
#include<Windows.h>
#include<iostream>
#include<string>
using namespace std;

class toy
{
public:
	toy();
	toy(string name, int price, string origin);
	~toy();

	string getName()const;
	float getPrice()const;
	string getOrigin()const;
	void setDiscount(float discount);
	void description()const;
private:
	string name;
	int price;
	string origin;
	float discount;
};


toy.cpp

//toy.cpp
#include "toy.h"
toy::toy(){
	discount = 1.0f;
}

toy::toy(string name, int price, string origin) {
	this->name = name;
	this->price = price;
	this->origin = origin;
	discount = 1.0f;
}

toy::~toy()
{
}
string toy::getName() const{
	return name;
}
float toy::getPrice()const {
	return price * discount;
}
string toy::getOrigin() const {
	return origin;
}
void toy::setDiscount(float discount) {
	this->discount = discount;
}
void toy::description()const {
	cout << getName() << "-价格(" << getPrice() << ")-产地(" << getOrigin() << ")" << endl;
}

main.cpp文件

#include"toy.h"
int main() {
	toy toy1("遥控玩具车", 2560, "made in China");
	toy1.description();

	cout << "优惠大酬宾--5折销售" << endl;
	toy1.setDiscount(0.5);
	toy1.description();

	system("pause");
	return 0;
}
发布了51 篇原创文章 · 获赞 0 · 访问量 528

猜你喜欢

转载自blog.csdn.net/weixin_40071289/article/details/103741813
今日推荐