其他分享
首页 > 其他分享> > 实验4

实验4

作者:互联网

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

 

 

battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP
#include<iostream>
class battery {
public:
    battery(int m = 70):capacity(m)
    {

    }
    int get_capacity();
private:
    int capacity;
};
int battery::get_capacity()
{
    return capacity;
}
#endif

car.hpp

#ifndef CAR_HPP
#define CAR_HPP
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class car {
public:
    car(string m,string n,int y,int o = 0) :maker(m), model(n), year(y), odometers(o)
    {

    }
    void info();
    void update_odometers(int new_odometers=0);
private:
    string maker;
    string model;
    int year;
    int odometers;
};
void car::info()
{
    cout << left << setw(30) << "maker:" << maker << endl;
    cout << left << setw(30) << "model:" << model << endl;
    cout << left << setw(30) << "year:" << year << endl;
    cout << left << setw(30) << "odometers:" << odometers << endl;
}
void car::update_odometers(int new_odometers)
{
    if (new_odometers < odometers)
    {
        cout << "invalid input.";
    }
    odometers = new_odometers;
}
#endif

electriccar.hpp

 

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#include"car.hpp"
#include"battery.hpp"
class electriccar :public car{
public:
    electriccar(string m, string n, int y, int o=0,int b=70) :car(m,n,y,o),Battery(b)
    {

    }
    void info();
private:
    battery Battery;
};
void electriccar::info()
{
    car::info();
    cout << left << setw(30) << "capacity:" << Battery.get_capacity() << "-KWh" << endl;
}

#endif

main.cpp

#include <iostream>
#include "electriccar.hpp"

int main()
{
    using namespace std;

    // test class of Car
    car oldcar("Benz", "E3", 2012);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    electriccar newcar("Tesla", "model x", 2020);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

 

 

MachinePets.hpp

#ifndef MACHINEPETS_HPP
#define MACHINEPETS_HPP
#include<iostream>
using namespace std;
class MachinePets {
public:
    MachinePets(const string s) :nickname(s)
    {

    }
    string get_nickname()
    {
        return nickname;
    }
    virtual string talk()
    {
        return "jiaosheng";
    }
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~";
    }
};
#endif

main.hpp

#include <iostream>
#include "MachinePets.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);
}

 

标签:draw,string,int,实验,MachinePets,include,public
来源: https://www.cnblogs.com/yyd2002/p/15608260.html