Experiment 3 derived class and inheritance experiment

Posted by jplock on Tue, 08 Feb 2022 09:30:28 +0100

Experiment 3: derived class and inheritance experiment

1. Experimental purpose

(1) Master the declaration method of derived class and the definition method of derived class constructor;

(2) Master the access properties of base class members in derived classes under different inheritance methods;

(3) Master the execution order and construction rules of constructors and destructors under inheritance mode.

2. Experimental class hours: 6 class hours

3. Experimental content

(1) Write a data input and display program for students and teachers. Student data includes number, name, gender, age, department and grade, while Teacher data includes number, name, gender, age, professional title and department. It is required to design the input and output of number, name, gender and age into a class Person, which is used as the base class of student and Teacher.

int main()

{Student stu(2015001, "Li Ming", "male", 20, "computer", 90);

  Teacher teac;

............
    }

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
class A
{
    private:
    int bianhao;
    string name;
    string sex;
    int nianling;
    public:
    int getbianhao();
    string getname();
    string getsex();
    int getnianling();
	int outbianhao();
    string outname();
    string outsex();
    int outnianling();
};
string A::getsex()
{
	cout<<"scanf sex:";
	cin>>sex;
	return sex;
}
A::getbianhao()
{
    cout<<"scanf bianhao:";
    cin>>bianhao;
	return bianhao;
}
string A::getname()
{
    cout<<"scanf name:";
    cin>>name;
	return name;
}
A::getnianling()
{
    cout<<"scanf nianling:";
    cin>>nianling;
	return nianling;
}
A::outbianhao()
{
    cout<<"bianhao:"<<bianhao<<endl;
	return bianhao;
}
string A::outname()
{
    cout<<"name:"<<name<<endl;
	return name;
}
A::outnianling()
{
    cout<<"nian ling:"<<nianling<<endl;
	return nianling;
}
string A::outsex()
{
	cout<<"sex:"<<sex<<endl;
	return sex;
}

class s:public A
{
    private:
    string xibie;
    int score;
    public:
    string getxibie();
    int getscore();
	string outxibie();
    int outscore();
};
string s::getxibie()
{
    cout<<"scanf xibie:";
    cin>>xibie;
	return xibie;
}
s::getscore()
{
    cout<<"scanf score:";
    cin>>score;
	return score;
}
string s::outxibie()
{
    cout<<xibie;
	return xibie;
}
s::outscore()
{
    cout<<score;
	return score;
}
class t:public A
{
    private:
    string zhicheng;
    string bumen;
    public:
    string getzhicheng();
    string getbumen();
	string outzhicheng();
    string outbumen();
};
string t::getzhicheng()
{
    cout<<"scanf zhicheng:";
    cin>>zhicheng;
	return zhicheng;
}
string t::getbumen()
{
    cout<<"scanf bumen:";
    cin>>bumen;
	return bumen;
}
string t::outzhicheng()
{
    cout<<zhicheng;
	return zhicheng;
}
string t::outbumen()
{
    cout<<bumen;
	return bumen;
}
int main()
{
    s s1;
    t t1;
    s1.getbianhao();
    s1.getname();
    s1.getsex();
    s1.getnianling();
    s1.getxibie();
	s1.getscore();
	t1.getbianhao();
    t1.getname();
    t1.getsex();
    t1.getnianling();
	t1.getzhicheng();
	t1.getbumen();
    s1.outbianhao();
    s1.outname();
    s1.outsex();
    s1.outnianling();
    s1.outxibie();
	s1.outscore();
	t1.outbianhao();
    t1.outname();
    t1.outsex();
    t1.outnianling();
	t1.outzhicheng();
	t1.outbumen();
    system("pause");
	return 0;
}
/*Write a data input and display program for students and teachers. Student data include number, name, gender, age, department and grade,
Teacher data include number, name, gender, age, professional title and department.
It is required to design the input and output of number, name, gender and age into a class of Person,
As the base class of Student and Teacher.*/

(2) Read, edit, compile, debug and run the following programs as required.

(1) Read, edit, compile, debug and run the following programs, and write the running results of the programs.

  #include <iostream>

  #include <string>

  using namespace std;

