zoukankan      html  css  js  c++  java
  • 实验4 继承

    实验任务2

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

     

     

    同名覆盖原则:派生类和继承类函数完全相同时,通过派生类对象只访问派生类中的函数。

    二元作用域分辨符:可以指定想要访问某个类的函数。

    类型兼容原则:派生类对象可以作为基类的对象使用。

    实验任务3

    battery.hpp

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

    car.hpp

    #ifndef CAR_HPP
    #define CAR_HPP
    #include<iostream>
    #include<string>
    using namespace std;
    class Car{
        public:
            Car(string m,string mo,int y,int od=0):maker(m),model(mo),year(y),odometres(od){}
            void info();
            void update_odometers(int odo);
            
            
        private:
            string maker;
            string model;
            int year;
            int odometres;
            
    };
    
    void Car::info(){ 
        cout<<"maker:        "<<maker<<endl;
        cout<<"model:        "<<model<<endl;
        cout<<"year:         "<<year<<endl;
        cout<<"odometres:    "<<odometres<<endl;
    }
    void Car::update_odometers(int odo){
        if(odo<odometres)
        {
            cout<<"Error updating value\n";
        }
        else
        {
            odometres=odo;
        }
        
    }
    #endif

    electricCar.hpp

    #ifndef ELECTRICCAR_HPP
    #define ELECTRICCAR_HPP
    #include "car.hpp"
    #include "battery.hpp"
    #include<iostream>
    #include<string>
    using namespace std;
    
    class ElectricCar:public Car
    {
        public:
            ElectricCar(string m,string mo,int y,int b=70):Car(m,mo,y),battery(b){}
            void info();
        private:
            Battery battery;
    };
    void ElectricCar::info(){
        Car::info();
        cout<<"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("Hongqi", "h6", 2020);
        cout << "--------oldcar's info--------" << endl;
        oldcar.update_odometers(25009);
        oldcar.info();
    
        cout << endl;
    
        // test class of ElectricCar
        ElectricCar newcar("Rongwei", "i5", 2021);
        newcar.update_odometers(1000);
        cout << "\n--------newcar's info--------\n";
        newcar.info();
    }

     

    实验任务4

     pets.hpp

    #ifndef PETS_HPP
    #define PETS_HPP
    #include<iostream>
    #include<string>
    using namespace std;
    class MachinePets{
        public:
            MachinePets(const string s):nickname(s){}
            virtual string talk(){}
            string get_nickname(){
                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~";
            }
    };
    #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);
    }

     

     

  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/qiansen/p/15611973.html
Copyright © 2011-2022 走看看