Friend
来源:互联网 发布:数据灾备计划 编辑:程序博客网 时间:2024/06/11 13:03
friend class for each other
#include <iostream>#include <string.h>using namespace std;class Curly { public: Curly(char *msg) { strcpy(message, msg); }; void show_message(void) { cout << message << endl; }; friend class Moe; void show_moe(class Moe moe); private: char message[256]; };class Moe { public: Moe(char *msg) { strcpy(message, msg); }; void show_message(void) { cout << message << endl; }; friend class Curly; void show_curly(class Curly curly); private: char message[256]; };void Curly::show_moe(class Moe moe) { cout << moe.message << endl; };void Moe::show_curly(class Curly curly) { cout << curly.message << endl; };int main(void){ class Moe moe("nuck..."); class Curly curly("whoop..."); moe.show_message(); moe.show_curly(curly); curly.show_message(); curly.show_moe(moe);}
Define friend function for <<
#include <iostream>#include <cstring>#include <cstdlib>using namespace std;class StringClass { char *p; int len;public: StringClass(char *ptr); ~StringClass() { delete [] p; } friend ostream &operator<<(ostream &stream, StringClass &ob);};StringClass::StringClass(char *ptr){ len = strlen(ptr)+1; p = new char [len]; if(!p) { cout << "Allocation error\n"; exit(1); } strcpy(p, ptr);}ostream &operator<<(ostream &stream, StringClass &ob){ stream << ob.p; return stream;}int main(){ StringClass stringObject1("www.java2s.com"), stringObject2("www.java2s.com"); cout << stringObject1; cout << endl << stringObject2 << endl; return 0;}
- friend
- friend
- Friend
- Friend
- friend
- friend
- Friend
- friend
- Friend
- Friend
- friend
- friend webpage
- friend 函数
- friend声明
- last friend!
- friend template
- make friend
- hello,friend!
- core animation 入门教程
- BerkeleyDB简介
- Android设计模式系列(0)--开篇
- Android之SQlite存储
- tomcat进行301转向
- Friend
- 程序设计语言的异常处理机制探讨
- 数组中只出现1次的两个数字(百度面试题) .
- Loaders
- Java多线程之Callable和Future接口的实现
- asp.net --数据访问通用类
- word 上传后转pdf,pdf又转swf,工具openoffice和pdf2swf.exe
- ubuntu下安装javaee6的sdk
- C# 向数据库批量更新数据(插入、更新、删除)