当前位置: 代码迷 >> 综合 >> C++,重载输出操作符, cout.operator()是什么东东?
  详细解决方案

C++,重载输出操作符, cout.operator()是什么东东?

热度:7   发布时间:2024-01-19 15:36:12.0

 

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
// 存
void setname(string s){	name = s;}
void setage(int y){age = y;	}
void setaddress(string add){address = add;}    
// 取
string getname(){return name;}
int getage(){return age;}
string getaddress(){return address;}
Student(string name="",int age=0,string address="")
{
this->name = name; this->age = age; this->address = address;      
}
~Student(){}
//重载 运算符<< : 把 "operator<<" 看作是函数名, 返回类型是 ostream类型的对象引用
friend ostream& operator<< (ostream &os,Student &st)
{
os<<st.name<<"------"<<st.age<<"------"<<st.address<<endl;
return os;
}
protected:	
private:
string name;
int    age;
string address;
};
void main()
{ 
Student x1("刘莉莉
  相关解决方案