Description
The student class contains private data members: Num,name,sex, public member functions: Input function Get_value () and Output function display (). Implement class Student1 with private inheritance, adding data members: AGE,ADDR, member functions: Get_value_1 () and Display_1 (). Enter the value of NUM,NAME,SEX,AGE,ADDR when the program runs, and call the output function to output the values of the above 5 data.
Input
Enter a value for NUM,NAME,SEX,AGE,ADDR
Output
The value of the output num,name,sex,age,addr
Sample Input
1001 Zhang M Shanghai
Sample Output
Num:1001name:zhangsex:mage:21address:shanghai
/* All rights reserved. * File Name: Test.cpp * Chen Dani * Completion Date: July 3, 2015 * version number: v1.0 */#include &L T;iostream> #include <cstring>using namespace Std;class student{public:void get_value () {Cin>> ;num>>name>>sex; } void Display () {cout<< "num:" <<num<<endl; cout<< "Name:" <<name<<endl; cout<< "Sex:" <<sex<<endl; }private:int num; Char name[10]; char sex;}; Class Student1:private Student{private:int age; String Addr;public:void get_value_1 () {get_value (); cin>>age>>addr; } void Display_1 () {display (); cout<< "Age:" <<age<<endl; cout<< "Address:" <<addr<<endl; }};int Main () {Student1 stud1; Stud1.get_value_1 (); Stud1.display_1 (); return 0;}
Learning experience: Private inheritance, you can inherit private members, but you cannot access private members directly, you can use a function to access. Keep trying!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
17th Week OJ Brush problem--problem e:c++ Exercise Input Output--Private inheritance