友元類:打破了類的封裝移斩。
a普通類聲明為友元函數(shù). 友元函數(shù)可以訪問(wèn)類中的私有成員肚医,打破了類的封裝。
b友元成員函數(shù)向瓷。一個(gè)類的成員函數(shù)是另一個(gè)類的友元函數(shù)肠套。
c友元類。 將一個(gè)類聲明為另一類的友元類猖任。
代碼如下
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Triangle;
class Point {
private:
int x, y;
friend Triangle; //Triangle 是Point的友元類你稚,Triangle 可以訪問(wèn)Point的所有成員
public:
//友元函數(shù)聲明,想要訪問(wèn)數(shù)據(jù)成員朱躺,必須傳遞對(duì)象的引用,通過(guò)對(duì)象訪問(wèn)類入宦。
friend double getLong(Point &x,Point &y); //不是成員函數(shù),所以類外定義
Point(int x, int y);
void prit(Point &p1, Point &p2);
};
Point::Point(int x=0, int y=0) //構(gòu)造函數(shù)
{
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函數(shù)是Triangle的友元函數(shù)
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類作為數(shù)據(jù)成員
};
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邊長(zhǎng)為:" ;
a.prit(a, b);
cout << "三角形ac邊長(zhǎng)為:" << getLong(a, c) << endl;
int *p=new int[2];
t.getxy(p);
cout << "c的坐標(biāo)為";
cout<<"(" <<p[0] <<","<<p[1]<<")"<< endl;
system("pause");
return 0;
}
結(jié)果如圖
![
001.PNG
運(yùn)算符重載(<<是最典型的的重載)
a 友元函數(shù)重載室琢。
b 成員函數(shù)重載乾闰。通過(guò)this指針訪問(wèn)本地的數(shù)據(jù)成員,可以少寫一個(gè)參數(shù)盈滴。
一般格式 函數(shù)類型 operator 運(yùn)算符(形式參數(shù)){ code }
不能重載的有5個(gè) .(成員訪問(wèn)運(yùn)算符), *(成員指針訪問(wèn)運(yùn)算符), ::(域運(yùn)算符),
sizeof(長(zhǎng)度運(yùn)算符), ?:(條件運(yùn)算符)
注意:
重載不能改變運(yùn)算符的運(yùn)算對(duì)象
重載不能改變運(yùn)算符的優(yōu)先級(jí)
重載不能改變運(yùn)算符的結(jié)合性
重載不能有默認(rèn)的參數(shù)
代碼
#include<iostream>
#include<string>
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); //友元函數(shù)
Fraction operator / (Fraction &f1);
};
// 構(gòu)造函數(shù)
Fraction::Fraction(int m=0,int n=1) {
numerator = m;
denominator = n;
}
// 輾轉(zhuǎn)相除法涯肩,求最大公約數(shù)。
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;
}
//變成最簡(jiǎn)分?jǐn)?shù)
void Fraction::print() {
int gy= maxYueshu(numerator, denominator);
cout << numerator/gy<<"/"<< denominator/gy << endl;
}
//友元函數(shù)重載巢钓。
Fraction operator + (Fraction &f1, Fraction &f2) { //友元函數(shù)不屬于類病苗。
Fraction f;
f.numerator = f1.numerator*f2.denominator + f2.numerator*f1.denominator;
f.denominator = f1.denominator*f2.denominator;
return f;
}
//成員函數(shù)重載
Fraction Fraction::operator / (Fraction &f1) {
Fraction f;
f.numerator = numerator*f1.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; //運(yùn)算符+重載成功。
f3.print();
cout <<" f1 / f2=";
f3 = f1 / f2;
f3.print();
system("pause");
return 0;
}
結(jié)果如圖
002.PNG
東風(fēng)啊症汹,東風(fēng)硫朦。