创建一个类, 用来表示“玩具”文具, 有以下数据:名称,价格,产地。在使用中,需要获取它的名称, 价格, 产地。注意:根据自己当前的优惠情况,有一个对外的价格
?
#pragma once
#include <string>
using namespace std;
class Toys{
public:
Toys();
~Toys();
Toys(string name, float price, string origin);
//定义成员函数的实现方法
string getName() const;
float getPrice() const;
string getOrigin()const;
void setGiscount(float num);
private:
string name;
float price;
string origin;
float discount = 1.0;
};
#include "Toys.h"
Toys::Toys() {
}
Toys::~Toys() {
}
Toys::Toys(string name, float price, string origin) {
this->name = name;
this->price = price;
this->origin = origin;
}
string Toys::getName() const{
return name;
}
float Toys::getPrice() const {
return price*discount;
}
string Toys::getOrigin()const {
return origin;
}
void Toys::setGiscount(float num) {
this->discount = num;
}
#include <iostream>
#include "Toys.h"
using namespace std;
int main(void) {
float num1 = 0.0;
Toys h1("变形金刚",10000,"China");
cout << "名字: " << h1.getName() << "价格: " << h1.getPrice()
<< "[made in " << h1.getOrigin() << " ]" << endl;
cout << "请输入折扣力度:";
cin >> num1;
h1.setGiscount(num1);
cout << "名字: " << h1.getName() << "价格: " << h1.getPrice()
<< "[made in " << h1.getOrigin() << " ]" << endl;
}
定义一个类,来表示某模拟养成游戏中人物:每个人物, 有昵称,年龄,性别, 配偶, 朋友 ,支持的活动有:结婚,离婚 , 交友,断交,询问昵称,询问性别,询问年龄 , 简介等。
?
.h
#pragma once
#include <string>
#include <vector>
using namespace std;
enum gender
{
MAN,//男人
WOMAN//女人
};
class Human{
public:
Human();
~Human();
Human(string name, int age, gender sex);
string getName()const;
int getAge()const;
gender getSex()const;
Human* getLover() const;
vector<Human*> getFriend()const;
string describe()const;
void marry(Human& other);
void devioce() ;
void addFriend(Human& other);
void delFriend(Human& other);
private:
string name;
int age;
gender sex;
Human* lover;//当你调用结束了,你的配偶不会一起消亡
vector<Human*> friends;
};
?.cpp
#include "Human.h"
#include <sstream>
Human::Human()
{
}
Human::~Human()
{
}
Human::Human(string name, int age, gender sex){
this->name = name;
this->age = age;
this->sex = sex;
}
string Human::getName() const
{
return name;
}
int Human::getAge() const
{
return age;
}
gender Human::getSex() const
{
return sex;
}
Human* Human::getLover() const
{
return lover;
}
vector<Human*> Human::getFriend() const
{
return friends;
}
string Human::describe() const
{
stringstream ret;//转字符串
ret << name << "-" << age << "-" << (sex==MAN?"男":"女") << endl;
return ret.str();
}
void Human::marry(Human& other)
{
//设置性别相同不能结婚
if (this->sex ==other.sex)
{
return;
}
this->lover = &other;
other.lover = this;
}
void Human::devioce()
{
if (this->lover ==NULL)
{
return;
}
lover->lover = NULL;
lover = NULL;
}
void Human::addFriend(Human& other)
{
this->friends.push_back(&other);
}
void Human::delFriend(Human& other)
{
//auto:自动匹配数据的数据类型
//friends.begin()是一个特殊的指针,相当于一个迭代器,指向第一个成员
//并不是 &friends[0]
//friends.end()是一个特殊的指针,指向最后一个成员的下一个位置
for (auto i = friends.begin(); i !=friends.end();)
{
if (*i == &other) {
//erase(i)清楚这个成员的数据,并指向下一个成员的位置
i = friends.erase(i);//返回下一个成员并设置为迭代器,
}
else {
i++;
}
}
/*//这种写法是错误的
for (auto i = friends.begin(); i !=friends.end();)
{
if (*i == &other) {
friends.erase(i);
}
}
*/
}
主函数:
#include "Human.h"
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
Human h("干", 23, MAN);
Human h1("货", 22, WOMAN);
Human h2("满", 20, WOMAN);
Human h3("当", 29, MAN);
Human h4("的", 18, WOMAN);
h.marry(h1);
cout << h.getName() << "的配偶是:" << h.getLover()->describe() << endl;
cout << h1.getName() << "的配偶是:" << h1.getLover()->describe() << endl;
cout << h.getName() << ": 离婚了" << endl;
h.devioce();
if (h1.getLover() == NULL) {
cout << h1.getName() << ": 是单身哦!" << endl;
}
h.addFriend(h1);
h.addFriend(h2);
h.addFriend(h3);
h.addFriend(h4);
vector<Human*> friends = h.getFriend();
for (int i = 0; i < friends.size(); i++){
cout << h.getName() << "的朋友是" << friends[i]->describe() << endl;
}
h.delFriend(h3);
cout << h.getName() << "删除了好友" << h3.getName() << endl;
friends = h.getFriend();
for (int i = 0; i < friends.size(); i++) {
cout << h.getName() << "的朋友是" << friends[i]->describe() << endl;
}
system("pause");
return 0;
}