Class MyArray / / declare a base class MyArray

  {

    public:

          MyArray(int leng); / / constructor

          ~MyArray(); / / destructor

          void Input(); / / member function of input data

          void Display(string); / / member function for outputting data

    protected:

          int *alist; / / store a group of integers in the base class

          int length; / / number of integers

  };

  MyArray::MyArray(int leng)

  { if (leng<=0)

          {    cout<<"error length";

                 exit(1);

          }

        alist=new int[leng];

        length=leng;

        if(alist==NULL)

        { cout<<"assign failure";

             exit(1);

        }

Cout < < "MyArray class object created."<< endl;

  }

  MyArray::~MyArray()

   { delete[] alist;

Cout < < "MyArray class object was revoked."<< endl;

   }

 

  void MyArray::Input()

{cout < < "please input" < < length < < "integers:";

        int i;

        int *p=alist;

        for(i=0;i<length;i++,p++) cin>>*p;

 }

 

  void MyArray::Display(string str)

 { int i;

       int *p=alist;

Cout < < STR < < length < < "integers:";

       for(i=0;i<length;i++,p++)  cout<<*p<<" ";

       cout<<endl;

 }

 

 int main()

{        MyArray a(5);

      a.Input();

a.Display("display entered");

      return 0;

}

(2) Declare a class SortArray, inherit the class MyArray, and define a function sort in this class, which has the function of sorting the input integers from small to large.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class MyArray              //Declare a base class MyArray
{
    public:
	    MyArray(int leng);      //Constructor
        MyArray();
	    ~MyArray();           //Destructor
	    void Input();           //Member function of input data
	    void Display(string);    //Member function of output data
    protected:
	    int *alist;             //A set of integers is stored in the base class
	    int length;            //Number of integers
};
MyArray::MyArray()
{}
MyArray::MyArray(int leng)
{ if (leng<=0)
	    {	cout<<"error length";
		    exit(1);
	    }
	  alist=new int[leng];
	  length=leng;
	  if(alist==NULL)
	  { cout<<"assign failure";
		exit(1);
	  }
	  cout<<"MyArray Class object created."<<endl;
}
MyArray::~MyArray()
{
	   cout<<"MyArray Class object was revoked."<<endl;
}

void MyArray::Input()
{  cout<<"Please enter from the keyboard"<<length<<"Integer:";
	  int i;
	  int *p=alist;
	  for(i=0;i<length;i++,p++) 	cin>>*p;
}
void MyArray::Display(string str)
{ int i;
	 int *p=alist;
	 cout<<str<<length<<"Integer:";
	 for(i=0;i<length;i++,p++)  cout<<*p<<" ";
	 cout<<endl;
}
class SortArray:public MyArray
{
    public:
    SortArray(int length):MyArray(length)
    {
        cout<<"Object created:"<<endl;
    }
    virtual ~SortArray();
    int sort();
};
SortArray::~SortArray()
{
    cout<<"SortArray Object is revoked:"<<endl;
}
int SortArray::sort()
{
    int i,j;
    int *p;
    for(i=0;i<length;i++)
    {
        p=alist;
        for(j=0;j<length-i-1;j++,p++)
        {
            if(*p>*(p+1))
            swap(*p,*(p+1));
        }
    }
}
int main()
{ 	SortArray a(5);
	a.Input();
	a.Display("Displays the entered");
    a.sort();
    a.Display("Show after sorting:");
	return 0;
}

(3) Declare a class AverArray, inherit the class MyArray, and define a function aver in this class, which has the function of calculating the average value of input integers.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class MyArray              //Declare a base class MyArray
{
    public:
	    MyArray(int leng);      //Constructor
        MyArray();
	    ~MyArray();           //Destructor
	    void Input();           //Member function of input data
	    void Display(string);    //Member function of output data
    protected:
	    int *alist;             //A set of integers is stored in the base class
	    int length;            //Number of integers
};
MyArray::MyArray()
{}
MyArray::MyArray(int leng)
{ if (leng<=0)
	    {	cout<<"error length";
		    exit(1);
	    }
	  alist=new int[leng];
	  length=leng;
	  if(alist==NULL)
	  { cout<<"assign failure";
		exit(1);
	  }
	  cout<<"MyArray Class object created."<<endl;
}
MyArray::~MyArray()
{
	   cout<<"MyArray Class object was revoked."<<endl;
}

