c++之路5:用类创建多个学生对象

来源:互联网 发布:中国电信网络传真系统 编辑:程序博客网 时间:2024/05/17 03:00

#include<iostream>
//#include<stdio.h>
#include<iomanip>
#include<string>
//#include<string.h>

using namespace std;
const int N=5;                                            //定义整形常量,意义是创建的最多学生对象为5个,这里定义常量是方便修改下文
class Student
{
 private:
     char name[10];
     char sex[5];
     int age;
 public:
  void Stu_set()                                                                            
   { 
         cout<<"请输入该学生的姓名 性别 年龄:"<<endl;
         cin>>name>>sex>>age;
   }
  void Stu_show()                                                                                  //显示创建学生对象信息
   {
         cout<<"姓名:"<<name<<"性别:"<<sex<<"年龄:"<<age<<endl; 
   }
};

int main()
{
    Student stu[N];
      int i=0;
      int kong;
      int n=0;
 do
 {                                             
    stu[i].Stu_set();
    i++;                              
    n=i;                                               //创建多少个对象n就为多少
    cout<<"继续输入请直接输入1,否则请输入0!"<<endl;                         //控制是否继续创建学生对象信息
    cin>>kong;
  
  
 }while(kong!=0);                                   //用do~while();循环来创建多个学生信息对象,输入0停止创建

 for(i=0;i<n;i++)
 stu[i].Stu_show();
 return 0;
}

0 0
原创粉丝点击