C++实验友元

桂 林 理 工 大 学
实 验 报 告
实验名称 友元 日期 2019年 04 月28 日
一、实验目的:
1. 掌握友元成员的定义与使用方法
2. 掌握友元类的定义与使用方法
二、实验环境:
Visual C++

三、实验内容:
(写出主要的内容)
1. 阅读项目ex4_1,试试分别取消两个类中的友元函数声明后会有什么结果?
无法直接访问私有成员
2. 阅读项目ex4_2,试试取消友元类的声明后会有什么结果?试着在使用友元的前提下实现同样的功能。

 #include "string"
#include "iostream"
using namespace std;

class Student
{
private:
    string number;
    string name;
public:
    Student();
    Student(string number, string name);
     void  show(Student stu);
};
 
Student::Student()
{
}

Student::Student(string number, string name)
{
	this->number = number;
	this->name = name;
}

class Score
{
private:
    double engScore;
    double mathScore;
public:
    Score();
    Score(double eng, double math);
    friend void show(Student stu, Score sc);
};

Score::Score()
{
}

Score::Score(double eng, double math):engScore(eng),mathScore(math)
{
}

void show(Student stu)
{
	cout << "学号:" << stu.name << endl;
	cout << "姓名:" << stu.number << endl;
}

void show(Student stu, Score sc)
{
   show(stu);
   cout << "英语成绩:" << sc.engScore << endl;
   cout << "数学成绩:" << sc.mathScore << endl;
}

int main()
{
	Student stu("bj1601", "yhz");
	Score sc(89, 98);
	show(stu, sc);
}

输出结果:学号:yhz
姓名:bj1601
英语成绩:89
数学成绩:98
3. 打开项目ex4_2,分别用类的成员函数与友元函数的方式,实现两个复数的减法操作,并给出相应的测试代码。

#include "string"
#include "iostream"
using namespace std;

class Complex
{
private:
	double real;   //实部
	double image;   //虚部
public:
	Complex(double r = 0, double i = 0)
	{
		real = r;
		image = i;
	}

	~Complex()
	{
	}

	//用成员函数的方式实现
	string toString()
	{
		char result[100];
		sprintf(result, "(%lf) + i(%lf)\n", real, image);

		return string(result);
	}

	//用友元函数的方式实现
	friend string toString(Complex c)
	{
		char result[100];
		sprintf(result, "(%lf) + i(%lf)\n", c.real, c.image);  //尽管不是成员函数,但是友元函数,可以直接访问私有成员

		return string(result);
	}

	//用友元函数的方式实现两个复数的加法
	friend Complex add(Complex c1, Complex c2)
	{
		Complex c;
		c.image = c1.image + c2.image;
		c.real = c1.real + c2.real;
		return c;
	}
};

int main()
{
	Complex c1(23, -3);
	cout << c1.toString();   //调用成员函数
	cout << toString(c1);   //调用友元函数
	Complex c2(-32, 7);
	Complex c3 = add(c1, c2);    //调用友元函数
	cout << toString(c3);
}

输出结果:(23.000000) + i(-3.000000)
(23.000000) + i(-3.000000)
(-9.000000) + i(4.000000)
4. 编写一个Point类,属性为点的x、y轴坐标,功能为计算两点之间的距离。并编写一段测试程序。

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
	public:
	Point(int xi=0,int yi=0)
	{x=xi;
	y=yi;
	}
	int GetX(){return x;
	}
	int GetY(){return y;
	}
	private:
		int x;
		int y;
};
class Distance
{
	public:
		Distance(Point xp1,Point xp2);
		double GetDis(){return dist;
		}
	private:
		Point p1,p2;
		double dist;
};
Distance::Distance(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
	double x=double(p1.GetX()-p2.GetX());
	double y=double(p1.GetY()-p2.GetY());
	dist=sqrt(x*x+y*y);	
}
int main()
{
	Point mp1(5,10),mp2(20,30);
	Distance mdist(mp1,mp2);
	cout<<"The distance is "<<mdist.GetDis()<<endl;
}

输出结果:
The distance is 25

四、心得体会:
1、掌握了友元成员的定义与使用方法
2、掌握了友元类的定义与使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值