前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++一些问题2.0 友元函数,运算符的重载

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

作者头像
热心的社会主义接班人
发布2018-04-27 13:13:52
7500
发布2018-04-27 13:13:52
举报
文章被收录于专栏:cscs

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


代码如下

代码语言:javascript
复制
#include<iostream>
#include<string>
#include<cmath>
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;
}

结果如图 ![

001.PNG


运算符重载(<<是最典型的的重载) a 友元函数重载。 b 成员函数重载。通过this指针访问本地的数据成员,可以少写一个参数。

代码语言:javascript
复制
一般格式   函数类型 operator 运算符(形式参数){ code }

不能重载的有5个  .(成员访问运算符), *(成员指针访问运算符), ::(域运算符),
                 sizeof(长度运算符), ?:(条件运算符)

注意: 重载不能改变运算符的运算对象 重载不能改变运算符的优先级 重载不能改变运算符的结合性 重载不能有默认的参数


代码

代码语言:javascript
复制
#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); //友元函数
    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.numerator*f2.denominator + f2.numerator*f1.denominator;
    f.denominator = f1.denominator*f2.denominator;
    return f;
}
               //成员函数重载
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; //运算符+重载成功。
    f3.print();
    cout <<" f1 / f2=";
    f3 = f1 / f2;
    f3.print();
    system("pause");
    return 0;
}

结果如图

002.PNG

东风啊,东风。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档