大一下 c + + 上机实验总结(八)

大一下c + +上机实验总目录:大一下c + +上机实验总结目录


1、定义复数类,用友元函数重载+,- 运算符。并编写主函数进行测试。(实验七是要求用成员函数重载)
参考答案0:
用友元函数重载运算符

#include<iostream.h>
class complex
{
double real,imag;

public:
	complex(double r=0,double i=0)
	{
		real=r;
		imag=i;
	}
	void print();
	friend complex operator+(complex a,complex b);
        friend complex operator-(complex a,complex b);
};
void complex::print()
{
	cout<<real;
	if(imag>0) cout<<"+";
	if(imag!=0)  cout<<imag<<"i"<<endl;
}
complex operator+(complex a,complex b)
{
	complex temp;
	temp.real=a.real+b.real;
	temp.imag=a.imag+b.imag;
	return temp;
}
complex operator-(complex a,complex b)
{
	complex temp;
	temp.real=a.real-b.real;
	temp.imag=a.imag-b.imag;
	return temp;
}
void main()
{
	complex A1(2,3),A2(1,1),A3,A4;
	A3=A1+A2;
	A4=A1-A2;
	A1.print();
	A2.print();
	A3.print();
	A4.print();
}

2、定义point类,分别用成员函数和友元函数重载前置–和后置的–运算符。
参考答案:
(1)用友元函数重载运算符
前置–

#include<iostream.h>
class point
{
	int x,y;
public:
	point(int,int);
	friend void operator--(point&);
	void print();
};
point::point(int a,int b)
{
	x=a;
	y=b;
}
void operator--(point &p)
{
	--p.x;
	--p.y;
}
void point::print()
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
void main()
{
   point p(2,2);
   p.print();
   (--p);
   p.print();
}

后置–

#include<iostream.h>
class Point
{
	int x,y;
public:
	Point(int x,int y);
	friend Point operator--(Point &point,int a);
	void showprint();
};
Point::Point(int x,int y)
{
	this->x=x;
	this->y=y;
}
Point operator--(Point &point,int a)
{
	Point p(1,1);
	p=point;
	--point.x;
	--point.y;
	return p;
}
void Point::showprint()
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
void main()
{
	Point point(2,2);
	point.showprint();
    (operator--(point,0)).showprint();
	point.showprint();

}

(2)用成员函数重载运算符
前置–

#include<iostream.h>
class Point
{
	int x,y;
public:
	Point(int,int);
        Point operator--();
	void print();
};
Point::Point(int a,int b)
{
	x=a;
	y=b;
}
Point Point::operator--()
{
	--x;
	--y;
	return *this;
}
 void Point::print()
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
void main()
{
   Point p(2,2);
   p.print();
   (--p);
   p.print();
}

后置–

#include<iostream.h>
class Point
{
	int x,y;
public:
	Point(int,int);
        Point operator--(int);
	void print();
};
Point::Point(int a,int b)
{
	x=a;
	y=b;
}
Point Point::operator--(int a)
{   Point p(1,1);
    p=*this;
    --x;
    --y;
   return p;
}
 void Point::print()
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
void main()
{
   Point p(2,2);
   p.print();
   (p.operator--(0)).print();
   p.print();
}

3、用友元类和类的组合解决下面的问题
(1)设计一个point 类
数据成员:
点的坐标x,y
成员函数:
带有参的构造函数(不带默认值)
(2)定义一个line类
数据成员:
线上的两个点point1,point2(用定义好的point)
成员函数:
定义一条直线(构造函数)
计算线段的长度
参考答案1:

#include <iostream>
#include <cmath>
using namespace std;
class Point{
	int x,y;
public:
	Point(int xx,int yy):x(xx),y(yy){}
	friend class Line;
};

class Line{
	Point p1,p2;
public:
	Line(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2)
	{}
	double length();
};
double Line::length(){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}
int main(){
	Line line(1,1,5,5);
	cout<<line.length()<<endl;
	return 0;
}

参考答案2:

#include<iostream.h>
#include<math.h>
class line;
class point
{private:
	float x,y;
 public:
 point(float a,float b){x=a;y=b;}
 friend class line;
};
class line
{
  point point1,point2;
 public:
 line(point a,point b):point1(a),point2(b){}
 float length()
  {return sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y));
}
};
void main()
{point poin1(1,2),poin2(3,4);
 line lin(poin1,poin2);
 cout<<lin.length();
} 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值