实验5电子宠物

#include <iostream>

using namespace std;

 

// 基类Base1

class Base1 {

    public:

        void display() const {cout << "Base1::display()" << endl; }

};

 

// 派生类Base2,公有继承自Base1

class Base2:public Base1{

    public:

        void display() const { cout << "Base2::display()" << endl; }

};

 

// 派生类Derived,公有继承自Base2

class Derived: public Base2 {

    public:

        void display() const { cout << "Derived::display()" << endl;}

};

 

void fun(Base1 *ptr) {

    ptr->display();

}

 

 

int main() {

   

    Base1 base1;

    Base2 base2;

    Derived derived;

   

    // 观察:通过"对象名.成员名"访问类族中同名成员函数时运行结果

    base1.display();

    base2.display();

    derived.display();

     

    // 观察:通过公共接口函数fun(),利用基类指针访问同名成员函数时运行结果

    fun(&base1);

    fun(&base2);

    fun(&derived);

    system("pause");

    return 0;

}

// 示例:多层继承,使用虚函数

#include <iostream>

using namespace std;

 

// 基类Base1

class Base1 {

    public:

        virtual void display() const {cout << "Base1::display()" << endl; }  // 相较于ex1_1.cpp, 基类Base1中成员函数前添加了关键字virtual   

};

 

// 派生类Base2,公有继承自Base1

class Base2:public Base1{

    public:

        void display() const { cout << "Base2::display()" << endl; }

};

 

// 派生类Derived,公有继承自Base2

class Derived: public Base2 {

    public:

        void display() const { cout << "Derived::display()" << endl;}

};

 

void fun(Base1 *ptr) {

    ptr->display();

}

 

 

int main() {

   

    Base1 base1;

    Base2 base2;

    Derived derived;

   

    // 观察:通过"对象名.成员名"访问类族中同名成员函数时运行结果

    base1.display();

    base2.display();

    derived.display();

     

    // 观察:通过公共接口函数fun(),利用基类指针访问同名成员函数时运行结果

    fun(&base1);

    fun(&base2);

    fun(&derived);

   

    return 0;

}

#include <iostream>
#include <cmath>
using namespace std;
const float PI = 3.14;
 
//抽象类Shape 
class Shape {
    public:
        virtual float area()=0;   // 纯虚函数 
}; 

// 派生类Rectangle供有继承自类Shape 
class Rectangle: public Shape {
    public:
        Rectangle() {}
        Rectangle(float l, float w): length(l), width(w) { } 
        float area() { return length*width; }
    private:
        float length, width;
};

// 派生类Cirle公有继承自类Shape
class Circle: public Shape {
    public:
        Circle() {}
        Circle(float r): radius(r) { }
        float area() { return PI*radius*radius; }
    private:
        float radius;
}; 


// 派生类Triangle公有继承自类Shape 
class Triangle: public Shape {
    public:
        Triangle() {} 
        Triangle(float a, float b, float c):side1(a), side2(b), side3(c) { }
        float area() { 
            float t;
            t = (side1 + side2 + side3) / 2;
            return sqrt( t*(t-side1)*(t-side2)*(t-side3) );
        }
    private:
        float side1, side2, side3; 
};

// 接口函数,为打印图形面积提供了一个统一的功能接口,用于打印图形面积 
// 形参是指向基类的指针,按照继承和派生机制中的赋值兼容原则,它也可以指向派生类的对象 
void printArea(Shape *p) {
    cout << p->area() << endl;
}

int main() {
    Rectangle rect(3,4);
    Circle  cc(2);
    Triangle  tt(3,4,5);
    
    printArea(&rect);
    printArea(&cc);
    printArea(&tt);
    system("pause");
    return 0;
}

#include<iostream>
#include<string>
using namespace std;
class MachinePets{
public:
    MachinePets(const string s):nickname(s){};
    virtual string talk()=0;
    string getnickname(){return nickname;};
private:
    string nickname;
};
class PetCats:public MachinePets{
public:
    PetCats(const string s):MachinePets(s){};
        string talk(){return "says miao wu";}
};
class PetDogs:public MachinePets{ 
public:
    PetDogs(const string s):MachinePets(s){};
    string talk(){return "says wang wang";}
};
void play(MachinePets *p){
    cout<<p->getnickname()<<" "<<p->talk()<<endl;
}
int main()
{
    PetCats cat("miku");
    PetDogs dog("dahuang");
    play(&cat);
    play(&dog);
    system("pause");
    return 0;
}

 

1、尝试并验证了virtual关键字的使用。

2、进一步巩固了对类的理解,加深了记忆。

posted @ 2019-06-03 17:16  与风无关  阅读(230)  评论(0编辑  收藏  举报