Object Oriented Programming Using C++ (CS - 103) : Lab Manual # 06
Object Oriented Programming Using C++ (CS - 103) : Lab Manual # 06
Lab Manual # 06
Registration# Name
Submitted by:
18-CS-13 Syed Sherafgan
Obtained Marks: 1
Page
Object Oriented Programming Using C++ (CS -103)
Q#1. Derive a class called employee2 from the employee class in the EMPLOY program in this chapter.
This new class should add a type double data item called compensation, and an enum type called period
to indicate whether the employee is paid hourly, weekly, or monthly. For simplicity you can change the
manager, scientist, and laborer classes so they are derived from employee2 instead of employee.
However, note that in many circumstances it might be more in the spirit of OOP to create a separate
base class called compensation and three new classes manager2, scientist2, and laborer2, and use
multiple inheritance to derive these three classes from the original manager, scientist, and laborer
classes and from compensation. This way none of the original classes needs to be modified. Write a
main() program to test the book and tape classes by creating instances of them, asking the user to fill in
data with getdata(), and then displaying the data with putdata().
Code:
1. #include<iostream>
2. using namespace std;
3. class employee{
4. protected:
5. string name;
6. int num;
7. public:
8. void getdata()
9. {
10. cout<<" Enter name";
11. cin>>name;
12. cout<<"enter num";
13. cin>>num;
14. }
15. void putdata()
16. {
17. cout<<"name"<<name;
18. cout<<"\n number"<<num;
19. }
20. };
21. class employee2:public employee{
22. protected:
23. int N;
24. double comp;
25. enum period{
26. monthly=1,
27. weekly=2,
28. hourly=3,
29. };period p;
30. public:
31. void getdata()
32. {
33. employee::getdata();
34. cout<<"Enter the compensation";
35. cin>>comp;
36. cout<<" Enter 1 for monthly payment\n 2 for weekly payment\n 3 for hourly
payment of salary ";
37. cin>>N;
38. }
39. void putdata()
40. {
41. employee::putdata();
42. cout<<"Compensation="<<comp;
43. if(N=1)
44. cout<<"The employee is paid monthly";
45. else if (N=2)
46. cout<<"The employee is paid weekly";
47. else if(N=3)
48. cout<<"The employee is paid hourly";
2
49. }
Page
50. };
51. class scientist:public employee2{
Object Oriented Programming Using C++ (CS -103)
52. protected:
53. int publications;
54. public:
55. void getdata()
56. {
57. employee2::getdata();
58. cout<<"enter no of pubication";
59. cin>>publications;
60. }
61. void putdata()
62. {
63. employee2::putdata();
64. cout<<"publications="<<publications;
65. }
66. };
67. class manager:public employee2{
68. protected:
69. string title;
70. float clubdues;
71. public:
72. void getdata()
73. {
74. employee2::getdata();
75. cout<<" Enter title";
76. cin>> title;
77. cout<<"\n Enter club dues";
78. cin>>clubdues;
79. }
80. void putdata()
81. {
82. employee2::putdata();
83. cout<<"Title=\t"<<title;
84. cout<<"\n clubdues= \t"<<clubdues;
85. }
86. };
87. class labour:public employee2{
88. };
89. int main()
90. {
91. manager m;
92. m.getdata();
93. m.putdata();
94. }
Output
3
Page
Object Oriented Programming Using C++ (CS -103)
Q2.Start with the publication, book, and tape classes of Exercise 1. Suppose you want to
add the date of publication for both books and tapes. From the publication class, derive a
new class called publication2 that includes this member data. Then change book and tape
so they are derived from publication2 instead of publication. Make all the necessary
changes in member functions so the user can input and output dates along with the other
data. For the dates, you can use the date class from Exercise 5 in Chapter 6, which stores
a date as three ints, for month, day, and year.
Code:
1. #include<iostream>
2. using namespace std;
3. class publication{
4. protected:
5. string title;
6. float price;
7. public:
8. void getdata()
9. {
10. cout<<" Enter the title of Book you need"<<endl;
11. cin>>title;
12. cout<<"\nEnter the price of required book\n";
13. cin>>price;
14. }
15. void putdata()
16. {
17. cout<<"book title= \t"<<title;
18. cout<<"\n book price =\t"<<price;
19. }
20. };
21. class publication2:public publication{
22. protected:
23. int day;
24. int month;
25. int year;
26. public:
27. void getdata()
28. {
29. publication::getdata();
30. cout<<"\n Enter the year of publication of required book";
31. cin>>month;
32. if(year>1500 && year<3000)
33. {
34. cout<<"\n Enter the month of publication";
35. cin>>month;
36. if(month>0 && month<=12)
37. {
38. cout<<"\n Enter the Day of publication";
39. cin>>day;
40. }
41. }
42. }
43. void putdata()
44. {
45. publication::putdata();
46. cout<<"\n Publication date is +\t"<<day<<","<<month<<","<<year<<",";
47. }
4
48. };
Page
Output
5
Page
Object Oriented Programming Using C++ (CS -103)
6
Page