ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验四 继承

2021-11-24 20:31:06  阅读:136  来源: 互联网

标签:string 继承 Car Battery int 实验 MachinePets include


实验任务三

1、Battery类:

#include <iostream>
#include <string>
using namespace std;

class Battery
{
private:
    int capacity;

public:
    Battery(int capacity);
    ~Battery();
    int get_capacity();
};

Battery::Battery(int capacity = 70)
{
    this->capacity = capacity;
}

Battery::~Battery()
{
}

int Battery::get_capacity()
{
    return this->capacity;
}

2、Car类

#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    string maker;  //制造商
    string model;  //型号
    int year;      //生产年份
    int odometers; //行车里程数
public:
    Car(string maker, string model, int year);
    ~Car();
    void info();
    void update_odometers(int newOdometers);
};

Car::Car(string maker, string model, int year)
{
    this->maker = maker;
    this->model = model;
    this->year = year;
    this->odometers = 0;
}

Car::~Car()
{
}
void Car::info()
{
    cout << "maker:\t\t" << this->maker << endl;
    cout << "model:\t\t" << this->model << endl;
    cout << "year:\t\t" << this->year << endl;
    cout << "odometers:\t" << this->odometers << endl;
}
void Car::update_odometers(int newOdometers)
{
    if (newOdometers < this->odometers)
        cout << "更新里程数有误" << endl;
    else
        this->odometers = newOdometers;
}

3、ElectricCar类

#include <iostream>
#include "car.hpp"
#include "battery.hpp"
using namespace std;

class ElectricCar : public Car
{
private:
    Battery battery;

public:
    ElectricCar(string maker, string model, int year);
    void info();
};

ElectricCar::ElectricCar(string maker, string model, int year) : Car(maker, model, year)
{
    Battery battery(70);
    this->battery = battery;
}

void ElectricCar::info()
{
    Car::info();
    cout << "capacity:\t" << this->battery.get_capacity() << "-kWh" << endl;
}
  • 测试代码如下:
#include <iostream>
#include "electricCar.hpp"

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}
  • 测试结果:

实验任务四

1、pets.hpp

#include <iostream>
#include <string>
using namespace std;

class MachinePets
{
private:
    string nickname;

public:
    MachinePets(const string nickname);
    ~MachinePets();
    virtual string talk();
    const string get_nickname();
};

MachinePets::MachinePets(const string nickname) : nickname(nickname) {}

MachinePets::~MachinePets() {}

string MachinePets::talk() { return ""; }

const string MachinePets::get_nickname()
{
    return nickname;
}

class PetCats : public MachinePets
{
public:
    PetCats(const string s);
    ~PetCats();
    string talk();
};

PetCats::PetCats(const string s) : MachinePets(s) {}

PetCats::~PetCats() {}

string PetCats::talk()
{
    return "miao wu~";
}

class PetDogs : public MachinePets
{
public:
    PetDogs(const string s);
    ~PetDogs();
    string talk();
};

PetDogs::PetDogs(const string s) : MachinePets(s) {}

PetDogs::~PetDogs() {}

string PetDogs::talk()
{
    return "wang wang~";
}
  • 测试代码如下:
#include <iostream>
#include "pets.hpp"

void play(MachinePets *ptr)
{
    std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl;
}

int main()
{
    PetCats cat("miku");
    PetDogs dog("da huang");

    play(&cat);
    play(&dog);
}
  • 测试结果:

标签:string,继承,Car,Battery,int,实验,MachinePets,include
来源: https://www.cnblogs.com/suu-8/p/15599905.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有