实验四

#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 tyoe:"<<typeid(ptr).name()<<"\n";
    std::cout<<"RTTI type:"<<typeid(*ptr).name()<<"\n";
    ptr->draw();
}

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

实验四实验四

 

 

#include<iostream>
#include<string>
using namespace std;
class car
{
public:
    car(){}
    car(string maker,string model,int year):maker(maker),model(model),year(year),odometers(0){}
    void info();
    void update_odometers(int n);
private:
    string maker;
    int year;
    string model;
    int odometers;
};
void car::info()
{
    cout<<"maker:\t\t"<<maker<<endl;
    cout<<"model:\t\t"<<model<<endl;
    cout<<"year:\t\t"<<year<<endl;
    cout<<"odometers:\t"<<odometers<<endl;
}

void car::update_odometers(int n)
{
    if(n<odometers)
    {
        cout<<"error"<<endl;
    }
    else
    {
        odometers=n;
    }
}
#include<iostream>
class battery
{
public:
    battery()
    {
        capacity=70;
    }
    int get_capacity();
private:
    int capacity;
};
int battery::get_capacity()
{
    return capacity;
}
#include<iostream>
#include<string>
#include"battery.hpp"
#include"car.hpp"
class electriccar:public car
{
public:
    electriccar(){}
    electriccar(string maker,string model,int year):maker(maker),model(model),year(year),odometers(0),battery(){}
    void info()
    {
    cout<<"maker:\t\t"<<maker<<endl;
    cout<<"model:\t\t"<<model<<endl;
    cout<<"year:\t\t"<<year<<endl;
    cout<<"odometers:\t"<<odometers<<endl;
    cout<<"capacity:\t"<<battery.get_capacity()<<"-kWh"<<endl;
    }
    void update_odometers(int n)
    {
        if(n<odometers)
            cout<<"error"<<endl;
        else
            odometers=n;
    }
private:
    battery battery;
    string maker;
    int year;
    string model;
    int odometers;
};#include<iostream>
#include<string>
#include"battery.hpp"
#include"car.hpp"
class electriccar:public car
{
public:
    electriccar(){}
    electriccar(string maker,string model,int year):maker(maker),model(model),year(year),odometers(0),battery(){}
    void info()
    {
    cout<<"maker:\t\t"<<maker<<endl;
    cout<<"model:\t\t"<<model<<endl;
    cout<<"year:\t\t"<<year<<endl;
    cout<<"odometers:\t"<<odometers<<endl;
    cout<<"capacity:\t"<<battery.get_capacity()<<"-kWh"<<endl;
    }
    void update_odometers(int n)
    {
        if(n<odometers)
            cout<<"error"<<endl;
        else
            odometers=n;
    }
private:
    battery battery;
    string maker;
    int year;
    string model;
    int odometers;
};
#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();
}

实验四

 

#include<iostream>
#include<string>
using namespace std;
class machinepets
{
public:
    machinepets(){}
    machinepets(const string s):nickname(s){}
    virtual string talk()
    {
        string t;
        t="hi~";
        return t;
    }
    string get_nickname();
private:
    string nickname;
};
string machinepets::get_nickname()
{
    return nickname;
}
class petcats:public machinepets
{
public:
    petcats();
    petcats(const string s):machinepets(s){}
    string talk()
    {
        string t;
        t="miao wu~";
        return t;
    }
};
class petdogs:public machinepets
{
public:
    petdogs();
    petdogs(const string s):machinepets(s){}
    string talk()
    {
        string t;
        t="wang wang~";
        return t;
    }
};
#include<iostream>
#include"pets.hpp"
using namespace std;
void play(machinepets *ptr)
{
    cout<<ptr->get_nickname()<<" says "<<ptr->talk()<<endl;
}
int main()
{
    petcats cat("miku");
    petdogs dog("da huang");
    play(&cat);
    play(&dog);
}

实验四

 

上一篇:Python 日期模块的 datetime.date 类


下一篇:02 python都有哪些语言元素?