当前位置:文档之家› 运算符重载—C++课程实验报告

运算符重载—C++课程实验报告

(2)具体要求
1,重载前置运算符++、--;
2,重载后置运算符++、--;
3,主函数:申明点对象,进行前置和后置运算并显示点值。
实验内

实验程序设计如下:
#include<iostream>
using namespace std; class AB
{
public:
AB(int xx, int yy); void ShowAB(); AB& operator ++(); AB operator ++(int);
{
AB old=*this;
--(*this);
return old;
}
int main(void)
{
AB AA(0,0);
AB BB(0,0);
cout<v"A的值为:";
AA.ShowAB(); cout«"B的值为:";
BB.ShowAB();
coutvv"B=A++
运算后,
A的值为:";
什+AA));
coutvv"
B
的值为:";
(BB++).ShowAB();
coutvv"B=++A
运算后,
A的值为:"
什+AA).ShowAB();
coutvv"
B
的值为:";
(++BB).ShowAB();
cout<v"B=A--运算后,A的值为:";
(--AA).ShowAB();
coutvv"B的值为:";
AB& operator --();
AB operator --(int);
private:
int x1,x2;
};
AB::AB(int xx, int yy)
{
x1=xx;
x2=yy;
}
void AB::ShowAB()
{
cout<vx1vv" , "<<x2<<endl;
}
AB& AB::operator ++()
C++
学生姓

xxx
班级
学号
xxxxxxxxx
实验项

实验四运算符重载
指导教师
杜之波
实验目
的和要

一、实验目的
(1)掌握通过运算符重载实现多态性的方法;
(2)学会运算符重载的成员函数法和友元函数法;
(3)能区分单目运算符的前置与后置。
二、实验内容
编写如下要求的完整程序:点对象运算符重载
(1)建立点类,包含两个成员变量,分别表示横坐标和纵坐标;
{
x1++;
x2++; return *this;
}
AB AB::operator ++(int)
{
AB old=*this;
++(*this);
return old;
}
AB& AB::operator --()
{
x1--;
x2--;
return *this;
}
AB AB::operator --(int)
(BB--).ShowAB();
cout<<"B=--A运算后,A的值为:";
(--AA).ShowAB();
coutvv"B的值为:";
(--BB).ShowAB();
return 0;
}
实验结果如图:
13
p
EN++
E
E
B
Press key
相关主题