实验四 继承

 

实验二

task2.cpp

#include <iostream>
#include <typeinfo>

// definitation of Graph
class Graph
{
public:
    virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
};


// definition of Rectangle, derived from Graph
class Rectangle : public Graph
{
public:
     void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
};


// definition of Circle, derived from Graph
class Circle : public Graph
{
public:
    void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
};


// definitaion of fun(): as a call interface
void fun(Graph *ptr)
{
    std::cout << "pointer type: " << typeid(ptr).name() << "\n";
    std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
    ptr -> draw();
}

// test 
int main()
{
    Graph g1;
    Rectangle r1;
    Circle c1;

    // call by object name
    g1.draw();
    r1.draw();
    c1.draw();

    std::cout << "\n";

    // call by object name, and using the scope resolution operator::
    r1.Graph::draw();
    c1.Graph::draw();

    std::cout << "\n";

    // call by pointer to Base class
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

实验三

task3.cpp

#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();

    system("pause");
}

car.hpp

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

class Car
{
public:
    Car(string ma, string mo, int y ,int o = 0):
        maker(ma),model(mo),year(y),odo(o){}
    void info(){
        cout << "maker:\t\t" << maker << endl;
        cout << "model:\t\t" << model << endl;
        cout << "year: \t\t" << year << endl;
        cout << "odometers:\t" << odo << endl;
    }
    void update_odometers(int o){
        if(o < odo)
            cout << "---wearning---" << endl;
        else
            odo = o;
    }
private:
    string maker, model;
    int year, odo;
};

battery.hpp

#include<iostream>
using namespace std;

class Battery
{
public:
    Battery(int ca = 70):capacity(ca){}
    int get_capacity(){
        return capacity;
    }
private:
    int capacity;

};

electricCar.hpp

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

class ElectricCar:public Car
{
public:
    ElectricCar(string a, string b,int c,int d = 0, int e = 70):
        Car(a,b,c,d), battery(e){}
    void info(){
        Car::info();
        cout << "capacity: \t" << battery.get_capacity() <<"-kwh"<<endl;
    }

private:
    Battery battery;
};

实验四

task4.cpp

#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);

    system("pause");
}

pets.hpp

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

class MachinePets
{
public:
    MachinePets(const string s):nickname(s){}
    ~MachinePets() = default;
    string get_nickname() const {
        return nickname;
    }
    virtual string talk(){}
private:
    string nickname;
};

class PetCats:public MachinePets
{
public:
    PetCats(const string name):MachinePets(name){}
    string talk(){
        return "miao wu~";
    }
};

class PetDogs:public MachinePets
{
public:
    PetDogs(const string name):MachinePets(name){}
    string talk(){
        return "wang wang~";
    }
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值