实验四--类和继承

实验结果:

实验2代码

#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 arectangle\n"; }
    };
    // definition of Circle, derived from Graph
    class Circle : public Graph
    {
    public:
        void draw() {
            std::cout << "Circle::draw(): programs of draw acircle\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);
        }

未调试的结果

 

将基类函数改为虚函数之后的结果

 

 调用函数的时候,不再静态调用基类的函数,而是根据指针指向的内容来决定使用的那一部分的函数,如图,在调用draw()时候分别调用的两个子类的函数

实验3代码

#include <iostream>
#include <typeinfo>
#include <cstring>
using namespace std;

class Battery
{
private:
double capacity;
public:
Battery(const double a) { capacity = a; }
double get_cacity()
{
return capacity;
}
};
class Car
{
private:
string maker;
string model;
int year;
double odometers;
public:
Car() {};
Car(const string a, const string b, const int c, const double d = 0) :maker(a), model(b), year(c), odometers(d) {};
virtual void info() {
cout << "maker: " << maker << endl << "model : " << model << endl <<
"year: " << year << endl << "odometers: " << odometers << endl;
}
void update_odometers(double miles)
{
if (miles < odometers)
{
cout << "typed wrong, please type again!";
int temp;
cin >> temp;
update_odometers(temp);
}

else
{
odometers = miles;
}
}
};

class ElectricCar : public Car
{
private:
Battery battery;
string maker;
string model;
int year;
double odometers;
public:
ElectricCar(const string a, const string b, int c, double capacity = 70):maker(a),model(b),year(c), battery(capacity),odometers(0) {};
void info() {
cout << "maker: " << maker << endl << "model : " << model << endl <<
"year: " << year << endl << "odometers: " << odometers << endl;
cout << "capaciry :" << battery.get_cacity() << endl;
}
};

实验3测试结果:

 

 实验四代码:

#include <iostream>
#include <typeinfo>
#include <cstring>
using namespace std;

class MachinePets
{
private:
    string nickname;
public:
    MachinePets(string a = "二狗子"): nickname(a) {};
    virtual string talk() {
        // write the type of talks here.
        return "blanked";
    }
    string get_nickname()
    {
        return nickname;
    }
};

class PetCats : public MachinePets
{
public:
    PetCats(const string a) :MachinePets(a) {};
    string talk()
    {
        return "Yes!";
    }
};

class PetDogs : public MachinePets
{
public:
    PetDogs(const string a) : MachinePets(a) {};
    string talk()
    {
        return "Oh!";
    }
};

 

实验四测试结果:

 

 

 实验总结:

类继承是面对对象编程当中的一个相当重要的环节,通过定义类继承,可以是代码更简洁,并且能够体现各个类之间的关系。

代码方面:

1、多态:通过定义虚函数,可以是编译器不会在编译过程当中过早的绑定函数,而是根据传递过来的指针来决定调用的是哪里的函数,从而使得函数更灵活,注意编写的时候虚函数的格式必须要和对应函数是一致的或者在c++当中属于无损失转化的同类。

2、类继承当中,可以继承父类的各种函数接口,但是不能通过非共有接口的方式访问私有成员,因此在编写继承类的构造函数时需要注意。

3、派生类无法继承父类的运算符重载以及友元函数

posted @ 2021-11-30 22:11  Rtianzhao  阅读(63)  评论(2编辑  收藏  举报