你如果是初学,这个程序是相当好了
#include <iostream>
#include<string>
using namespace std;
namespace demo
{
class student
{
friend ostream & operator<<(ostream &,const student & );
friend istream & operator>>(istream & ,student & );
public:
student(int,string,string);
student(student &);
int getAge();
string getName();
string getClassName();
void setAge(int);
void setName(string);
void setClassName(string);
virtual ~student();
private:
int age;
string name;
string className;
};
student::student(student &stu)
{
this->age = stu.age;
this->className = stu.className;
this->name = stu.name;
}
student::student(int age,string name,string className)
{
this->age = age;
this->name = name;
this->className = className;
}
int student::getAge(){
return this->age;
}
string student::getName(){
return this->name;
}
string student::getClassName(){
return this->className;
}
void student::setAge(int age){
this->age = age;
}
void student::setName(string name){
this->name = name;
}
void student::setClassName(string className){
this->className = className;
}
student::~student()
{
}
int main(int argc, char* argv[])
{
return 0;
}
ostream & operator<<(ostream &out ,const student &stu){
out<<"this student info is :\n"
<<"name :"<<stu.name<<"\n"
<<"age :"<<stu.age<<"\n"
<<"className :"<<stu.className<<"\n"<<endl;
return out;
}
istream &operator>>(istream &in ,student &stu)
{
in>>"Please input this student info :\n";
cout<<"name is :"<<"\n";
in>>stu.name;
cout<<"age is:"<<"\n";
in>>stu.age;
cout<<"className is:"<<"\n";
in>>stu.className;
return in;
}
}
int main(void)
{
demo::student s(14,"王二","一班");
cout<<s;
return 0;
}