C++——类的多态性(一)

雷薄
• 阅读 935

目的
掌握运算符重载的使用方法

内容
编程题
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;
}

结果:
C++——类的多态性(一)
(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;
}

结果:
C++——类的多态性(一)
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;
}

C++——类的多态性(一)
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;
}

结果:
C++——类的多态性(一)
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;
}

结果:

自增:
C++——类的多态性(一)
自减:
C++——类的多态性(一)

相加:
C++——类的多态性(一)
相减:
C++——类的多态性(一)
文章图片来源:网络游戏

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
4年前
java重载和重写的区别
一、重载重载方法的规则:1、重载是针对在同一个类中。2、重载方法名一个样。3、参数列表:被重载的方法必须改变参数列表。4、返回类型: 可以改变返回类型。5、修饰符:可以改变修饰符。6、异常:可以声明新的或者更广泛的异常。其中:1.方法重载是让类以统一的方式处理不同类型数据的一种手段。多个同名函数同时
Irene181 Irene181
4年前
一篇文章带你了解Python运算符重载
您可以根据所使用的操作数来更改Python中运算符的含义。这种做法称为运算符重载,今天我们一起来聊聊运算符重载。一、什么是Python中的运算符重载?Python运算符用于内置类。但是相同的运算符对不同的类型有不同的行为。例如,运算符将对两个数字执行算术加法、合并两个列表并连接两个字符串。Python中的这一功能允许同一运算符根据上下文具有不同的含
Irene181 Irene181
4年前
一篇文章带你了解Python运算符重载
您可以根据所使用的操作数来更改Python中运算符的含义。这种做法称为运算符重载,今天我们一起来聊聊运算符重载。一、什么是Python中的运算符重载?Python运算符用于内置类。但是相同的运算符对不同的类型有不同的行为。例如,运算符将对两个数字执行算术加法、合并两个列表并连接两个字符串。Python中的这一功能允许同一运算符根据上下文具有不同的含
Wesley13 Wesley13
4年前
C++课程第五次博客——多态
\TOC\多态性Part1多态性概述多态是指同样的消息被不同类型的对象接收时导致不同的行为。在C中,所谓信息是指对类的成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数。1)多态的类型分为四类:重载多态,强制多态,包含多态和参数多态。前两者为专用多态,而后者称为通用多态。2)
Stella981 Stella981
4年前
JetBrains ReSharper 快捷键
CtrlSpace代码完成CtrlShiftSpace代码完成CtrlAltSpace代码完成CtrlP显示参数信息AltInsert生成构造函数,属性,重载成员函数,实现接口CtrlAltJ生成if,try..catch,region块Ctrl/注释及取消
Wesley13 Wesley13
4年前
C++运算符重载
1、运算符重载:实质就是函数重载返回值类型operator运算符(形参表){......}运算符重载为普通函数:!(https://oscimg.oschina.net/oscnet/ef9c4a7f84c3c3351000426043d49537abb.png)运算符重载为成员函
Stella981 Stella981
4年前
Boost Python官方样例(三)
导出C类(纯虚函数和虚函数)大致做法就是为class写一个warp,通过get\_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出warp类,但是类名使用class,析构函数不需要导出,因为它会被自动调用纯虚函数编写C函
Wesley13 Wesley13
4年前
C++中构造函数和析构函数
构造函数定义它是一种特殊的方法。主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。另外,一个类可以有多个构造函数,我们可以根据其参数个数的不同或参数类型的不同来区分它们(这就是构造函数的重载)特点1.构造函数的命名必须和类名完全相同;2.构造函数的功能主要用于在类的对象创建时定义
Wesley13 Wesley13
4年前
C++ 逗号运算符
1.逗号运算符的结果是,最后那个式子的结果2.逗号运算符是优先级最低的,比等号的运算符优先级还低1inta2;2intb3;3intca,b,ab;4//结果是c2;因为等号赋值运算符
Wesley13 Wesley13
4年前
C++重载双目运算符(2)(对象与数之间)
有两种方法:(1)采用重载双目运算符方式(2)1.类型转换函数(将类的对象转换为其他数据类型)2.转换构造函数(将其他类型转换为类的对象)(仍然要配合重载双目运算符的方式,因为最终实现的是类的两个对象相加)(注意:类型转换函数和转换构造函数不可同时使用,会出现二
Stella981 Stella981
4年前
Python运算符
1.运算符1.1运算符的概念运算符⽤于执⾏程序代码运算,会针对⼀个以上操作数项⽬来进⾏运算。例如:23,其操作数是2和3,⽽运算符则是“”1.2运算符的分类算术运算符赋值运算符⽐较运算符(关系运算符)逻辑运算符条件运算符(三元运算符)2.算术运算符