C++友元函数输出整个类

#ifndef AFX_XXXXXX_111111000000000_PEOPLE_H
#define  AFX_XXXXXX_111111000000000_PEOPLE_H
 
#include <string>
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 <iostream>
//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");
  
}

 

posted @ 2013-06-05 19:49  Predator  阅读(348)  评论(0编辑  收藏  举报