实验四

task2.cpp

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

 

 task2.cpp

在draw()前加了virtual

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

 

 task3

battery.hpp

#include<iostream>
#include<string>
using namespace std;
class Battery {
private:
    int capacity;
public:
    Battery(int cap = 70) :capacity(cap)
    {
    }
    int get_capacity()
    {
        return capacity;
    }
};

car.hpp

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Car {
private:
    string maker;
    string model;
    int years;
    int odometers;
public:
    Car(string m, string mo, int y, int odo = 0)
    {
        maker = m;
        model = mo;
        years = y;
        odometers = odo;
    }
    void Info()
    {
        cout << "制造商:"<<setw(10)<<maker<<endl;
        cout << "型号:"<<setw(10)<<model<<endl;
        cout << "生产年份:"<<setw(10)<<years<<endl;
        cout << "当前行车里程数:"<<setw(10)<<odometers<<endl;
    }
    void update_odometers(int _odometers)
    {
        if ( _odometers >= odometers ) {
            odometers = _odometers;
        }
        else {
            cout << "ERROR!" << endl;
        }
    }
};

ElectricalCar.hpp

#include "battery.hpp"
#include "car.hpp"
#include <iostream>

using namespace std;

class ElectricCar :public Car {
private:
    Battery battery;
public:
    ElectricCar(string ma, string mo, int y, Battery bat=70, int odo = 0) :Car(ma, mo, y, odo), battery(bat)
    {}
    void Info()
    {
        Car::Info();
        cout << "capacity:" << battery.get_capacity() << "-kWh" << endl;
    }
};

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("mky", "mod", 2090);
    newcar.update_odometers(60000);
    cout << "\n--------newcar's info--------\n";
    newcar.Info();
}

 

 task4

pets.hpp

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

using namespace std;

class MachinePets {
private:
    string nickname;
public:
    MachinePets(const string s)
    {
        nickname = s;
    }
    virtual string talk()
    {
        return "lala";
    }
    string get_nickname()const
    {
        return 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-27 16:00  suanmeitang  阅读(17)  评论(3编辑  收藏  举报