void MyArray::Input()
{  cout<<"Please enter from the keyboard"<<length<<"Integer:";
	  int i;
	  int *p=alist;
	  for(i=0;i<length;i++,p++) 	cin>>*p;
}
void MyArray::Display(string str)
{ int i;
	 int *p=alist;
	 cout<<str<<length<<"Integer:";
	 for(i=0;i<length;i++,p++)  cout<<*p<<" ";
	 cout<<endl;
}
class AverArray:public MyArray
{
    public:
    AverArray(int length):MyArray(length)
    {
        cout<<"Object created:"<<endl;
    }
    virtual ~AverArray();
    int aver();
};
AverArray::~AverArray()
{
    cout<<"AverArray Object is revoked:"<<endl;
}
int AverArray::aver()
{
    int i,j;
    int *p;
    double sum=0,ave;
    p=alist;
    for(i=0;i<length;i++,p++)
    {
        sum=sum+*p;
    }
    ave=sum/length;
    cout<<"average:"<<ave<<endl;
}
int main()
{ 	
    AverArray a(5);
	a.Input();
	a.Display("Displays the entered");
    a.aver();
	return 0;
}

(4) Declare a class ASArray, which inherits the class MyArray, and contains the functions of the above functions sort and aver.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class MyArray              //Declare a base class MyArray
{
    public:
	    MyArray(int leng);      //Constructor
        MyArray();
	    ~MyArray();           //Destructor
	    void Input();           //Member function of input data
	    void Display(string);    //Member function of output data
    protected:
	    int *alist;             //A set of integers is stored in the base class
	    int length;            //Number of integers
};
MyArray::MyArray()
{}
MyArray::MyArray(int leng)
{ if (leng<=0)
	    {	cout<<"error length";
		    exit(1);
	    }
	  alist=new int[leng];
	  length=leng;
	  if(alist==NULL)
	  { cout<<"assign failure";
		exit(1);
	  }
	  cout<<"MyArray Class object created."<<endl;
}
MyArray::~MyArray()
{
	   cout<<"MyArray Class object was revoked."<<endl;
}

void MyArray::Input()
{  cout<<"Please enter from the keyboard"<<length<<"Integer:";
	  int i;
	  int *p=alist;
	  for(i=0;i<length;i++,p++) 	cin>>*p;
}
void MyArray::Display(string str)
{ int i;
	 int *p=alist;
	 cout<<str<<length<<"Integer:";
	 for(i=0;i<length;i++,p++)  cout<<*p<<" ";
	 cout<<endl;
}
class ASArray:public MyArray
{
    public:
    ASArray(int length):MyArray(length)
    {
        cout<<"Object created:"<<endl;
    }
    virtual ~ASArray();
    int aver();
    int sort();
};
ASArray::~ASArray()
{
    cout<<"AverArray Object is revoked:"<<endl;
}
int ASArray::sort()
{
    int i,j;
    int *p;
    for(i=0;i<length;i++)
    {
        p=alist;
        for(j=0;j<length-i-1;j++,p++)
        {
            if(*p>*(p+1))
            swap(*p,*(p+1));
        }
    }
}
int ASArray::aver()
{
    int i,j;
    int *p;
    double sum=0,ave;
    p=alist;
    for(i=0;i<length;i++,p++)
    {
        sum=sum+*p;
    }
    ave=sum/length;
    cout<<"average:"<<ave<<endl;
}
int main()
{ 	
    ASArray a(5);
	a.Input();
	a.Display("Displays the entered");
    a.sort(); 
    a.Display("Show sorted");
    a.aver();
	return 0;
}

(3) People class is the virtual base class of Teacher class and cadre class Cader.

Data members of People class: name, age, sex, add, Tel

Data members of Teacher class: name, age, sex, add, Tel and title

Data members of Cader class: name, age, sex, add, Tel, post

Teacher_Cader class is a derived class from teacher class and cader class, and a new data member waves (salary) is added

 

int main()

