11th Week Practice Item 2.2-Student class to store monitor information (if the base class data member is changed to private)

Source: Internet
Author: User

/* *copyright (c) 2016, *all right reserved, school of computer and Control engineering, Yantai University. * File name: 77.cpp * Author: Dong Kai * Completion Date: May 6, 2016 * version number: v1.0 * * Problem Description: Please complete the definition of member function in class * Input Description: * Program output: Student's information */#include <iostream> using namespace Std;    Class Stu {public:stu (int n,string nam);    void display ();p rivate:int num; String name; };     Stu::stu (int n,string nam) {num=n; Name=nam;     } void Stu::d isplay () {cout<< "Study No.:" <<num<<endl; cout<< "Name:" <<name<<endl; }class studetail:public stu{public://Student NAM, study number n, live ad, his monitor nam1, study number n1 studetail (int n,string nam,int a,string ad,int N1    , string nam1);    void Show ();    void Show_monitor ();p rivate:stu Monitor;    int age; string addr;}; Studetail::studetail (int n,string nam,int n1,string nam1,int a,string AD): Stu (N,nam), monitor (N1,NAM1), age (a), addr (ad    {}void studetail::show () {cout<< "Student information:" <<endl;    Stu::d isplay ();    cout<< "Age:" <<age<<endl;   cout<< "Address:" <<addr<<endl; Cout<<endl;}    void Studetail::show_monitor () {cout<< "Monitor info:" <<endl; Monitor.display ();} int main () {//Student Wang Li, No. 10,010th, 19 years old, lives in Shanghai's Beijing Road, his monitor is Jason, study number 10001 Studetail s (10010, "Wang-li", "1", "Beijing Road,shanghai"    0001, "Li-sun");                       S.show ();                Output Student's data s.show_monitor (); Output sub-object data return 0;}

Summary of Knowledge points:

If you change the base class data member to private, you can take the following two methods:

1. Add two read number and name functions to the base class member function: int getnum{return num;} and string GetName {return name;}

In a derived class

void Studetail::show ()
When the member function is implemented, change to:

  1. void studetail::show ()
  2. {
  3.     cout<<" Student Information: " <<ENDL;&NBSP;&NBSP;
  4.     cout<<":  " <<getnum () <<endl;              //output num and name &NBSP;&NBSP;
  5.     cout<<" name:  " <<getname () <<endl;   
  6.     cout<<" Age:  " <<age<<endl;              //output age &NBSP;&NBSP;
  7.     cout<<" address:  " <<addr<<endl<< endl;      //output addr &NBSP;&NBSP;
  8. }  
2. Directly call the base class member function directly in the member function.

Learning experience:

To learn how to master some methods, flexible.

11th Week Practice Item 2.2-Student class to store monitor information (if the base class data member is changed to private)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.