ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验4

2021-11-26 17:02:31  阅读:126  来源: 互联网

标签:draw string int 实验 MachinePets include public


#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有