Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Qaiwan International University
  3. // SCSJ1023- Programming Technique II
  4. // Section 2
  5. // Semester 1, 2019/2020
  6. // Inheritance and Polymorphism
  7. // 11 December 2019
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Student Name : Zheer Ardalan
  10. // Student Matrix No: QU18-0029
  11.  
  12. #include <iostream>
  13. #include <string>
  14.  
  15.  
  16. using namespace std;
  17.  
  18. //Task 1 : Define class lecturer
  19. class Lecturer
  20. {
  21. private:
  22. string name;
  23. string dept;
  24. public:
  25. Lecturer();
  26. void set(string na,string de)
  27. {
  28. name = na;
  29. de = dept;
  30. }
  31.  
  32. string getName()
  33. {
  34. return name;
  35. }
  36.  
  37. string getDept()
  38. {
  39. return dept;
  40. }
  41.  
  42. void setName(string nam){
  43. name = nam;
  44. }
  45. };
  46.  
  47. //Task 2 : Define class Course
  48. class Course
  49. {
  50. private:
  51. string course;
  52. string code;
  53. public:
  54. set(string cou , string co)
  55. {
  56. course = cou;
  57. code = co;
  58. }
  59. string getNamecourse()
  60. {
  61. return course;
  62. }
  63.  
  64. string getcodecourse()
  65. {
  66. return code;
  67. }
  68. virtual void print () = 0;
  69. virtual void read () = 0;
  70.  
  71. };
  72.  
  73. // Task 3 : Define class LecturedCourse that inherit Course
  74.  
  75. class LecturedCourse :public Course
  76. {
  77. private:
  78. Lecturer *lec;
  79. public:
  80.  
  81.  
  82.  
  83. void setLecturer(Lecturer *le1)
  84. {
  85. lec = le1 ;
  86. }
  87. Lecturer *getLecturer()
  88. {
  89. return lec;
  90. }
  91. void read ()
  92. {
  93. string code ,course;
  94. cout<<"Adding a new subject course:"<<endl;
  95. cout<<"Code =>";
  96. cin>>code;
  97. cout<<"Name =>";
  98. cin>>course;
  99. set(code,course);
  100.  
  101. string lect , dep;
  102. cout<<"Lecturer =>";
  103. cin>>lect;
  104. cout<<"Department =>";
  105. cin>>dep;
  106. lec->set(lect,dep);
  107. }
  108. void print ()
  109. {
  110. cout<<"The list of all courses:"<<endl<<endl;
  111. cout<<"Course Name"<<"\t"<<getNamecourse()<<endl;
  112. cout<<"Lecturer"<<"\t"<<lec->getName()<<endl;
  113. cout<<"Department"<<"\t"<<lec->getDept()<<endl;
  114. }
  115. LecturedCourse(){};
  116. };
  117.  
  118. // Task 4 : Define class NonLecturedCourse that inherit Course
  119.  
  120. class NonLecturedCourse :public Course
  121. {
  122. private:
  123. int minCredit;
  124. string project;
  125. public:
  126. void setminCredit(int mi)
  127. {
  128. minCredit = mi;
  129. }
  130. int getMinCredit()
  131. {
  132. return minCredit;
  133. }
  134.  
  135. void setproject(string pro)
  136. {
  137. project = pro;
  138. }
  139.  
  140. string getproject()
  141. {
  142. return project;
  143. }
  144.  
  145. void read ()
  146. {
  147. string c ,n;
  148. cout<<"Adding a new project course:";
  149. cout<<"Code =>";
  150. cin>>c;
  151. cout<<"Name =>";
  152. cin>>n;
  153. set(n,c);
  154.  
  155. cout<<"Min Credit =>";
  156. cin>>minCredit;
  157.  
  158. string pr;
  159. cout<<"Project Name =>";
  160. cin>>pr;
  161. setproject(pr);
  162. }
  163. void print ()
  164. {
  165. cout<<"Course Name"<<"\t"<<getNamecourse()<<endl;
  166. cout<<"Min Credit"<<"\t"<<getMinCredit()<<endl;
  167. cout<<"Project Name"<<"\t"<<getproject()<<endl;
  168. }
  169. NonLecturedCourse();
  170. };
  171.  
  172. // Given Menu function
  173.  
  174. int menu()
  175. {
  176. int choice;
  177.  
  178. cout << endl;
  179. cout << "--------- Menu------------" << endl;
  180. cout << "1. Add subject" << endl;
  181. cout << "2. Add project" << endl;
  182. cout << "3. Print all subjects" << endl;
  183. cout << "4. Exit" << endl;
  184.  
  185. cout << endl;
  186. cout << "Enter your choice => ";
  187. cin >> choice;
  188. cout << endl;
  189.  
  190. return choice;
  191. }
  192.  
  193.  
  194. int main()
  195. {
  196. //Polymorphism Implementation
  197. Course *courses[10]; // Array of Courses that store all subjects
  198. int nc=0; // number of courses
  199. int choice=0;
  200. Course *newCourse;
  201. while (choice != 4){
  202. choice = menu();
  203.  
  204. switch (choice){
  205. case 1: //"Adding a new subject course:
  206. newCourse = new LecturedCourse;
  207. newCourse->read(); // polymorphism
  208. courses[nc]=newCourse;
  209. nc++;
  210. break;
  211.  
  212. case 2: // Adding courses
  213.  
  214. newCourse = new NonLecturedCourse;
  215. newCourse->read();
  216. courses[nc]=newCourse;
  217. nc++;
  218. break;
  219.  
  220. case 3: // List of subject courses
  221. cout << "Number of courses offered : " << nc << endl;
  222. cout << "The list of all courses: " << endl;
  223. for (int i=0; i<nc; i++)
  224. courses[i]->print() ;
  225. break;
  226.  
  227.  
  228. }
  229. }
  230.  
  231. cout << "Program ends!" << endl;
  232. return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement