c++一些问题2 0 友元函数,运算符的重载

243 阅读3分钟

友元类:打破了类的封装。 a普通类声明为友元函数. 友元函数可以访问类中的私有成员,打破了类的封装。 b友元成员函数。一个类的成员函数是另一个类的友元函数。 c友元类。 将一个类声明为另一类的友元类。

**代码如下** ``` #include #include #include using namespace std; class Triangle; class Point { private: int x, y; friend Triangle; //Triangle 是Point的友元类,Triangle 可以访问Point的所有成员 public: //友元函数声明,想要访问数据成员,必须传递对象的引用,通过对象访问类。 friend double getLong(Point &x,Point &y); //不是成员函数,所以类外定义 Point(int x, int y); void prit(Point &p1, Point &p2); };

Point::Point(int x=0, int y=0) //构造函数 { this->x = x; this->y = y; }

void Point::prit(Point &p1, Point &p2) { cout << sqrt((p1.x - p2.x)(p1.x - p2.x) + (p1.y - p2.y)(p1.y - p2.y)) << endl;

} double getLong(Point &p1, Point &p2) { double le; le = sqrt((p1.x-p2.x)(p1.x-p2.x)+(p1.y-p2.y)(p1.y-p2.y)); return le; }

class Triangle { public: Triangle(Point &a, Point &b, Point &c); //声明Point中的prit函数是Triangle的友元函数 friend void Point::prit(Point &p1, Point &p2);

void getxy(int *p)
{
	
	p[0] = c.x;
	p[1] = c.y;

}

private: Point a, b, c; //Point类作为数据成员

};

Triangle::Triangle(Point &a, Point &b, Point &c) { this->a = a; this->b = b; this->c = c; }

int main() { Point a(0, 0); Point b(3, 0); Point c(3, 4);

Triangle t(a,b,c);
cout << "三角形ab边长为:" ;
 a.prit(a, b);
 cout << "三角形ac边长为:" << getLong(a, c) << endl;
 int *p=new int[2];
 t.getxy(p);
 cout << "c的坐标为";
 cout<<"(" <<p[0] <<","<<p[1]<<")"<< endl;
system("pause");
return 0;

}

<hr>
*结果如图*
![
![001.PNG](http://upload-images.jianshu.io/upload_images/4976516-16c59351e424542c.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<hr>
运算符重载(<<是最典型的的重载)
    a 友元函数重载。
	b 成员函数重载。通过this指针访问本地的数据成员,可以少写一个参数。

	一般格式   函数类型 operator 运算符(形式参数){ code }

	不能重载的有5个  .(成员访问运算符), *(成员指针访问运算符), ::(域运算符),
	                 sizeof(长度运算符), ?:(条件运算符)
  注意:
     重载不能改变运算符的运算对象
	 重载不能改变运算符的优先级
	 重载不能改变运算符的结合性
	 重载不能有默认的参数 <br>
<hr>
**代码**

#include #include using namespace std; class Fraction { private: int numerator; //分子 int denominator; //分母 public: Fraction(int m, int n); void print(); friend Fraction operator + (Fraction &f1, Fraction &f2); //友元函数 Fraction operator / (Fraction &f1); };

// 构造函数 Fraction::Fraction(int m=0,int n=1) { numerator = m; denominator = n; }

// 辗转相除法,求最大公约数。 int maxYueshu(int m, int n) { if (m < n) { //保证m>n int temp = m; m = n; n = temp; } int r; while (true) { r = m %n; if (r == 0) break; m = n; n = r; }

return n;

}

//变成最简分数 void Fraction::print() { int gy= maxYueshu(numerator, denominator);

cout << numerator/gy<<"/"<< denominator/gy << endl;

} //友元函数重载。 Fraction operator + (Fraction &f1, Fraction &f2) { //友元函数不属于类。 Fraction f; f.numerator = f1.numeratorf2.denominator + f2.numeratorf1.denominator; f.denominator = f1.denominatorf2.denominator; return f; } //成员函数重载 Fraction Fraction::operator / (Fraction &f1) { Fraction f; f.numerator = numeratorf1.denominator; f.denominator = denominator*f1.numerator; return f; }

int main() { Fraction f1(8, 48); Fraction f2(6, 72); Fraction f3; cout << "f1+f2="; f3 = f1 + f2; //运算符+重载成功。 f3.print(); cout <<" f1 / f2="; f3 = f1 / f2; f3.print(); system("pause"); return 0; }

<hr>
*结果如图*
![002.PNG](http://upload-images.jianshu.io/upload_images/4976516-63aedfc8ef1d3323.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



东风啊,东风。