实验四

实验任务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);
}

截图;

 修改后代码:

#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:

car.hpp

#include<iostream>
#include<string.h>
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;
            
        
        }
        void update_odometers(int a){
            if(a<odometers)cout<<"the new odometers is wrong."<<endl;
            else odometers=a;
            
        }
        
        
    private:
        string maker;
        string model;
        int year;
        int odometers;
        
        
            
};

battery.hpp

#include<iostream>
using namespace std;

class Battery{
    public:
        Battery(int a=70){
            capacity=a;
            
        }
        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 e=70,int d=0):Car(a,b,c,d),battery(e){}
        void info(){
            Car::info();
            cout<<"capacity:"<<battery.get_capacity()<<"-KWH"<<endl;

        }
        
    private:
        Battery battery;
};

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

截图;

 

实验任务4

pets.hpp

#include<iostream>
#include<string.h>
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~";}

};

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-26 12:36  潘俊杰  阅读(3)  评论(0编辑  收藏  举报