继承与派生[练习]

 1 #include<iostream>

 2 #include<string>

 3 using namespace std;

 4 class stud

 5 {

 6     public:

 7     stud(int xh=150209,string xm="kuangdaoyizhimei");

 8     ~stud()

 9     {

10         cout<<"xigou"<<endl;

11     }

12     void shuchu();

13     void shuru();

14     private:

15     int xuehao ;

16     string xingming;

17 };

18 stud::stud(int xh,string xm)

19 {

20     xuehao=xh;

21     xingming=xm;

22     cout<<"gouzao"<<endl;

23 }

24 void stud::shuru()

25 {

26     cin>>xuehao>>xingming;

27 }

28 void stud::shuchu()

29 {

30     cout<<"xuehao:"<<xuehao<<endl<<"xingming:"<<xingming<<endl;

31 }

32 class paisheng:public stud

33 {

34     public:

35     paisheng(int,int ,string);

36     ~paisheng();

37     void shuchu();

38     void shuru();

39     private:

40     int nianling;

41 };

42 paisheng::paisheng(int nl,int xh,string xm):stud(xh,xm)

43 {

44     nianling=nl;

45     cout<<"paishenggouzao"<<endl;

46 }

47 paisheng::~paisheng()

48 {

49     cout<<"paishengxigou"<<endl;

50 }

51 void paisheng::shuru()

52 {

53     cin>>nianling;

54 }

55 void paisheng::shuchu()

56 {

57     cout<<"nianling:"<<nianling<<endl;

58     stud::shuchu();

59 }

60 class paisheng1:public paisheng

61 {

62     public:

63     void shuchu();

64     paisheng1(int nl,int xh,string xm,string xb);

65     ~paisheng1();

66     private:

67     string xingbie;

68 };

69 paisheng1::paisheng1(int nl,int xh,string xm,string xb):paisheng(nl,xh,xm)

70 {

71     cout<<"paisheng1gouzao"<<endl;

72     xingbie=xb;

73 }

74 paisheng1::~paisheng1()

75 {

76     cout<<"paisheng1xigou"<<endl;

77 }

78 void paisheng1::shuchu()

79 {

80     cout<<"xingbie:"<<xingbie<<endl;

81     paisheng::shuchu();

82 }

83 int main()

84 {

85     paisheng1 std(20,816249,"zhengrongjun","nan");

86     std.shuchu();

87     return 0;

88 }
View Code

 

你可能感兴趣的:(继承)