项目练习之类的使用 (第一卷项目十的练习)

一 项目练习一

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

//-------------------  main.cpp -------------------
#include <iostream>
#include "Toy.h"

int main()
{
    Toy toy("变形金刚", 1600, "China");
    cout << toy.getName() << " 价格:" << toy.getPrice() << " 产地:" << toy.getOrigin() << endl;
    cout << "全场五折促销:" << endl;
    toy.setDiscount(0.5);
    cout << toy.getName() << " 价格:" << toy.getPrice() << " 产地:" << toy.getOrigin() << endl;

    return 0;
}

//---------------  Toy 类 ----------------
/* Toy.h */
#pragma once
#include <string>

using namespace std;

class Toy
{
public:
    Toy();
    Toy(string name, int price, string origin, double discount = 1);
    ~Toy();

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

    void setDiscount(double discount);
private:
    string name;       // 名称
    int price;         // 价格
    string origin;     // 产地
    double discount;   // 折扣
};

/* Toy.cpp */
#include "Toy.h"

Toy::Toy()
{
    name = "";
    price = 0;
    origin = "";
    discount = 1;
}

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

Toy::~Toy()
{
}

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

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

string Toy::getOrigin() const
{
    return origin;
}

void Toy::setDiscount(double discount)
{
    this->discount = discount;
}

image

二 项目练习二

定义一个类,来表示某模拟养成游戏中人物:
每个人物, 有昵称,年龄,性别, 配偶, 朋友,
支持的活动有:结婚,离婚, 交友,断交,询问昵称,询问性别,询问年龄, 简介等。

//-------------------  main.cpp  ------------------
#include "Human.h"
#include <iostream>

using namespace std;

int main()
{
    Human lhc("令狐冲", 25, MAN);
    Human ryy("任盈盈", 26, WOMAN);
    Human tbg("田伯光", 30, MAN);
    Human yls("岳灵珊", 20, WOMAN);
    Human cx("冲虚道长", 55, MAN);

    lhc.marry(&yls);
    Human* who = lhc.getLover();
    cout << lhc.getName() << "的配偶是:" << lhc.getLover()->getName() << endl;
    cout << who->getName() << "的配偶是:" << who->getLover()->getName() << endl;

    cout << endl << "-------离婚-------" << endl;
    lhc.divorce();
    if (lhc.getLover() == NULL)
    {
        cout << lhc.getName() << "离婚成功,单身" << endl;
    }
    else
    {
        cout << lhc.getName() << "离婚失败,配偶是:" << lhc.getLover()->getName() << endl;
    }

    lhc.addFriend(&tbg);
    lhc.addFriend(&cx);
    lhc.addFriend(&ryy);
    vector<Human*> friends = lhc.getFriends();
    cout << lhc.getName() << "的朋友们有:";
    for (int i = 0; i < friends.size(); i++)
    {
        cout << friends[i]->getName() << " ";
    }

    cout << endl << endl << "----------删朋友-----------" << endl;
    lhc.delFriend(&cx);
    friends = lhc.getFriends();
    cout << lhc.getName() << "的朋友们有:";
    for (int i = 0; i < friends.size(); i++)
    {
        cout << friends[i]->getName() << " ";
    }

    cout << endl;
    return 0;
}

//--------------------  Human 类  --------------------
/* Human.h */
#pragma once
#include <string>
#include <vector>
using namespace std;

typedef enum gender //性别
{
    MAN,    // 男人
    WOMAN   // 女人
}gender_t;


class Human
{
public:
    Human();
    Human(string name, int age, gender_t gender, Human* lover = NULL);
    ~Human();

    string getName() const;
    int getAge() const;
    gender_t getGender() const;
    Human* getLover() const;
    vector<Human*> getFriends() const;
    string description() const;

    void marry(Human* lover);
    void divorce();
    void addFriend(Human* other);
    void delFriend(Human* other);

private:
    string name;               // 姓名
    int age;                   // 年龄
    gender_t gender;           // 性别
    Human* lover;              // 配偶,这里是聚合关系
    vector<Human*> friends;    // 朋友
};


/* Human.cpp */
#include "Human.h"
#include <sstream>
Human::Human()
{
    name = "";
    age = 0;
    gender = MAN;
    lover = NULL;
}

Human::Human(string name, int age, gender_t gender, Human* lover)
{
    this->name = name;
    this->age = age;
    this->gender = gender;
    this->lover = lover;
}

Human::~Human()
{
}

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

int Human::getAge() const
{
    return age;
}

gender_t Human::getGender() const
{
    return gender;
}

Human* Human::getLover() const
{
    return lover;
}

vector<Human*> Human::getFriends() const
{
    return friends;
}

string Human::description() const
{
    stringstream des;
    des << name << " 年龄:" << age << " 性别:" << (gender == MAN ? "男" : "女");
    return des.str();
}

void Human::marry(Human* lover)
{
    if (gender == lover->gender)
    {
        return;
    }
    this->lover = lover;
    lover->lover = this;
}

