文章标题
2016-06-23 17:41
169 查看
友元函数
2016.6.20
友元:1.友元函数2.友元类
友元;可以直接访问私有成员(破坏了数据安全)
友元函数:friend /友元函数不是成员函数¥¥
成员函数调用:对象。类名
普通函数调用:函数名
友元类:friend
如果是类A是类B的一个友元函数,那么类A中放入所有成员函数都是类B的友元函数
程序结果:
友元类:friend
如果是类A是类B的一个友元函数,那么类A中放入所有成员函数都是类B的友元函数
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; }
相关文章推荐
- Android Native 绘图方法
- Python动态类型的学习---引用的理解
- 土人系列AS入门教程 -- 对象篇
- C#托管堆对象实例包含内容分析
- C#中struct和class的区别详解
- C#实现获取不同对象中名称相同属性的方法
- javascript asp教程第十一课--Application 对象
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- VBS ArrayList Class vbs中的数组类
- VBS教程:对象-正则表达式(RegExp)对象
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- C#检查指定对象是否存在于ArrayList集合中的方法
- sql2008启动代理未将对象应用到实例解决方案
- C#编程自学之类和对象
- C++中对象的常引用、动态建立和释放相关知识讲解
- php中将一个对象保存到Session中的方法
- php对象和数组相互转换的方法
- PHP中把对象转换为关联数组代码分享
- 深入了解PHP类Class的概念
- C#写入对象或集合类型数据到xml文件的方法