实验四 多态性

实验名称

实验四 多态性

实验目的和要求

(1)掌握C++中运算符重载的机制和运算符重载的方式;

(2)理解类型转换的必要性,掌握类型转换的使用方法;

(3)理解多态性,掌握虚函数的设计方法;

(4)掌握纯虚函数和抽象类的使用方法。

实验内容

1. 定义point类,有坐标x,y两个数据成员,对point类重载“++”(自增),--(自减)运算符,实现对坐标值的改变。

2. 声明一个车(Vehicle)基类,有RunStop等成员函数,由此派生出自行车(Bicycle)类、汽车(car)类,从自行车和汽车类派生出摩托车(Motorcycle)类,它们都有Run()、Stop()等成员函数。利用纯虚函数和抽象类解决问题。(注意和实验三第(2)题实现方法的区别,考察动态连编和静态连编的不同实验结果)

3声明一个shape基类,有计算面积arae()成员函数,由此派生出正方形类(Square)、直角三角形类(Triangle)、圆类(Circle),它们都有arae()成员函数。利用虚函数编程计算正方形、直角三角形和圆的面积之和。

主要仪器设备

台式或笔记本电脑

实验记录

1. 

#include<iostream>

using namespace std;

 

class Point

{

int x,y;

public:

Point ( int a, int b ) // 构造函数,调用成员函数对 x,y作初始化

    { x = a ;  y = b ;  }

void show(){cout << "x:" << x << " y:" << y << endl;}

Point operator ++();

Point operator --();

};

Point Point::operator++()//感觉前置比后置好写,所以没写后置,以后有机会补上

{

x++;

y++;

return *this;

}

Point Point::operator--()

{

x--;

y--;

return *this;

}

 

int main()

{

    int a, b;

    cin >> a >> b;

    Point p(a, b);

    p.show();

         cout << "Show ++p:  ";

 

         (++p).show();

             cout << "Show --p:  ";

 

         (--p).show();

 

 

    return 0;

}

2.

#include<iostream>
using namespace std;
class vehicle
{
public:
   virtual void run(){}//virtual写一个就可以了,后面会直接引用
   virtual void stop(){}
};
class bicycle:virtual public vehicle
{
public:
    bicycle(){}
    void run();
    void stop();
};
class car:virtual public vehicle
{
public:
    car(){}
    void run();
    void stop();
};
class motocycle:public bicycle,public car
{
public:
    motorcycle(){}
    void run();
    void stop();
};
void bicycle::run()
{
    cout<<"bicycle:run"<<endl;
}
void car::run()
{
    cout<<"car:run"<<endl;
}
void motocycle::run()
{
    cout<<"motocycle:run"<<endl;
}
void bicycle::stop()
{
    cout<<"bicycle:stop"<<endl;
}
void car::stop()
{
    cout<<"car:stop"<<endl;
}
void motocycle::stop()
{
    cout<<"motocycle:stop"<<endl;
}
int main()
{   bicycle a1;
    vehicle *p = &a1;
//    motocycle *m = &a1;//错误
    a1. run();
    a1. stop();
    p->run();//有结果输出,且输出非基类的结果
    p->stop();
//    m->run();
//    m->stop();

    car a2;
    p=&a2;
    a2. run();
    a2. stop();
    p->run();
    p->stop();

    motocycle a3;
    bicycle *b = &a3;//有结果输出,且输出为指针指向的值
    p=&a3;
    a3. run();
    a3. stop();
    p->run();
    p->stop();
    b->run();
    b->stop();

return 0;
}

3. 

#include <iostream.h>

#include<math.h>

 using namespace std;

const double PI = 3.1415926;

class Shape

{

   public:

     virtual void area( ) =0;

};

//

class circle:public Shape

{

    double r;

    public:

    circle(double i):r(i){}

    void area( )

    {

     cout << "圆的面积:" << r*r*PI << endl;

    }

    

};

class Retangle:public Shape

{

   double a, b, s;

   public:

    Retangle(double x=0,double y=0){

     a=x;

      b=y;

    }

    void area( )

    {

     s = a*b;

     cout << "长方形面积:" << s << endl;

    }

};

class Triangle:public Shape

{

    double a, b, s;

   public:

    Triangle(double i, double j):a(i), b(j){}

    void area( )

    {

     s = (a*b)/2;

     cout << "三角形面积:" << s << endl;

    }

   

};

int main( )

{

  

 double r, a, b, c, d;

 cin >> r >> a >> b >> c >> d;

 Shape  *p;

 circle c1(r);

 p = &c1;

 p -> area();

 Retangle c2(a, b);

 p = &c2;

 p -> area();

 Triangle c3(c, d);

 p = &c3;

 p -> area();  

return 0;

}

遇到的问题和解决方法

1. 重载自增和自减运算符的时候有前置和后置的区别,我对这后置形参这儿还有点迷糊,不知道实参应该写哪,

解决方法:查书查资料。

心得体会

重载还是需要记住的,也得会用,我现在写的这个基本上是套的模板,目前还记不住。

虚函数还是蛮好用的,以后在写代码的时候应该能探索到更多。

猜你喜欢

转载自blog.csdn.net/klftespace/article/details/80798274
今日推荐