文章标题

友元函数
2016.6.20
友元:1.友元函数2.友元类
友元;可以直接访问私有成员(破坏了数据安全)
友元函数:friend /友元函数不是成员函数¥¥
成员函数调用:对象。类名
普通函数调用:函数名
友元类:friend
如果是类A是类B的一个友元函数,那么类A中放入所有成员函数都是类B的友元函数

#include "point.hpp”                                           //main函数(测试)
#include <iostream>
using namespace std;
#include<math.h>
float distance(const Mypoint &p1,const Mypoint &p2){
     float a=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
     return a;

}
Mypoint middlepoint(const Mypoint &p1,const Mypoint &p2){
    return Mypoint((p1.x+p2.x)/2,(p1.y+p2.y)/2);
}

int main(int argc, const char * argv[]) {
    Mypoint p1(3,4);
    Mypoint p2(0,0);
    p1.print();
    p2.print();
    float a=distance(p1,p2);
    cout<<"diatance:"<<a<<endl;
    return 0;
}

#include "point.hpp”        
                                 //实现
#include <iostream>
using namespace std;
#include<math.h>
Mypoint::Mypoint(int x,int y){
    this->x=x;
    this->y=y;
}
void  Mypoint:: print(){
    cout<<"("<<x<<","<<y<<")"<<endl;
}

#ifndef point_hpp             
                                 //头文件
#define point_hpp

#include <stdio.h>
class Mypoint{
    friend   float distance(const Mypoint &p1,const Mypoint &p2);
    friend   Mypoint middlepoint(const Mypoint &p1,const Mypoint &p2);
public:
    Mypoint(int x,int y);
    void print();
private:
    int x;
    int y;
};
#endif /* point_hpp */

程序结果:

(3,4)
(0,0)
diatance:5
Program ended with exit code: 0

友元类:friend
如果是类A是类B的一个友元函数,那么类A中放入所有成员函数都是类B的友元函数

#ifndef Rectangle_hpp                                               **//rectangle的声明**
#define Rectangle_hpp
#include <stdio.h>
class Rectanle{
    friend class Cube;
public:
    Rectanle(int l,int w);
    int Area()const;
    void print();
private:
    int longth;
    int width;
};
#endif /* Rectangle_hpp */
#include <iostream>                                                  **//rectangle的实现     rectangle.cpp**
using namespace std;
#include "Rectangle.hpp"
Rectanle::Rectanle(int l,int w){
    longth=l;
    width=w;
}
int  Rectanle:: Area()const{
    return longth*width;
}
void  Rectanle::  print(){
    cout<<"longth:"<<longth<<"width"<<width<<endl;
}
#include "Rectangle.hpp”                                            **//cube头文件(的声明) cube.cpp**
#ifndef Cube_hpp
#define Cube_hpp
#include <stdio.h>
class Cube{
    friend  Cube mVolume( Cube & c1, Cube& c2);
    friend const int SumCube(const Cube & c1,const Cube c2);
public:
    Cube(int l,int w,int h);
    int volume()const;
    int pare();
    void set(int l,int w,int h);
    void print();
private:
    Rectanle r;
    int heigth;
};
#endif /* Cube_hpp */
#include "Rectangle.hpp”                                           **//cube.cpp**
#include "Cube.hpp"
#include <iostream>
using namespace std;
Cube::Cube(int l,int w,int h):r(l,w){
    heigth=h;
}
int  Cube::volume()const{
    return r.Area()* heigth;
}int Cube::pare(){
    return ( (r.width*r.longth)+(r.width*heigth)+(r.longth*heigth))*2;
}
void Cube::set(int l,int w,int h){
    r.longth=l;
    r.width=w;
    heigth=h;
}
void Cube::print(){
    cout<<"longth:"<<r.longth<<"wigth:"<<r.width<<"heigth"<<heigth<<endl;
}

 Cube mVolume( Cube & c1, Cube& c2){                          //main.cpp
    if(c1.volume()>c2.volume())
        return c1;
    else
        return c2;
}
 const int SumCube(const Cube & c1,const Cube c2){
     return c1.volume()+c2.volume();
}

int main(int argc, const char * argv[]) {
  Rectanle r(10, 20);
    r.print();
    Cube cu(10, 20, 30);
    cu.print();
    int v=cu.volume();
    int p=cu.pare();
    cout<<"colume:"<<v<<"pare:"<<p<<endl;
      Cube c2(10,5,20);
      Cube c3(10,10,20);
    Cube c1=mVolume(c2,c3);
    //maxVolume(cu,c2);
    int b=SumCube(cu,c2);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值