自学内容网 自学内容网

【27】C++项目练习

练习1

题目如下

代码如下

.h

#pragma once
#include <string>
using namespace std;

class Toy
{
public:
Toy();
Toy(string name,int price,string place);
~Toy();

string getName() const;
int getPrice() const;
string getPlace() const;

void changePrice(float count);
void discription();
private:
string name;
int price;
string place;
float count = 1.0;
};

.cpp

#include "Toy.h"
#include <string>
#include <iostream>

using namespace std;

Toy::Toy()
{
}

Toy::Toy(string name, int price, string place)
{
this->name = name;
this->price = price;
this->place = place;
}

Toy::~Toy()
{
}

string Toy::getName() const
{
return name;
}

int Toy::getPrice() const
{
return price;
}

string Toy::getPlace() const
{
return place;
}

void Toy::changePrice(float count)
{

this->price = price*count;
}

void Toy::discription()
{
cout << this->name << ":" << this->price << "," << this->place << endl;
}

 main.cpp

#include "Toy.h"

int main() {
Toy toy("变形金刚", 5600, "China");
toy.discription();
toy.changePrice(0.5);
toy.discription();

system("pause");
return 0;
}

 


原文地址:https://blog.csdn.net/m0_57667919/article/details/142445275

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!