C++ experiment---the composition of the school (I)

The composition of the school (I)

Description

The school is made up of teachers and students. Now define Student, Teacher and School three classes to simulate a school.
The Student class has only one data member of type int, which represents the grade of the student; the Teacher class has only one data member of type double, which represents the teacher’s salary; the School class is composed of one Student class object and one Teacher class object combination. to make. Define their constructors and destructors so that the program can produce the output shown in the sample.

Input

Enter 4 lines. Lines 1 and 3 are two positive integers of type int; lines 2 and 4 are positive numbers of type double.

Output

See example.

Sample Input

3
1600.31
4
2451.34

Sample Output

A student grade 3 is created.
A teacher with salary 1600.31 is created.
A school is created.
A student grade 4 is created.
A teacher with salary 2451.34 is created.
A student grade 4 is created.
A teacher with salary 2451.34 is created.
A school is created.
A school is erased.
A teacher with salary 2451.34 is erased.
A student grade 4 is erased.
A teacher with salary 2451.34 is erased.
A student grade 4 is erased.
A school is erased.
A teacher with salary 1600.31 is erased.
A student grade 3 is erased.

Topic given procedure

int main()
{
    
    
    int g;
    double s;
    cin>>g>>s;
    School sch(g, s);
    cin>>g;
    Student stu(g);
    cin>>s;
    Teacher tea(s);
    School(g, s);
    return 0;
}

code

#include<iostream>

using namespace std;

class Student{
    
    
	int grade;
public:
	Student(int nn){
    
    
		grade=nn;
		cout<<"A student grade "<<grade<<" is created."<<endl;
	}
	~Student(){
    
    
		cout<<"A student grade "<<grade<<" is erased."<<endl;
	}
};

class Teacher{
    
    
	double salary;
public:
	Teacher(double ss){
    
    
		salary=ss;
		cout<<"A teacher with salary "<<salary<<" is created."<<endl;
	}
	
	~Teacher(){
    
    
		cout<<"A teacher with salary "<<salary<<" is erased."<<endl;
	}
};

class School{
    
    
	Student stu;
	Teacher teac;
public:
	School(int g,double s):stu(g),teac(s){
    
    
		cout<<"A school is created."<<endl;
	}
	
	~School(){
    
    
		cout<<"A school is erased."<<endl;
	}
};

int main()
{
    
    
    int g;
    double s;
    cin>>g>>s;
    School sch(g, s);
    cin>>g;
    Student stu(g);
    cin>>s;
    Teacher tea(s);
    School(g, s);
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115262292