继承与派生

来源:互联网 发布:数据库市场占有率 2016 编辑:程序博客网 时间:2024/06/14 03:44


继承与派生

#include<iostream>

using namepsace std;

class student
{
 public:
  void display();
  void  get_value()
  {
   cin>>num>>name>>sex;
  }
 protected:
  int num;
  string name;
  char sex;
  
};
void student::display()
{
  cout<<"num="<<num<<endl;
  cout<<"name="<<name<<endl;
  cout<<"sex="<<sex<<endl;
}
class student1 :protected student
{
 public:
  void display1();
  void get_value1()
  {
   get_value();
   cin>>age>>addr;
  }
 private:
  int age;
  string addr;
};
void student1::display1()
{
  display();
  cout<<"age="<<age<<endl;
  cout<<"address="<<addr<<endl; 
}
int main()
{
 student1 stu;
 stu.get_value1();
 stu.display1();
 
 return 0;
}

0 0
原创粉丝点击