//函数返回结构的使用示例

//函数返回结构的使用示例 
#include<iostream.h>
#include<conio.h> 
 struct student 
 {
    int num;
    char name[20];
    char sex;
    float score;       
        
 }; 
 
 student getstudent();
 void displaystudentinfo(const student &stud);
 
 int main()
 {
   student thestud={102,"li xiao ming",'M',92}; 
   
   cout<<"Intial student information:";
   displaystudentinfo(thestud); 
   
   thestud=getstudent();  //将右边结构类型的成员整体赋给左边 
   cout<<"\nAfter call getstudent:";
   displaystudentinfo(thestud);
    
      getch(); 
   return 0;  
 } 
 
student getstudent()     //返回值为student 型   
{
  student stud;
  
  cout<<"Please enter the number:";
  cin>>stud.num;
  
  cout<<"Please enter the name:";
  cin>>stud.name; 
  
  cout<<"Please enter the sex:";
  cin>>stud.sex;
  
  cout<<"Please enter the score:"; 
  cin>>stud.score;
          
       
  return stud;      //返回值仍然为值传递,类似return n(n为整型等基本数据类型); 
        
} 
 
void displaystudentinfo(const student &stud) //用传引用的方式 输出原始信息 ,为防止原数据被修改用const进行限定 
{
   cout<<endl;
   cout<<"num="<<stud.num<<"\t";
   cout<<"name="<<stud.name<<"\t";     
   cout<<"sex="<<stud.sex<<"\t";
   cout<<"score="<<stud.score<<"\t"<<endl;      
} 

你可能感兴趣的:(struct,float)