• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

实验四

开发技术 开发技术 4小时前 1次浏览

任务二

#include <typeinfo>
#include <iostream>

using namespace std;


    //定义Graph类
    class Graph
    {
    public:
        void draw() //通过virtual关键词,改变指针ptr指向
        {
            cout << "Graph::draw():just as an interface" << endl;
        }
    };

    //定义Rectangle类,继承Graph类
    class Rectangle : public Graph
    {
    public:
        void draw()
        {
            cout << "Rectangle::draw():programes of draw a recetangle" << endl;
        }
    };

    //定义Circle类,继承Graph类
    class Circle : public Graph
    {
    public:
        void draw()
        {
            cout << "Circle::draw():programes of draw a circle" << endl;
        }
    };

    //定义fun()函数
    void fun(Graph *ptr)
    {
        cout << "pointer type: " << typeid(ptr).name() << endl;
        cout << "RTTI type: " << typeid(*ptr).name() << endl;
        ptr->draw();
    }

    int main()
    {
        Graph g1;
        Rectangle r1;
        Circle c1;


        g1.draw();
        r1.draw();
        c1.draw();

        cout << endl;

        r1.Graph::draw();
        c1.Graph::draw();

        cout << endl;

        fun(&g1);
        fun(&r1);
        fun(&c1);
    }
    

实验结果:

实验四

 

 微调后实验结果:

实验四

 

 

 

实验任务三

代码:

#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

class Car
{
private:
    string maker;
    string model;
    int year, odometers;

public:
    Car(string Maker, string Model, int Year)
    {
        maker = Maker;
        model = Model;
        year = Year;
        odometers = 0;
    }

    string get_maker()
    {
        return maker;
    }

    string get_model()
    {
        return model;
    }

    int get_year()
    {
        return year;
    }

    int get_odometers()
    {
        return odometers;
    }

    void info()
    {
        cout << left << setw(25) << "maker:" << get_maker() << endl;
        cout << left << setw(25) << "model:" << get_model() << endl;
        cout << left << setw(25) << "year:" << get_year() << endl;
        cout << left << setw(25) << "odometers:" << get_odometers() << endl;
        cout << endl;
    }

    void up_date_odometers(int new_odometers)
    {
        if (new_odometers >= odometers)
        {
            odometers = new_odometers;
        }
        else
        {
            cout << "Wrong data." << endl;
        }
    }
};

class Battery
{
private:
    int capacity;

public:
    Battery(int new_capacity = 70)
    {
        capacity = new_capacity;
    }

    int get_capacity()
    {
        return capacity;
    }
    
};

class ElectricCar:public Car
{
private:
    Battery battery;
public:
    ElectricCar(string Maker,string Model,int Year,int Battery=70):Car(Maker,Model,Year),battery(Battery){}
    void info()
    {
        cout << left << setw(25) << "maker:" << get_maker() << endl;
        cout << left << setw(25) << "model:" << get_model() << endl;
        cout << left << setw(25) << "year:" << get_year() << endl;
        cout << left << setw(25) << "odometers:" << get_odometers() << endl;
        cout << left << setw(25) << "capacity:" << battery.get_capacity() <<"-kWh" << endl;
        cout << endl;
    }
};

int main()
{
    //tset class of Car
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldercar's info--------" << endl;
    oldcar.up_date_odometers(25000);
    oldcar.info();

    //test class of ElectricCar
    ElectricCar newcar("Tesla","model s", 2016);
    cout << "--------newercar's info--------" << endl;
    newcar.up_date_odometers(35000);
    newcar.info();
}

实验结果:

实验四

 

 

 

实验任务四

代码:

#include <string>
#include <iostream>
#include <map>

using namespace std;

class MachinePets
{
private:
    string nickname;

public:
    MachinePets(const string name) :nickname(name) {};

    string get_nickname()
    {
        return nickname;
    }

    virtual string talk() 
    {
        return "123456";//派生函数talk输出与该返回值无关
    }
};

class PetCats:public MachinePets
{
public:
    PetCats(string name) :MachinePets(name) {};
    string talk()
    {
        return "miao miao";
    }

};

class PetDogs :public MachinePets
{
public:
    PetDogs(string name) :MachinePets(name) {};
    string talk()
    {
        return "wang wang";
    }
};

void play(MachinePets *ptr)
{
    cout << ptr->get_nickname() << " says " << ptr->talk() << endl;
}

int main()
{
    PetCats cat("dog");
    PetDogs dog("cat");

    play(&cat);
    play(&dog);
}

实验结果:

实验四

 


程序员灯塔
转载请注明原文链接:实验四
喜欢 (0)