实验4继承

实验二:

task2.cpp:

#include <iostream>
#include <typeinfo>

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

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

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

void fun(Graph *ptr)
{
    std::cout<<"pointer type: "<<typeid(ptr).name()<<"\n";
    std::cout<<"RTTI type: "<<typeid(*ptr).name()<<"\n";
    ptr->draw();
}

int main()
{
    Graph g1;
    Rectangle r1;
    circle c1;
    std::cout<<"\n";
    r1.Graph::draw();
    c1.Graph::draw();
    std::cout<<"\n";
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

实验结果:

 

 task2.cpp:

#include <iostream>
#include <typeinfo>

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

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

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

void fun(Graph *ptr)
{
    std::cout<<"pointer type: "<<typeid(ptr).name()<<"\n";
    std::cout<<"RTTI type: "<<typeid(*ptr).name()<<"\n";
    ptr->draw();
}

int main()
{
    Graph g1;
    Rectangle r1;
    circle c1;
    std::cout<<"\n";
    r1.Graph::draw();
    c1.Graph::draw();
    std::cout<<"\n";
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

实验结果:

 

实验三: 

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

battery.hpp:

#include <iostream>
class Battery{
    public:
        Battery(int a=70):capacity(a){}
        ~Battery(){}
        int get_capacity(){
            return capacity;
        }
    private:
        int capacity;
};

car.hpp:

#include <iostream>
#include <string>
using namespace std;
class Car{
    public:
        Car(string a,string b,int c,int d=0): maker(a),model(b),year(c),odometers(d){}
        void info(){
            cout<<"maker        "<<maker<<endl;
            cout<<"model        "<<model<<endl;
            cout<<"year         "<<year<<endl;
            cout<<"odometers    "<<odometers<<endl;
        }
        ~Car(){}
    void update_odometers(int e){
        if(e<odometers)
        {
            cout<<"wrong"<<endl;
        }
        else
        odometers=e;
    }
    protected:
        string maker;
        string model;
        int year;
        int odometers;
};

electricCar.hpp:

#include <iostream>
#include "battery.hpp"
#include "car.hpp"
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(){
            cout<<"maker        "<<maker<<endl;
            cout<<"model        "<<model<<endl;
            cout<<"year         "<<year<<endl;
            cout<<"odometers    "<<odometers<<endl;
            cout<<"battery      "<<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);
}

pets.hpp:

#include <iostream>
#include <string>
using namespace std;
class MachinePets{
    public:
        MachinePets(const string s): nickname(s){
        }
        string get_nickname()const{
          return nickname;
        }
        virtual string talk(){
            string a;
            a="huhu";
            return a;
        }
    protected:
        string nickname;
};
class PetCats: public MachinePets{
    public:
        PetCats(const string s) : MachinePets(s){
        }
        string talk(){
            string a;
            a="miao wu~";
            return a;
        }
};
class PetDogs:public MachinePets{
    public:
        PetDogs(const string s): MachinePets(s){
        }
        string talk(){
            string a;
            a="wang wang~";
            return a;
        }
};

实验结果:

 

posted @ 2021-11-25 13:21  叶金萌  阅读(27)  评论(3)    收藏  举报