C++ experiment---organizational structure of the university

The organizational structure of the university

Description

A university is composed of several colleges and departments, each of which has its own name and leadership. Define the Organization class, which has two string attributes, which are the name of an organization and the name of its leader; it has a show method to display information about the organization.

This category has 2 subcategories: College and Department. The display format of College's show method is:

Dean of $ is &

The display format of the show method of Department is:

Director of $ is &

In the above format, $ represents the name of College or Department, and & is the name of the corresponding leader .

Input

Enter multiple lines.

The first row N represents the number of affiliated institutions of a university.

Then there are N groups of inputs. Each group of input has 3 lines. The first line is 0 or 1,0 which means that this is a College, and 1 means that this is a Department.

The second line is the name of College or Department.

The third line is the corresponding leader's name. See the example for
Output
.
Sample Input

4
0
College of Information
Tom Zhang
1
Department of Software
Jack Li
0
College of Math
Mary Wang
1
Department of Computer
Fu Qi

Sample Output

Dean of College of Information is Tom Zhang
Director of Department of Software is Jack Li
Dean of College of Math is Mary Wang
Director of Department of Computer is Fu Qi

Title given code

int main()
{
    
    
    vector<Orgnization*> university;
    vector<Orgnization*>::iterator itr;
    int n, i, t;
    string str1, str2;
    cin >> n;
    for (i = 0; i < n; i++)
    {
    
    
        cin >> t;
        cin.ignore();
        getline(cin, str1);
        getline(cin, str2);
        if (t == 0)
            university.push_back(new College(str1, str2));
        else
            university.push_back(new Department(str1, str2));
    }
    for (itr = university.begin(); itr != university.end(); itr++)
        (*itr)->show();
    return 0;
}

code:

#include<iostream>
#include<string>
#include<vector>
using namespace std;


class Orgnization{
    
    
protected:
	string name;
	string leader;
public:
	virtual void show()=0;
};

class College:public Orgnization{
    
    
public:
	College(string N,string L){
    
    
		name=N;
		leader=L;
	}
	
	void show(){
    
    
		cout<<"Dean of "<<name<<" is "<<leader<<endl;
	}
};

class Department:public Orgnization{
    
    
public:
	Department(string N,string L){
    
    
		name=N;
		leader=L;
	}
	
	void show(){
    
    
		cout<<"Director of "<<name<<" is "<<leader<<endl;
	}
};


int main()
{
    
    
    vector<Orgnization*> university;
    vector<Orgnization*>::iterator itr;
    int n, i, t;
    string str1, str2;
    cin >> n;
    for (i = 0; i < n; i++)
    {
    
    
        cin >> t;
        cin.ignore();
        getline(cin, str1);
        getline(cin, str2);
        if (t == 0)
            university.push_back(new College(str1, str2));
        else
            university.push_back(new Department(str1, str2));
    }
    for (itr = university.begin(); itr != university.end(); itr++)
        (*itr)->show();
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115217883
Recommended