实验4 继承

实验任务2

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

运行结果截图:

 归纳总结:同名覆盖原则:当派生类与基类中有相同成员时:若未强行指名,则通过派生类对象使用的是派生类的同名成员;如果要通过派生类的对象访问基类被覆盖的同名成员,需要加 对象名.基类名::同名成员 来限定。

         二元作用域运算符在不同的地方有着不同的含义:如果成员函数中定义了和类域中的变量同名的变量,则类域中的变量被块作用域中的变量隐藏。这时可以在变量名前面加类名和作用域运算符(::)访问这种隐藏变量;定义类中声明的成员函数时可以用类名和二元作用于运算符::,将每个成员函数“绑定到”声明成员函数与数据成员的类定义上。

        类型兼容规则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。这样,公有派生类实际就具备了基类的所有功能,凡是基类能解决的问题,公有派生类都可以解决。

加上关键字virtual后的运行结果截图:

 

实验任务3

Battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP
#include<iostream>
using namespace std;
class Battery{
public:
    Battery(int n=70):capacity(n) {}
    int get_capacity() const {return capacity;}
private:
    int capacity;
};
#endif

Car.hpp

#ifndef CAR_HPP
#define CAR_HPP
#include<iostream>
#include<string>
using namespace std;
class Car{
public:
    Car(string a,string b,int c);
    void info();
    void update_odometers(int m);
private:
    string maker,model;
    int year,odometers;
};
Car::Car(string a,string b,int c):maker(a),model(b),year(c),odometers(0) {}
void Car::info()
{
    cout<<"maker:             "<<maker<<endl;
    cout<<"model:             "<<model<<endl;
    cout<<"year:              "<<year<<endl;
    cout<<"odometers:         "<<odometers<<endl;
}
void Car::update_odometers(int m)
{
    if(m<odometers)
    cout<<"警告!更新数值有误!"<<endl;
    else
    odometers=m;
}
#endif

ElectricCar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include"battery.hpp"
#include"car.hpp"
#include<iostream>
#include<string>
using namespace std;
class ElectricCar: public Car
{
    public:
        ElectricCar(string a,string b,int c,int d=70);
        void info();
    private:
        Battery battery;
};
ElectricCar::ElectricCar(string a,string b,int c,int d):Car(a,b,c),battery(d) {}
void ElectricCar::info()
{
    Car::info();
    cout<<"capacity:          "<<battery.get_capacity()<<"-kWh"<<endl;
}
#endif

修改数据后的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("Benz", "GLA SUV", 2020, 120);
newcar.update_odometers(5000);
cout << "\n--------newcar's info--------\n";
newcar.info();
}

 

实验任务4

pets.hpp

 

#ifndef PETS_HPP
#define PETS_HPP
#include<iostream>
#include<string>
using namespace std;
class MachinePets{
public:
    MachinePets(const string s):nickname(s) {}
    virtual string talk()
    {
        string m;
        m="hi~";
        return m;
    }
    string get_nickname() 
    {
        return nickname;
    }
private:
    string nickname;
};
class PetCats:public MachinePets{
public:
    PetCats(const string s):MachinePets(s) {}
    string talk()
    {
        string m;
        m="miao wu~";
        return m;
     } 
};
class PetDogs:public MachinePets{
public:
    PetDogs(const string s):MachinePets(s) {}
    string talk()
    {
        string m;
        m="wang wang~";
        return m;
    }
};
#endif

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-25 14:34  yami123  阅读(22)  评论(2)    收藏  举报