一个c++ strunct的范例

 

 

/*

  结构体与类之间的区别在于:结构体中所有成员(包括数据成员和成员函数)缺省情况下都是public,
  而类中所有成员缺省情况下都是private。除些之外,两者可以通用。

*/

#include <iostream>
#include <string>
using namespace std;
struct student
{
 student();
 void show();
 string getInfo();
 int age;
 void setSex(string sexStr);
 private:
  string sex;
};
student::student(){
 sex = "boy";
}
void student::setSex(string sexStr){
 sex = sexStr;
}
void student::show()
{
 cout<<"age = "<<age<<"\n";
};
string student::getInfo()
{
 string typeSex = "";
 if(sex=="boy"){
  typeSex = "He";
 }else if(sex=="girl"){
  typeSex = "She";
 }
 return "this a student."+typeSex+" is "+sex+"."; 
};
int main(){
 student stud;
 stud.age = 101;
 stud.show();
 string sexType;
 cout<<"请输入这个学生的性别girl/boy: ";
 cin>>sexType;
 stud.setSex(sexType);
 //cout<<"hello: "<<stud.show()<<"\n";
 cout<<stud.getInfo()<<"\n";
 system("PAUSE");
 return 0;
}

posted @ 2008-12-19 11:23  vily_雷  阅读(342)  评论(0编辑  收藏  举报