Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // Qaiwan International University
- // SCSJ1023- Programming Technique II
- // Section 2
- // Semester 1, 2019/2020
- // Inheritance and Polymorphism
- // 11 December 2019
- ////////////////////////////////////////////////////////////////////////////////
- // Student Name : Zheer Ardalan
- // Student Matrix No: QU18-0029
- #include <iostream>
- #include <string>
- using namespace std;
- //Task 1 : Define class lecturer
- class Lecturer
- {
- private:
- string name;
- string dept;
- public:
- Lecturer();
- void set(string na,string de)
- {
- name = na;
- de = dept;
- }
- string getName()
- {
- return name;
- }
- string getDept()
- {
- return dept;
- }
- void setName(string nam){
- name = nam;
- }
- };
- //Task 2 : Define class Course
- class Course
- {
- private:
- string course;
- string code;
- public:
- set(string cou , string co)
- {
- course = cou;
- code = co;
- }
- string getNamecourse()
- {
- return course;
- }
- string getcodecourse()
- {
- return code;
- }
- virtual void print () = 0;
- virtual void read () = 0;
- };
- // Task 3 : Define class LecturedCourse that inherit Course
- class LecturedCourse :public Course
- {
- private:
- Lecturer *lec;
- public:
- void setLecturer(Lecturer *le1)
- {
- lec = le1 ;
- }
- Lecturer *getLecturer()
- {
- return lec;
- }
- void read ()
- {
- string code ,course;
- cout<<"Adding a new subject course:"<<endl;
- cout<<"Code =>";
- cin>>code;
- cout<<"Name =>";
- cin>>course;
- set(code,course);
- string lect , dep;
- cout<<"Lecturer =>";
- cin>>lect;
- cout<<"Department =>";
- cin>>dep;
- lec->set(lect,dep);
- }
- void print ()
- {
- cout<<"The list of all courses:"<<endl<<endl;
- cout<<"Course Name"<<"\t"<<getNamecourse()<<endl;
- cout<<"Lecturer"<<"\t"<<lec->getName()<<endl;
- cout<<"Department"<<"\t"<<lec->getDept()<<endl;
- }
- LecturedCourse(){};
- };
- // Task 4 : Define class NonLecturedCourse that inherit Course
- class NonLecturedCourse :public Course
- {
- private:
- int minCredit;
- string project;
- public:
- void setminCredit(int mi)
- {
- minCredit = mi;
- }
- int getMinCredit()
- {
- return minCredit;
- }
- void setproject(string pro)
- {
- project = pro;
- }
- string getproject()
- {
- return project;
- }
- void read ()
- {
- string c ,n;
- cout<<"Adding a new project course:";
- cout<<"Code =>";
- cin>>c;
- cout<<"Name =>";
- cin>>n;
- set(n,c);
- cout<<"Min Credit =>";
- cin>>minCredit;
- string pr;
- cout<<"Project Name =>";
- cin>>pr;
- setproject(pr);
- }
- void print ()
- {
- cout<<"Course Name"<<"\t"<<getNamecourse()<<endl;
- cout<<"Min Credit"<<"\t"<<getMinCredit()<<endl;
- cout<<"Project Name"<<"\t"<<getproject()<<endl;
- }
- NonLecturedCourse();
- };
- // Given Menu function
- int menu()
- {
- int choice;
- cout << endl;
- cout << "--------- Menu------------" << endl;
- cout << "1. Add subject" << endl;
- cout << "2. Add project" << endl;
- cout << "3. Print all subjects" << endl;
- cout << "4. Exit" << endl;
- cout << endl;
- cout << "Enter your choice => ";
- cin >> choice;
- cout << endl;
- return choice;
- }
- int main()
- {
- //Polymorphism Implementation
- Course *courses[10]; // Array of Courses that store all subjects
- int nc=0; // number of courses
- int choice=0;
- Course *newCourse;
- while (choice != 4){
- choice = menu();
- switch (choice){
- case 1: //"Adding a new subject course:
- newCourse = new LecturedCourse;
- newCourse->read(); // polymorphism
- courses[nc]=newCourse;
- nc++;
- break;
- case 2: // Adding courses
- newCourse = new NonLecturedCourse;
- newCourse->read();
- courses[nc]=newCourse;
- nc++;
- break;
- case 3: // List of subject courses
- cout << "Number of courses offered : " << nc << endl;
- cout << "The list of all courses: " << endl;
- for (int i=0; i<nc; i++)
- courses[i]->print() ;
- break;
- }
- }
- cout << "Program ends!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement