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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值