多态性

1、编写一个程序,要求:

(1)生明一个类Complex(复数类),定义类Complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;

(2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回两个复数对象相加操作;

(3)定义成员函数print,调用该函数时,以格式“real+imag i”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:4.2+6.5 i;

(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。

 

#include<iostream>

using namespace std;

class complex

{

private:

    double real,imag;

public:

    complex(double r,double i)

    {

        real=r;

        imag=i;

    }

    friend complex operator+(complex& a,complex& b);

    print()

    {

        cout<<real<<"+"<<imag<<endl;

    }

};

complex operator+(complex& a,complex& b)

{

    complex t;

    t.real=a.real+b.real;

    t.imag=a.imag+b.imag;

    return t;

}

int main()

{

    complex c1(2.5,3.7);

    complex c2(4.2,6.5),c3;

    c3=c1+c2;

    c3.print();

    return 0;

}

2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在0~59分,秒钟范围限制在0~59秒。

提示:时间类Time的参考框架如下:

class Time

{

   public:

     Time(int h=0,int m=0,int s=0);//构造函数

     Time operator+(Time &);//运算符重载函数,实现两个时间的相加

     void disptime();//显示时间函数

   private:

     int hours,minutes,seconds;

};

 

 

#include<iostream>

using namespace std;

class time

{

private:

    int hours,minutes,seconds;

public:

     time(int h=0,int m=0,int s=0)

     {

         hours=h;

         minutes=m;

         seconds=s;

     }

     time operator+(time &a)

     {

         time t;

         t.seconds=seconds+a.seconds;

         t.minutes=minutes+a.minutes;

         t.hours=hours+a.hours;

          if(t.seconds>=60)

         {

              t.seconds-=60;

              t.minutes=t.minutes+1;

         }

           if(t.minutes>60)

         {

              t.minutes-=60;

              t.hours=t.hours+1;

         }

            if(t.hours>24)

              t.hours=t.hours-24;

         return t;

     }

     void disptime()

     {

         cout<<"the time is:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;

     }

};

int main()

{

    time t1(6,15,30);

    time t2(12,13,30),t3;

    t3=t1+t2;

    t3.disptime();

    return 0;

}

3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。

#include<iostream>

using namespace std;

class matrix

{

private:

    int **x;

public:

    int m,n;

    matrix(int k,int p)

    {

        cout<<"矩阵的行数为:";cin>>k;cout<<"矩阵的列数为:";cin>>p;

        m=k;

        n=p;

        cout<<endl<<"输入矩阵:"<<endl;

        x=new int*[m];

        for(int i=0;i<m;i++)

        {

             x[i]=new int[n];

        }

        for(i=0;i<m;i++)

        {

             for(int r=0;r<n;r++)

             {  

                 cin>>x[i][r];

             }

        }

    }

     void operator+(matrix& c)

     {

         int t;

         cout<<"相加结果为:"<<endl;

         for(int a=0;a<m;a++)

         {

              for(int b=0;b<n;b++)

              {

             t=x[a][b]+c.x[a][b];

              cout<<t<<" ";

             t=0;

              }

              cout<<endl;

         }

     }

     void operator-(matrix& c)

     {

         int t;

         cout<<"相减结果为:"<<endl;

         for(int a=0;a<m;a++)

         {

              for(int b=0;b<n;b++)

              {

             t=x[a][b]-c.x[a][b];

              cout<<t<<" ";

             t=0;

              }

              cout<<endl;

         }

     }

    void operator*(matrix& c)

     {

         int t=0;

         cout<<"相乘结果为:"<<endl;

         for(int a=0;a<n;a++)

         {

              for(int b=0;b<m;b++)

              {

                  for(int f=0;f<n;f++)

                  

                   t+=x[a][f]*c.x[f][b];

                    cout<<t<<" ";

                    t=0;

              }

              cout<<endl;

         }

     }     

};

int main()

{

    int a,b,c,d;

    matrix m1(a,b);

    matrix m2(c,d);

    m1+m2;

    m2-m1;

    m1*m2;

    return 0;

}

 

4、【提示】

(1)可以用一下表达式实现整数集合的基本运算:

s1+s2  两个整数集合的并运算

s1-s2  两个整数集合的差运算

s1*s2  两个整数集合的交运算

(2)参考以下Set类的框架,用于完成集合基本运算所需的各项功能。

class Set

{

   public:

     Set();

     void input(int d);//向集合中添加一个元素

     int length();//返回集合中的元素个数

     int getd(int i);//返回集合中位置i的元素

     void display();//显示集合的所有元素

     Set operator+(Set s1);//成员运算符重载函数,实现集合的并运算

     Set operator-(Set s1);//成员运算符重载函数,实现集合的差运算

     Set operator*(Set s1);//成员运算符重载函数,实现集合的交运算

     Set operator=(Set s1);//成员运算符重载函数,实现集合的赋值运算     

   protected:

     int len;//统计结合中元素的个数;

     int s[MAX];//存放集合中的元素

};

 

#include<iostream>

using namespace std;

class set

{

private:

    int len;

    int s[10];

public:

    set()

    {len=0;}

    void input(int d)

    {

        for(int i=0;i<len;i++)

             if(s[i]==d)return;

             s[len]=d;

             len++;

    }

    int length()

    {

        return len;

    }

    int getd(int i)

    {

        if(i>=0&&i<len)

             return s[i];

        else return 0;

    }

    void disp()

    {

        for(int i=0;i<len;i++)

             cout<<s[i]<<" ";

        cout<<endl;

    }

    void operator+(set &ss)

    {  

        int len1=len,same;

        for(int i=0;i<ss.len;i++)

        {

             same=0;

             for(int j=0;j<ss.len;j++)

                 if(ss.getd(i)==getd(j))

                 {same=1;break;}

            if(same==0)

             {

                 s[len]=ss.getd(i);

                len++;

             }

    }

}

        void operator-(set &ss)

    {  

        int same;

        for(int i=0;i<len;i++)

        {

             same=0;

             for(int j=0;j<ss.len;j++)

                 if(s[i]==ss.getd(j))

                 {same=1;break;}

            if(same==1)

             {

                 for(int k=i;k<len;k++)

                     s[k]=s[k+1];

                 i--;

                 len--;

             }

    }

}

    void operator*(set &ss)

    {  

        int same;

        for(int i=0;i<len;i++)

        {

             same=0;

             for(int j=0;j<ss.len;j++)

                 if(s[i]==getd(j))

                 {same=1;break;}

            if(same==0)

             {

                 for(int k=i;k<len;k++)

                     s[k]=s[k+1];

                 i--;

                 len--;

             }

    }

}

    void operator=(set &ss)

    {

        this->len=ss.len;

             for(int i=0;i<ss.len;i++)

                 this->s[i]=ss.getd(i);

    }

    };

    int main()

    {

        set s1,s2,s3,s4;

    s1.input(9);s1.input(5);s1.input(4);s1.input(3);s1.input(6);s1.input(7);

        s3=s1;s4=s1;

        cout<<"集合s1中元素个数为:"<<s1.length()<<endl;

        cout<<"元素为:"<<endl;

        s1.disp();                                

      s2.input(2);s2.input(4);s2.input(6);s2.input(9);

        cout<<"集合s2中元素个数为:"<<s2.length()<<endl;

        cout<<"元素为:"<<endl;

        s2.disp();

        s1+s2;

        cout<<"s1并s2为:"<<endl;

        s1.disp();

        s3-s2;

        cout<<"s1和s2的差为:"<<endl;

        s3.disp();

        s4*s2;

        cout<<"s1和s2的交运算为:"<<endl;

        s4.disp();

        return 0;

    }

5、下面提供一个体会继承中的多态性和虚函数在多态性中的作用的题目。请根据提示进行实验。定义类BaseFly,其中Fly()函数输出特定内容。例如:

    (1)定义类BirdFly、DragonFly和PlaneFly,都继承自BaseFly,重载Fly()函数,使得各类中的Fly()函数分别输出不同的内容。

    class BirdFly:public BaseFly

    {

    public:

    void Fly() {cout<<" -----class BirdFly::Fly()----- ";}

    };

    class DragonFly:public BaseFly

    {

    public:

    vold Fly(){cout<<" -----Class DragonFly::Fly()----- ";)

    );

    class PlaneFly:public BaseFly

    {

    public:

    void Fly(){cout<<" -----Class PlaneFly::Fly()----- ";}

    };

    (2)在main()函数中,用“new”关键字分配出以上四个类的实例,调用各个实例的Fly()函数测试多态性。请参考以下代码:

    int main()

    {

    cout<<" 测试继承中的多态性(Virtual? Or not?): ';

    BaseFly *pBase;

    BirdFly *pBird=new BirdFly;

    pBasc=pBird;

    cout<<" BirdFly-> ";

    pBase->Fly();

    DragonFly * pDragon=new DragonFly;

    pBase=pDragon;

    cout<<" DragonFly-> ";

    pBase->Fly();

    PlaneFly * pPlane=new PlaneFly;

    pBase=pPlane;

    cout<<" PlaneFly-> ";

    pBase->Fly();

    return 0,

}

 

    (3) 将BaseFly::Fly()声明为virtual,在main()中定义BaseFly的指针:*pBase,依次分别指向UirdFly、DragonFly和P1aneFly,并调用各类的Fly()函数,体会虚函数作用。

   #include<iostream>

using namespace std;

class BaseFly

    {

public:

         virtual Fly()

          {cout<<" -----CIass BaseFly::Fly()----- ";}

    };

 

class BirdFly:public BaseFly

    {

public:

     Fly()

    {cout<<" -----class BirdFly::Fly()----- ";}

    };

 

class DragonFly:public BaseFly

    {

public:

    Fly()

   {cout<<" -----Class DragonFly::Fly()----- ";}

    };

 

class PlaneFly:public BaseFly

    {

public:

    Fly()

    {cout<<" -----Class PlaneFly::Fly()----- ";}

    };

 

 int main()

 {

    cout<<" 测试继承中的多态性(Virtual? Or not?): ";

    BaseFly *pBase;

    BirdFly *pBird=new BirdFly;

    pBase=pBird;

    cout<<" BirdFly-> ";

    pBase->Fly();

    DragonFly * pDragon=new DragonFly;

    pBase=pDragon;

    cout<<" DragonFly-> ";

    pBase->Fly();

    PlaneFly * pPlane=new PlaneFly;

    pBase=pPlane;

    cout<<" PlaneFly-> ";

    pBase->Fly();

    return 0;

 }

 

6、写一个程序,定义抽象类Container:

class Container

{

   protected:

      double radius;

   public:

      Container(double r);//抽象类Container的构造函数

      virtual double surface_area()=0;//纯虚函数surface_area

      virtual double volume()=0;//纯虚函数volume

};

【要求】

建立3个继承Container的派生类:Sphere(球体)、Cylinder(圆柱体)、Cube(正方体),让每一个派生类都包含虚函数surface_area()和volume(),分别用来球体、圆柱体和正方体的表面积和体积。要求写出主程序,应用C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。

 

 #include<iostream>

using namespace std;

class container

{

   protected:

      double radius;

   public:

      container(double r)

      {

          radius=r;

      }//抽象类Container的构造函数

      virtual double surface_area()=0;//纯虚函数surface_area

      virtual double volume()=0;//纯虚函数volume

};

class sphere:public container

{

protected:

public:

    sphere(double r):container(r)

    {

    }

    double surface_area()

    {

        double area;

        area=4*3.14*radius*radius;

        return area;

    }

    double volume()

    {

        double v;

        v=(4.0/3.0)*3.14*radius*radius*radius;

        return v;

    }

};

class cylinder:public container

{

protected:

    double hight;

public:

    cylinder(double r,double h):container(r)

    {

        hight=h;

    }

    double surface_area()

    {

        double area;

    area=2*3.14*radius*radius+2*radius*3.14*hight;

        return area;

    }

    double volume()

    {

        double v;

        v=3.14*radius*radius*hight;

        return v;

    }

};

class cube:public container

{

protected:

public:

    cube(double r):container(r)

    {

    }

    double surface_area()

    {

        double area;

        area=6*radius*radius;

        return area;

    }

    double volume()

    {

        double v;

        v=radius*radius*radius;

        return v;

    }

};

int main()

{

    sphere s(5.0);

    cout<<"the surface area of sphere is:"<<s.surface_area()<<endl;

    cout<<"the volume of sphere is:"<<s.volume()<<endl;

    cylinder c(5.0,6.0);

    cout<<"the surface area of cylinder is:"<<c.surface_area()<<endl;

    cout<<"the volume of cylinder is:"<<c.volume()<<endl;

    cube cu(6.0);

    cout<<"the surface area of cube is:"<<cu.surface_area()<<endl;

    cout<<"the volume of cube is:"<<cu.volume()<<endl;

    return 0;

}

朝闻道
原文地址:https://www.cnblogs.com/wander-clouds/p/8443780.html