博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++友元函数输出整个类
阅读量:4307 次
发布时间:2019-06-06

本文共 2699 字,大约阅读时间需要 8 分钟。

#ifndef AFX_XXXXXX_111111000000000_PEOPLE_H#define  AFX_XXXXXX_111111000000000_PEOPLE_H #include 
using namespace std;//类的实现 class People{ public: string m_Name; //姓名 string m_Number; //学号 string      m_Address; //地址 int m_Age; //年龄 //const int len =20; enum {len = 12}; int m_Datas[len]; static   double m_Price; private: int m_closePrice; int m_openPrice; public: //构造函数 People(void); People(string name, string number, int age, string address); People(string name, string number, int age, string address, int quantity); //析构函数 ~People(void); void People::Breath(void); void People::ByBus(void); void People::Sleep(void); void People::Work(void) ; //友元函数 friend int GetSum(const People &people);//友元函数声明,获取开仓价和平仓价的和 friend  std::ostream & operator <<(std::ostream &os, const People &obj);//输出类的基本信息; //复制People对象 People Copy(); protected: int m_nTotal; };#endif

 

#include "People.h" #include 
//using namespace std;//类的实现 People::People(){ this->m_Address = ""; this->m_Age = 0; this->m_Name = ""; this->m_Number = ""; this->m_nTotal =0; } People::People(string name, string number, int age, string address){ this->m_Name = name; this->m_Number = number; this->m_Age = age; this->m_Address = address;} //友元函数实现 int GetSum(const People &people) {  int result =people.m_closePrice+ + people.m_openPrice;  return result;  }   std::ostream & operator << (std::ostream &os, const People &obj)  {   os << obj.m_Name << "  " << obj.m_Number << " " <<     obj.m_Age  << "  " << obj.m_Address << endl;   return os;   }    People People::Copy(){ return *this;}People::People(string m_Name, string m_Number, int m_Age, string m_Address, int m_nTotal){ this->m_Name = m_Name; this->m_Number = m_Number; this->m_Age = m_Age; this->m_Address = m_Address; //this->m_nTotal = m_nTotal;}void People::Breath(void){ cout << "Breath" << endl; } void People::ByBus(void){ cout << "ByBus" << endl; int len; if (len) { cout << "OK" << endl; } else { cout << "NO"<< endl; }} void People::Sleep(void){ cout << "Sleep" << endl;} void People::Work(void) { cout << "Work" << endl;} People::~People(){ cout << "析构函数调用" << this->m_Name << endl;};

 

#include "People.h"

#include <sstream>
#include <iostream>

 

void main()
{
 friend  std::ostream & operator <<(std::ostream &os, const People &obj);//输出类的基本信息;

 std::ostream & operator << (std::ostream &os, const People &obj)

 {
  os << obj.m_Name << "  " << obj.m_Number << " " <<
   obj.m_Age  << "  " << obj.m_Address << endl;
  return os;

 }

 

 People student = People("dd", "108253040226", 24, "ddd");
 cout << student <<endl;

 cout << endl;
 system("pause");
  
}

 

转载于:https://www.cnblogs.com/ganquanfu2008/archive/2013/06/05/3119528.html

你可能感兴趣的文章
python try 异常处理 史上最全
查看>>
poj3723Conscription
查看>>
判断listview是上滑还是下滑的方法
查看>>
转: Vim快捷键分类
查看>>
P1971 [NOI2011]兔兔与蛋蛋游戏
查看>>
SSM整合笔记
查看>>
Generator 函数的异步应用
查看>>
需求?
查看>>
c++11编码规范 NULL还是nullptr
查看>>
sql反模式分析2
查看>>
Poj 1556 The Doors 计算几何+最短路
查看>>
react项目在ie空白解决
查看>>
[spring boot] 01 环境搭建 - 配置java和mvn环境
查看>>
10个机器学习人工智能开发框架和AI库(优缺点对比表)/贪心学院
查看>>
docker常用命令
查看>>
前序工作(宏定义,typedef,函数等)
查看>>
莫队模板
查看>>
Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
查看>>
php获取当前时间戳方法
查看>>
JAVA字符串格式化-String.format()的使用
查看>>