5513,
Topic content:
P140 Example 5.2 defines the person class, which contains private data members age and constructor, member functions setage and show functions. Define the derived class student private inheritance person, the derived class contains private data members critit and constructor, member function setage_cre and show function. The derived class object stu1 is defined in the main function, and the initial values are 19 and 166. Use the object to call the member function setage_cre, the actual parameters are 20 and 168. The output is the member value of the stu1 object.
Input and output description:
Output: age is 20 credit is 168#include<iostream> using namespace std; class person{ protected: int age; public: person(int age){ this->age=age; } void setage(int age){ this->age=age; } void show(){ cout<<"age is "<<age<<endl; } }; class student :public person{ protected: int credit; public: student(int credit,int age1) : person(age1){ this->credit=credit; } void setage_cre(int credit,int age){ this->credit=credit; this->age=age; } void show(){ person::show(); cout<<"credit is "<<credit<<endl; } }; int main(){ student stu1(166,19); stu1.setage_cre(168,20); stu1.show(); }
5518,
Topic content:
Define base class B with private data member i, define constructor and destructor, and include output statements to ensure that they are called. There is also a member function that outputs the value of i.
Define the derived class D with private data member j, define the constructor and destructor, and include output statements to ensure that they are called. There is also a member function that outputs the value of j.
The main function defines the derived class object obj with initial values of 50 and 60. Output the value of the obj data member.
Input and output description:
Output: c base c derived 60 50 d derived d base
#include<iostream> using namespace std; class B{ protected: int i; public: B(int i){ this->i=i; cout<<"c base"<<endl; } ~B(){ cout<<"d base"<<endl; } void output(){ cout<<i<<endl; } }; class D : public B{ protected: int j; public: D(int i,int j):B(i){ this->j=j; cout<<"c derived"<<endl; } ~D(){ cout<<"d derived"<<endl; } void output(){ B::output(); cout<<j<<endl; } }; int main(){ D obj(60,50); obj.output(); }
5519,
Topic content:
P158 Example 5.11
Define base classes base1 and base2, which contain private data members x and y, respectively. Both constructors and member functions must be defined to obtain the value of the data member.
Define the derived class derived, which inherits base1 publicly and inherits base2 privately. Contains data member z. Define constructors, and member functions.
The derived class object obj is defined in the main function, and the initial values are 1, 3, and 5. Implements the output of the data member.
Input and output description:
Output: x=1 y=3 z=5
#include<iostream> using namespace std; class base1{ protected: int x; public: base1(int x){ this->x=x; } void output(){ cout<<"x="<<x<<endl; } }; class base2{ protected: int y; public: base2(int y){ this->y=y; } void output(){ cout<<"y="<<y<<endl; } }; class derived :public base1,private base2{ protected: int z; public: derived(int x,int y,int z):base1(x),base2(y){ this->z=z; } void output(){ base1::output(); base2::output(); cout<<"z="<<z<<endl; } }; int main(){ derived obj(1,3,5); obj.output(); }
5520,
Topic content:
P165 Example 5.15
Define the virtual base class base, which contains a data member a and a constructor.
The derived classes base1 and base2 that define the virtual base class base contain data members b, c and constructors respectively.
The derived class derived that defines base1 and base2 contains data member d and a constructor.
Requires an output statement in the constructor to know if it is called.
The derived object obj is defined in the main function.
Input and output description:
Output: c base c base1 c base2 c derive
#include<iostream> using namespace std; class base{ protected: int a; public: base(int a){ this->a=a; cout<<"c base"<<endl; } base(){ cout<<"c base"<<endl; } }; class base1:virtual public base{ protected: int b; public: base1(int a,int b):base(a){ this->b=b; cout<<"c base1"<<endl; } }; class base2:virtual public base{ protected: int c; public: base2(int a,int c):base(a){ this->c=c; cout<<"c base2"<<endl; } }; class derived :public base1,public base2{ protected: int d; public: derived(int a1,int a2,int b,int c,int d):base1(a1,b),base2(a2,c){ this->a=a; this->d=d; cout<<"c derived"<<endl; } }; int main(){ derived obj(1,2,3,4,5); }