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