关于c++:C类的多态性一

34次阅读

共计 3624 个字符,预计需要花费 10 分钟才能阅读完成。

目标
把握运算符重载的应用办法

内容
编程题
1.(1)以成员函数的形式,实现运算符“+”的重载,程序运行后果放弃不变;(2)以友元函数的形式,实现运算符“+”的重载,程序运行后果放弃不变。
代码:
(1)

#include<iostream.h>
class Box
{
public:
    Box(){}
    Box(int l,int b, int h)
    {
    length=l;
    breadth=b;
    height=h;
    }
    Box operator+(Box& b)
    {
    Box box;
    box.length=length+b.length;
   box.breadth=breadth+b.breadth;
    box.height=height+b.height;
    return box;
    }
   void print()
   {
   cout<<"length:"<<length<<endl;
   cout<<"breadth:"<<breadth<<endl;
   cout<<"height:"<<height<<endl;
   }
private:
    double length;
    double breadth;
    double height;
};
int main()
{Box Box1(1,2,3);
Box Box2(4,5,6);
Box Box3;
Box3=Box1+Box2;
Box3.print();
return 0;
}

后果:

(2)

#include<iostream.h>
class Box
{
public:
    Box(){}
    Box(int l,int b, int h)
    {
    length=l;
    breadth=b;
    height=h;
    }
   friend Box operator+(Box& b,Box& b1);
   void print()
   {
   cout<<"length:"<<length<<endl;
   cout<<"breadth:"<<breadth<<endl;
   cout<<"height:"<<height<<endl;
   }
private:
    double length;
    double breadth;
    double height;
};
    Box operator+(Box& b,Box& b1)
    {
    Box box;
    box.length=b1.length+b.length;
    box.breadth=b1.breadth+b.breadth;
    box.height=b1.height+b.height;
    return box;
    }
int main()
{Box Box1(1,2,3);
Box Box2(4,5,6);
Box Box3;
Box3=Box1+Box2;
Box3.print();
return 0;
}

后果:

2. 把握以类的成员函数重载运算符的应用办法
代码:

#include<iostream.h>
class Time
{
private:
    int hour;
    int minute;
public:
    Time(){hour=0; minute=0;}
    Time (int h,int m);
    Time operator+(Time &t2);
    void display();};
Time::Time(int h,int m)
{
hour=h;
minute=m;
}
Time Time::operator +(Time &t2)
{
Time t;
t.hour=hour+t2.hour;
t.minute=minute+t2.minute;
if(t.minute>=60)
{
t.minute-=60;
t.hour++; 
}
return t;
}
void Time::display()
{cout<<hour<<"小时"<<minute<<"分钟"<<endl;}
int main()
{Time t1(3,30),t2(2,40);
Time t;
t=t1+t2;
cout<<"t1+t2=";
t.display();
return 0;
}


3. 把握以非成员函数重载运算符的应用办法
代码:

#include<iomanip>
#include<iostream.h>
class Matrix
{
private:
int mat[2][3];
public:
Matrix();
friend Matrix operator+(Matrix &a,Matrix &b);
void input();
void display();};
Matrix::Matrix()
      {for(int i=0;i<2;i++)
          for(int j=0;j<3;j++)
              mat[i][j]=0;
       }
Matrix operator+(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
          for(int j=0;j<3;j++)
             c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
          return c;
}
void Matrix::input()
{for(int i=0;i<2;i++)
          for(int j=0;j<3;j++)
              cin>>mat[i][j];
}
void Matrix::display()
{for(int i=0;i<2;i++)
{for(int j=0;j<3;j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
}
int main()
{
Matrix a,b,c;
cout<<"input a:\n";
a.input();
cout<<"input b:\n";
b.input();
c=a+b;
cout<<"output c:\n";
c.display();
return 0;
}

后果:

4. 定义一个 Point 类,蕴含公有的数据成员 x,y,按要求实现上面的编程
(1)重载运算符“<<”和“>>”,用于输入输出 Point 类的对象。
(2)重载运算符“++”和“–”,实现对 Point 类对象的自增和自减运算(x 和 y 同时加 1 或减 1),要求同时重载前缀和后缀的模式。
(3)重载运算符“+”和“-”,实现两个点坐标的相加与相减。

代码:

#include<iomanip>
#include<iostream.h>
class point
{
private:
int x,y;
public:
friend ostream & operator<<(ostream&,point&);
friend istream & operator>>(istream&,point&);
point operator++()
{
x++;
y++;
return *this;
}
point operator++(int)
{
x++;
y++;
return *this;
}
point operator--()
{
x--;
y--;
return *this;
}
point operator--(int)
{
x--;
y--;
return *this;
}
friend point operator+(point &,point &);
friend point operator-(point &,point &);
};
ostream & operator<<(ostream &out,point &a)
{
    out<<"x 坐标为:"<<a.x<<"y 坐标为:"<<a.y<<endl;
    return out;
}
istream & operator>>(istream &in,point &b)
{
    in>>b.x;
    in>>b.y;
    return in;
}

point operator+(point &b,point &b1)
{
point bb;
bb.x=b1.x+b.x;
bb.y=b1.y+b.y;
return bb;
}
point operator-(point &b,point &b1)
{
point bb;
bb.x=b.x-b1.x;
bb.y=b.y-b1.y;
return bb;
}
void getint(int &f)
{
cin>>f;
int a;
a=f;
}
int main()
{
point a,b,f,z;
int c,d,e;
cout<<"请输出两个类对象的 x 和 y 坐标:"<<endl;
cout<<"请输出第一个的 x 和 y 坐标:"<<endl;
cin>>a;
cout<<a;
cout<<"请输出第二个的 x 和 y 坐标:"<<endl;
cin>>b;
cout<<b;
cout<<"请抉择:1、两个坐标点自增或自减 2、两个坐标点相加或相减"<<endl;
getint(c);
if(c==1)
{
cout<<"1: 自增,2: 自减"<<endl;
getint(d);
if(d==1)
{
++a;
b++;
cout<<"自增后 x 和 y 坐标为:"<<endl;
cout<<a;
cout<<b;
}
else 
{
--a;
b--;cout<<"自减后 x 和 y 坐标为:"<<endl;
cout<<a;
cout<<b;
}
}
else
{
cout<<"1: 相加 2: 相减"<<endl;
getint(e);
if(e==1)
{
f=a+b;
cout<<"相加为:"<<endl;
cout<<f;
}
else
{
z=a-b;
cout<<"相减为:"<<endl;
cout<<z;
}
}
return 0;
}

后果:

自增:

自减:

相加:

相减:

文章图片起源:网络游戏

正文完
 0