Questions and codes:
/* *copyright (c) 2016, Yantai University School of computer *all rights reserved. * File name: Zwj.cpp * Author: Zhang Weijing * Completion Date: May 5, 2016 * version number: v1.0 * * Problem Description: The student class that stores monitor information * Input Description: * Program output: */#include <iostream> #includ E<string> using namespace std; Class Stu{public:stu (int n,string nam); Base class constructor void display (); member function, output base class data member Protected://(*) Access rights are protected data member int num; Student study number string name; Student name}; Stu::stu (int n,string nam): num (n), name (NAM) {}class studetail:public Stu//Declaration derived class Studetail{public://Student Nam, school number n,a years old, lives ad , its monitor is NAM1, study number n1 studetail (int n,string nam,int a,string ad,int n1,string nam1); Derived class constructor void show (); member function, output student's information void Show_monitor (); member function, the output monitor information Private:stu monitor; The class monitor of the student, the monitor is the student, is the member of the Stu class int age; Student Age string Addr; Student address}; Studetail::studetail (int n, string nam, int A, string ad, int n1, string nam1): Stu (N,nam), monitor (N1,NAM1), age (a), addr (ad {}void Stu::d isplay () {cout<< "No.:" <<num<<endl; cout<< "Name:" <<name<<endl;} void Studetail::show () {cout<< "Student information:" <<endl; Display (); 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 Beijing Road, his monitor is Jason, study number 10001 Studetail s (10010, "Wang-li", 100, "Beijing Road,shanghai "Li-sun"); S.show (); Output Student information s.show_monitor (); Output monitor message return 0;}
Operation Result:
Summary of Knowledge points:
When you need to execute a constructor of a parameter in the base class to initialize the base class data, the derived class constructor should provide parameters for the base class constructor in the initialization list.
Learning experience:
Item 2-Class of students who store monitor information