实验四 继承

------------恢复内容开始------------

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

实验四 继承

 

实验四 继承

 

 实验二:

同名覆盖原则:如果派生类中声明了与基类函数成员相同的新函数,基类的同名函数及其所有重载形式都会被隐藏。

二元作用域分辨符:如果派生类的部分或全部直接基类是从另一个共同的基类派生而来的,在这些直接基类中,从上一级基类继承的成员都拥有相同的名称,因此派生类中也就会产生同名现象,因此需要使用作用域分辨符来唯一标识。

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

 两次区别为第一次的RTTI type为Graph,第二次的RTTI type为所指的派生类。原因是加上了virtual,构成了虚函数,指针所指的元素变为了派生类。

#ifndef battery_hpp
#define battery_hpp
#include<iostream>
using namespace std;
class Battery{
    public:
        Battery(int capacity0);
        ~Battery();
        int get_capacity();
    private:
        int capacity;
};
Battery::Battery(int capacity0=70):capacity(capacity0){
}
Battery::~Battery(){
}
int Battery::get_capacity(){
    return capacity;
}
#endif
#ifndef car_hpp
#define car_hpp
#include<iostream>
#include<string>
using namespace std;
class Car{
    public:
    Car(string maker0,string model0,int year0);
    ~Car(){}
    void info();
    void update_odometers(int odometers1);    
    private:
    string maker;
    string model;
    int year;
    int odometers;    
};
Car::Car(string maker0,string model0,int year0):maker(maker0),model(model0),year(year0),odometers(0){
}
void Car::info(){
    cout<<"maker:         "<<maker<<endl;
    cout<<"model:         "<<model<<endl;
    cout<<"year:          "<<year<<endl;
    cout<<"odometers:     "<<odometers<<endl;
    return ;
}
void Car::update_odometers(int odometers1){
    if(odometers<=odometers1)
    odometers=odometers1;
    else
    cout<<"updated odometers is wrong"<<endl;
    return ;
}
#endif
#ifndef electricCar_hpp
#define electricCar_hpp
#include<iostream>
#include<string>
#include"car.hpp"
#include"battery.hpp"
using namespace std;
class ElectricCar:public Car{
    public:
    ElectricCar(string maker0,string model0, int year0,int capacity0=70):Car(maker0,model0,year0),battery(capacity0){
    }
    void info();    
    private:
    Battery battery;
};
void ElectricCar::info()
{
    Car::info();
    cout<<"capacity:      "<<battery.get_capacity()<<endl;
}

#endif
#include<iostream>
#include"electricCar.hpp"
#include"car.hpp"
#include"battery.hpp"
int main()
{
using namespace std;
// test class of Car
Car oldcar("Su change", "a4", 2006);
cout << "--------oldcar's info--------" << endl;
oldcar.update_odometers(25000);
oldcar.info();
cout << endl;
// test class of ElectricCar
ElectricCar newcar("Li zhiyang", "model sss", 3016);
newcar.update_odometers(25009);
cout << "\n--------newcar's info--------\n";
newcar.info();
}

实验四 继承

 

#ifndef pets_hpp
#define pets_hpp
#include<iostream>
#include<string>
using namespace std;
class Machinepets{
    public:
    Machinepets(const string nickname0);
    string get_nickname();
    virtual string talk();    
    private:    
    string nickname;
};
Machinepets::Machinepets(string nickname0):nickname(nickname0){
}
string Machinepets::get_nickname(){
    return nickname;
}
string Machinepets::talk(){
    string s=" ";
    return s; 
}
class PetCats:public Machinepets{
    public:
    PetCats(const string s):Machinepets(s){
    };
    string talk();
    private:
    string nickname;    
};
string PetCats::talk(){
    string s="miao wu~";
    return s;
}
class PetDogs:public Machinepets{
    public:
    PetDogs(const string s):Machinepets(s){
    };
    string talk();
    private:
    string nickname;    
};
string PetDogs::talk(){
    string s="wang wang~";
    return s;
}
#endif
#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);
}

实验四 继承

实验总结:

派生类构造函数形式还需要多写几遍。对于继承的规则掌握的还不够,需要经常翻书才能完成实验

2.

上一篇:东北大学软件项目管理与过程改进bb平台题目整理(Chapter 1)


下一篇:Chapter 8 理解需求