1. 程式人生 > >建立一個學生類(Student),包括學號成績,程式設計輸入和顯示學生的資訊。建立一個人類(Person),包含姓名、性別和年齡,並作為學生類的基類

建立一個學生類(Student),包括學號成績,程式設計輸入和顯示學生的資訊。建立一個人類(Person),包含姓名、性別和年齡,並作為學生類的基類

 #include<iostream.h>
class Person
{
 char name[10];
 char sex;
 int age;
public:
 void input()
 {
  cout<<"請輸入姓名:";
  cin>>name;
  cout<<"請輸入性別:";
  cin>>sex;
  cout<<"請輸入年齡:";
  cin>>age;
 }
 void display()
 {
  cout<<"姓名:"<<name<<",性別:"<<sex<<",年齡:"<<age<<endl;
 }
};
class Student:public Person
{
   char sno[10];
   int score;
public:
 void input()
 {
  Person::input();
  cout<<"請輸入學號:";
  cin>>sno;
  cout<<"請輸入成績:";
  cin>>score;
 }
 void display()
 {
  Person::display();
  cout<<"學號;"<<sno<<",成績:"<<score<<endl;
 }
};
void main()
{
 Student s1;
 s1.input();
 s1.display();
}