{Teacher_Cader ob1("Li Ming", 30, "male", "Huaibei Normal University", 3803333, "lecturer", "section chief", 4500), ob2;

  ................................

/*People Class is the virtual base class of Teacher class and cadre class Cader.
People Data members of class: name, age, sex, add, Tel
Teacher Data members of class: name, age, sex, add, Tel, title
Cader Data members of class: name, age, sex, add, Tel, post
Teacher_Cader Class is a derived class of Teacher class and Cader class, and a new data member, waves (salary), is added

int main()
{ Teacher_Cader  ob1("Li Ming, 30, "male", "Huaibei Normal University", 3803333, "lecturer", "section chief", 4500), ob2;
*/
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
class Teacher
{
    private:
    string name;//full name
    int age;//Age
    string sex;//Gender
    string add;//address
    string tel;//Telephone
    string title;//title
    public:
    Teacher();
    int input_teacher(); 
    void ouput_teacher(); 
    Teacher(string name,int age,string sex,string add,string tel,string title)
    {
    cout<<"Teacher Object built successfully"<<endl;
    }
    ~Teacher();
};
int Teacher::input_teacher()
{
	cout<<"Teacher name:";
	cin>>name;
	cout<<"Age:";
	cin>>age;
	cout<<"Gender:";
	cin>>sex;
	cout<<"address:";
	cin>>add;
	cout<<"Telephone:";
	cin>>tel;
	cout<<"title:"; 
	cin>>title;
	return 0;
}
void Teacher::ouput_teacher()
{
	cout<<"Teacher name:"<<name<<endl;
	cout<<"Age:"<<age<<endl;
	cout<<"Gender:"<<sex<<endl;
	cout<<"address:"<<add<<endl;
	cout<<"Telephone:"<<tel<<endl;
	cout<<"title:"<<title<<endl;
}
Teacher::Teacher()
{
    cout<<"Teacher Object built successfully"<<endl;
}
Teacher::~Teacher()
{
    cout<<"Teacher Object revocation succeeded"<<endl;
}
class Cader
{
    private:
    string name;//Cadre name
    int age;//Age
    string sex;//Gender
    string add;//address
    string tel;//Telephone
    string post;//post
    public:
    Cader(string name,int age,string sex,string add,string tel,string post)
    {
    	cout<<"Cader Object built successfully"<<endl;
    }
    Cader();
    ~Cader();
    int input_cader();
    void ouput_cader();
};
int Cader::input_cader()
{
	cout<<"Cadre name:";
	cin>>name;
	cout<<"Age:";
	cin>>age;
	cout<<"Gender:";
	cin>>sex;
	cout<<"address:";
	cin>>add;
	cout<<"Telephone:";
	cin>>tel;
	cout<<"post:"; 
	cin>>post;
	return 0;
}
void Cader::ouput_cader()
{
	cout<<"Cadre name:"<<name<<endl;
	cout<<"Age:"<<age<<endl;
	cout<<"Gender:"<<sex<<endl;
	cout<<"address:"<<add<<endl;
	cout<<"Telephone:"<<tel<<endl;
	cout<<"post:"<<post<<endl;
}
Cader::Cader()
{
    cout<<"Cader Object built successfully"<<endl;
}
Cader::~Cader()
{
    cout<<"Cader Object revocation succeeded"<<endl;
}
class People:virtual public Teacher,virtual public Cader
{
	private:
    string name;//full name
    int age;//Age
    string sex;//Gender
    string add;//address
    string tel;//Telephone
    public:
    People();
    ~People();
    People(string name,int age,string sex,string add,string tel,string title,string post):Teacher(name,age,sex,add,tel,title),Cader(name,age,sex,add,tel,post)
    {
		cout<<"People Object built successfully"<<endl;
    }
    int input_people();
    void output_people();
};
int People::input_people()
{
	cout<<"full name:";
	cin>>People::name;
	cout<<"Age:";
	cin>>People::age;
	cout<<"Gender:";
	cin>>People::sex;
	cout<<"address:";
	cin>>People::add;
	cout<<"Telephone:";
	cin>>People::tel;
	return 0;
}
void People::output_people()
{
	cout<<"full name:"<<People::name<<endl;
	cout<<"Age:"<<People::age<<endl;
	cout<<"Gender:"<<People::sex<<endl;
	cout<<"address:"<<People::add<<endl;
	cout<<"Telephone:"<<People::tel<<endl;
}
People::People()
{
	cout<<"People Object built successfully"<<endl;
}
People::~People()
{
	cout<<"People Object revocation succeeded"<<endl;
}
class Teacher_Cader:public Teacher,public Cader
{
    private:
    int wages;//wages
    public:
    Teacher_Cader(string name,int age,string sex,string add,string tel,string title,string post):Teacher(name,age,sex,add,tel,title),Cader(name,age,sex,add,tel,post)
    {
    	cout<<"Teacher_Cader Object built successfully"<<endl;
    }
    ~Teacher_Cader();
    Teacher_Cader();
};
Teacher_Cader::Teacher_Cader()
{
    cout<<"Teacher_Cader Object built successfully"<<endl;
}
Teacher_Cader::~Teacher_Cader()
{
    cout<<"Teacher_Cader Object revocation succeeded"<<endl;
}
int main()
{ 
    Teacher_Cader ob1("Li Ming",30,"male","Huaibei normal university ","3803333","lecturer","Section chief"),ob2;
    People p1;
    ob2.input_cader();
    ob2.input_teacher();
    ob1.ouput_cader();
    ob2.ouput_teacher();
    p1.input_people();
    p1.output_people();
    system("pause");
    return 0;
}

 

Topics: C++ Programming Class