【实验4】

实验四

实验任务二

点击查看代码
#include <iostream>
#include <typeinfo>

// definitation of Graph
class Graph
{
public:
    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);
}

image

添加virtual后

image

对于同名的函数,继承类将覆盖基类的函数
对于继承的类,可以通过继承类::函数的方式去访问被隐藏的函数
在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
在增添了virtual后,fun()函数输出发生了变化。通过重写虚拟函数来实现对基类虚拟函数的覆盖。

实验任务三

task3.cpp

点击查看代码
#include<iostream>
#include"ElectricCar.hpp"

int main()
{
    using namespace std;
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25000);
    oldcar.info();

    cout << endl;
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

ElectricCar.hpp

点击查看代码
#include<iostream>
#include"battery.hpp"
#include"car.hpp"

 using namespace std;

 class ElectricCar : public Car {
     public:
            ElectricCar(string ma, string mo, int y, int od = 0, int ba = 70) : Car(ma, mo, y, od), battery(ba) {};
             void info() {
                     Car::info();
                     cout << "capacity:\t" << battery.get_capacity() << "-KWh" << endl;
        
    }
     private:
             Battery battery;
    
};

car.hpp

点击查看代码
#include<iostream>
#include<string>
using namespace std;

class Car {
public:
    Car(string ma, string mo, int y, int od = 0) : maker(ma), model(mo), year(y), odometers(od) {};
    void info() {
        cout << "maker\t\t" << maker << endl;
        cout << "model\t\t" << model << endl;
        cout << "year:\t\t" << year << endl;
        cout << "odometerd:\t" << odometers << endl;
    }
    void update_odometers(int od) {
        if (od < odometers)
            cout << "The update_odometers is wrong" << endl;
        else
            odometers = od;
    }
private:
    string maker;
    string model;
    int year;
    int odometers;
};

battery.hpp

点击查看代码
#include<iostream>
using namespace std;

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

        return capacity;
    }
private:
    int capacity;
};

image

实验任务四

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

Pets.hpp

点击查看代码
#include<iostream>
#include<string>
using namespace std;
class MachinePets
{
public:MachinePets(const string s): nickname(s) {};
      virtual string talk() { return 0; };
      string get_nickname() const
      {
          return nickname;
      };
private: string nickname;
};
class PetCats :public MachinePets
{
public:PetCats(const string s) : MachinePets(s) {};
      string talk()
      {
          return "miao wu~";
      }
};
class PetDogs : public MachinePets
{
public:PetDogs(const string s) : MachinePets(s) {};
      string talk()
      {
          return "wang wang~";
      }
};

image

posted @ 2021-11-30 21:14  katachip  阅读(39)  评论(1编辑  收藏  举报