其他分享
首页 > 其他分享> > 实验四 继承

实验四 继承

作者:互联网

#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、

实验代码:

#include<iostream>
#include<string>

using namespace std;

class Car{
public:
    Car(string a="***",string b="***",int c=0,double d=0.0):maker(a),model(b),year(c),odometers(d){}
    void info();
    void update_odometers(int a);
private:
    string maker;
    string model;
    int year;
    int odometers;
};

void Car::info(){
    cout<<"maker:"<<maker<<endl;
    cout<<"model:"<<model<<endl;
    cout<<"year:"<<year<<endl;
    cout<<"odometers:"<<odometers<<endl;
}

void Car::update_odometers(int a){
    if(a<odometers)
    cout<<"Invalid input.New odometer must be larger than the last one.";
    else odometers=a;
}

class Battery{
public:
    Battery(string m="70kWh"):capacity(m){}
    string get_capacity(){return capacity;}
private:
    string capacity;
};

class ElectricCar:public Car
{
public:
    ElectricCar(string a="***",string b="***",int c=0,double d=0.0,string e="70kWh"):Car(a,b,c,d),battery(e){}
    void info(){
        Car::info();
        cout<<"battery:"<<battery.get_capacity()<<endl;
    }
private:
    Battery battery;
};
#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("Tesla", "model s", 2016);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

实验结果:

 Task4、

实验代码:

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

class MachinePets
{
public:
    MachinePets(const string s="momo"):nickname(s){}
    string get_nickname() const {
        return nickname;
    }
    virtual string talk(){}
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~"; }
};
#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);
}

实验结果:

 

标签:std,string,继承,实验,MachinePets,基类,include,public
来源: https://www.cnblogs.com/aishanglaonvren/p/15626994.html