实验4 继承

四、实验结论

1.实验任务1

验证性内容。

2.实验任务2

  • 程序源码,及,运行结果截图

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

  • 归纳总结:

    同名覆盖原则:若未明确指出,则通过派生类对象使用的是派生类中的同名成员。

    二元作用域分辨符:类名::成员 //数据成员 类目::成员名(参数表) //函数成员

    类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。在替代之后,派生类对象就可以作为基类的对象使用,但只能使用从基类继承的成员。

  • 对代码微调后,运行结果截图

    // definitation of Graph
    class Graph
    {
      public:
      // 声明时加了关键字virtual
      virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
    };
    

3.实验任务3

  • 完整源代码:

    Battery.hpp

    #ifndef BATTERY_H
    #define BATTERY_H
    
    #include <iostream>
    using namespace std;
    
    class Battery
    {
    public:
        Battery(int capacity0);
        int get_capacity();
        ~Battery() = default;
    
    private:
        int capacity;
    };
    
    Battery::Battery(int capacity0 = 70) : capacity(capacity0) {}
    
    int Battery::get_capacity()
    {
        return capacity;
    }
    #endif
    

    Car.hpp

    #ifndef CAR_H
    #define CAR_H
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class Car
    {
    public:
        Car(string maker0, string model0, int year0);
        void info() const;
        void update_odometers(int newOdometers);
        ~Car() = default;
    
    private:
        string maker;
        string model;
        int year;
        int odometers;
    };
    
    Car::Car(string maker0, string model0, int year0) : maker(maker0), model(model0), year(year0)
    {
        odometers = 0;
    }
    
    void Car::info() const
    {
        cout << left << setw(20) << "maker:" << maker << endl;
        cout << left << setw(20) << "model:" << model << endl;
        cout << left << setw(20) << "year:" << year << endl;
        cout << left << setw(20) << "odometers:" << odometers << endl;
    }
    
    void Car::update_odometers(int newOdometers)
    {
        if (newOdometers < odometers)
            cout << "update error" << endl;
        else
            odometers = newOdometers;
    }
    #endif
    

    ElectricCar.hpp

    #ifndef ELECTRIC_CAR_H
    #define ELECTRIC_CAR_H
    
    #include <iostream>
    #include "car.hpp"
    #include "battery.hpp"
    using namespace std;
    
    class ElectricCar : public Car
    {
    public:
        ElectricCar(string maker, string model, int year);
        void info();
        ~ElectricCar() = default;
    
    private:
        Battery battery;
    };
    
    ElectricCar::ElectricCar(string maker, string model, int year) : Car(maker, model, year), battery(70) {}
    
    void ElectricCar::info()
    {
        Car::info();
        cout << left << setw(20) << "capacity:" << battery.get_capacity() << "-kWh" << endl;
    }
    #endif
    

    task3.cpp

    #include <iostream>
    #include "electricCar.hpp"
    
    int main()
    {
      using namespace std;
    
      // test class of Car
      Car oldcar("Audi", "a7", 2021);
      cout << "--------oldcar's info--------" << endl;
      oldcar.update_odometers(30000);
      oldcar.info();
    
      cout << endl;
    
      // test class of ElectricCar
      ElectricCar newcar("Tesla", "model s", 2022);
      newcar.update_odometers(3000);
      cout << "\n--------newcar's info--------\n";
      newcar.info();
    }
    
  • 运行测试截图

4.实验任务4

  • 完整源代码:

    pets.hpp

    #ifndef PETS_H
    #define PETS_H
    
    #include <iostream>
    using namespace std;
    
    class MachinePets
    {
    
    public:
        MachinePets(const string nickname);
        virtual string talk();
        const string get_nickname();
        ~MachinePets() = default;
    
    private:
        string nickname;
    };
    
    MachinePets::MachinePets(const string nickname0) : nickname(nickname0) {}
    
    string MachinePets::talk() { return ""; }
    
    const string MachinePets::get_nickname()
    {
        return nickname;
    }
    
    class PetCats : public MachinePets
    {
    public:
        PetCats(const string s);
        string talk();
        ~PetCats() = default;
    };
    
    PetCats::PetCats(const string s) : MachinePets(s) {}
    
    string PetCats::talk()
    {
        return "miao wu~";
    }
    
    class PetDogs : public MachinePets
    {
    public:
        PetDogs(const string s);
        string talk();
        ~PetDogs() = default;
    };
    
    PetDogs::PetDogs(const string s) : MachinePets(s) {}
    
    string PetDogs::talk()
    {
        return "wang wang~";
    }
    #endif
    

    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);
    }
    
  • 运行测试截图

五、实验总结(选)

posted @ 2021-11-27 23:05  幸福^_^灿灿  阅读(34)  评论(1编辑  收藏  举报