C++实验---家教课程

家教课程

Description

老师都是穷人,所以需要经常去外面搞点兼职啥的。可是除了上课,啥也不会啊。所以就只好做家教了。现在请利用面向对象的思想设计这样一个系统。具有如下类:

Person类:有一个string类型的属性,表明对象的名字。是Student和Teacher的父类。

Student类:是Person类的子类,拥有一个int类型的属性,表明对象的序号。

Teacher类:是Person类的子类,拥有一个string类型的属性,表明对象的职称。

Course类:是一个组合类,有1个Teacher类的对象、1个Student类型的对象,以及一个string类型的属性(表明对象的名称)组成。

请定义上述类的构造函数和析构函数,并在函数中输出相应的字符串。具体格式请参照样例输出。

Input

输入5行,前4个是4个字符串,分别是老师的名字、学生的名字、老师的职称、课程的名字。最后一行是一个整数,表示学生的序号。

Output

见样例~

Sample Input

Tom
Jack
Prof
C++
10

Sample Output

Person Tom is created.
Person Jack is created.
Person Tom is created.
Teacher Tom with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Person Jack is created.
Teacher Jack with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Course C++ is created.
Course C++ is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Jack with position Prof is erased.
Person Jack is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Tom with position Prof is erased.
Person Tom is erased.
Person Jack is erased.
Person Tom is erased.

题目给定代码

int main()
{
    
    
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

注意:这道题样例给的十分奇怪,再Coures的对象里,Jack即是老师又是学生,十分离谱,这就导致传递参数时s1卵用没有,,,哎,这不是面向对象编程,完全就是面向样例编程啊~~~~
code:

#include<iostream>

using namespace std;

class Person{
    
    
protected:
	string name;
public:
	Person(string n){
    
    
		name=n;
		cout<<"Person "<<name<<" is created."<<endl;
	}	
	
	virtual ~Person(){
    
    
		cout<<"Person "<<name<<" is erased."<<endl;
	}
};

class Student:public Person{
    
    
 	int id;
public:
	Student(string n,int i):Person(n){
    
    
		id=i;
		cout<<"Student "<<name<<" with id "<<id<<" is created."<<endl;
	}
	
	~Student(){
    
    
		cout<<"Student "<<name<<" with id "<<id<<" is erased."<<endl;
	}
};

class Teacher:public Person{
    
    
	string position;
public:
	Teacher(string n,string p):Person(n){
    
    
		position=p;
		cout<<"Teacher "<<name<<" with position "<<position<<" is created."<<endl;
	}
	
	~Teacher(){
    
    
		cout<<"Teacher "<<name<<" with position "<<position<<" is erased."<<endl;
	}
};

class Course{
    
    
	Teacher t;
	Student s;
	string Name;
public:
	Course(string name_t,string name_s,string pos,string N,int i):t(name_s,pos),s(name_s,i){
    
    
		Name=N;
		cout<<"Course "<<Name<<" is created."<<endl;
	}
	
	~Course(){
    
    
		cout<<"Course "<<Name<<" is erased."<<endl;
	}
};

int main()
{
    
    
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/timelessx_x/article/details/115399187