void Human::divorce()
{
    if (lover == NULL)
    {
        return;
    }
    lover->lover = NULL;
    this->lover = NULL;
}

void Human::addFriend(Human* other)
{
    friends.push_back(other);
}

void Human::delFriend(Human* other)
{
    for (auto it = friends.begin(); it != friends.end();)
    {
        if (*it == other)
        {
            it = friends.erase(it);
        }
        else 
        {
            it++;
        }
    }
}

image

三 项目练习三

  1. 定义一个或多个类,来描述以下需求:
    汽车,有多个轮胎,一个发动机,品牌,型号, 价格, 行驶里程。
    轮胎,有品牌,气压。
    发动机,有品牌,型号。
//---------------  main.cpp  --------------
#include <iostream>
#include "Car.h"

int main()
{
    Car car("宝马", 7, 300000,"米其林", 2.5, "宝马",3.5);

    cout << car.description() << endl;

    return 0;
}

//---------------  Engine 类  -------------------
/* Engine.h */
#pragma once
#include <string>
using namespace std;

// 发动机类
class Engine
{
public:
    Engine();
    Engine(const string& brand, float version);
    ~Engine();

    string description() const;
private:
    string brand;     // 品牌
    float version;    // 型号
};

/* Engine.cpp */
#include "Engine.h"
#include <sstream>
#include <iostream>
Engine::Engine()
{
    brand = "";
    version = 0;
}

Engine::Engine(const string& brand, float version)
{
    this->brand = brand;
    this->version = version;
}

Engine::~Engine()
{
    cout << __FUNCTION__ << endl;
}

string Engine::description() const
{
    stringstream ret;
    ret << "发动机(品牌:" << brand << " 型号:" << version << ")";
    return ret.str();
}

//---------------  Tire 类  -------------------
/* Tire.h */
#pragma once
#include <string>

using namespace std;

// 轮胎类
class Tire
{
public:
    Tire(const string& brand = "米其林", float pressure = 2.5);
    ~Tire();

    string getBrand() const;
    float getPressure() const;
    string description() const;
private:
    string brand;     // 品牌
    float pressure;   // 胎压
};

/* Tire.cpp */
#include "Tire.h"
#include <sstream>
#include <iostream>

Tire::Tire(const string& brand, float pressure)
{
    this->brand = brand;
    this->pressure = pressure;
}

Tire::~Tire()
{
    cout << __FUNCTION__ << endl;
}

string Tire::getBrand() const
{
    return brand;
}

float Tire::getPressure() const
{
    return pressure;
}

string Tire::description() const
{
    stringstream ret;
    ret << "轮胎(品牌:" << brand << " 胎压:" << pressure << ")";
    return ret.str();
}

//---------------  Car 类  -------------------
/* Car.h */
#pragma once
#include <string>
#include "Engine.h"
#include "Tire.h"
using namespace std;

class Car
{
public:
    Car();
    Car(const string& carBrand, float carVersion, int price,
        const string& tireBrand, float tirePressure,
        const string& engineBrand, float engineVersion);
    ~Car();

    Engine getEngine() const;
    const Tire* getTires(int i) const;  //参数i:第几个轮胎
    string getBrand() const;
    float getVersion() const;
    int getPrice() const;
    int getMileage() const;
    string description() const;

private:
    Tire tires[4];    // 轮胎,这里是组合关系
    Engine engine;    // 发动机,这里是组合关系
    string brand;     // 汽车品牌 
    float version;    // 汽车型号
    int price;        // 价格
    int mileage;      // 行驶里程

};
/* Car.cpp */
#include "Car.h"
#include <iostream>
#include <sstream>

Car::Car()
{
}

Car::Car(const string& carBrand, float carVersion, int price, 
    const string& tireBrand, float tirePressure, 
    const string& engineBrand, float engineVersion)
    :tires{ {tireBrand,tirePressure}, {tireBrand,tirePressure}, {tireBrand,tirePressure}, {tireBrand,tirePressure}},
    engine(engineBrand, engineVersion)
{
    this->brand = carBrand;
    this->version = carVersion;
    this->price = price;
    this->mileage = 0;
}

Car::~Car()
{
    cout << __FUNCTION__ << endl;
}

Engine Car::getEngine() const
{
    return engine;
}

const Tire* Car::getTires(int i) const
{
    return &tires[i];  //这里是常函数,所以返回值类型也应该是常指针
}

string Car::getBrand() const
{
    return brand;
}

float Car::getVersion() const
{
    return version;
}

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

int Car::getMileage() const
{
    return mileage;
}

string Car::description() const
{
    stringstream ret;
    ret << "汽车(品牌:" << brand << " 型号:" << version << " 价格:" << price << " 行驶里程:" << mileage << ")" << endl
        << tires[0].description() << endl << engine.description() << endl;
    return ret.str();
}

image

posted @ 2022-05-01 18:02  荒年、  阅读(25)  评论(0编辑  收藏  举报