You are on page 1of 13

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

LAB 8

Objective At the end of this lab activity, the students should be able to:

Implement the main concepts in object-oriented programming. Use single inheritance and multiple inheritance. Use new and delete operator.

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

EXERCISE 1 ( Single Inheritance ) Create a complete program based on the following criteria: (a) Create class Staff. (i) (ii) data members member functions : id, age, salary, nett_salary. : - void setdata() --- to get user input for all except nett_salary. (b) Create class Executive which inherits from class Staff. (i) (ii) data members member functions : OT_hrs, rate. : - void setExecutive() --- to get user input for OT_hrs and rate. - void cal_salary_exec() --- to calculate nett_salary Formula : nett_salary = salary + (OT_Hrs * rate). - void display --- display all information. (c) Create class Salesperson which inherits from class Staff. (i) (ii) data members member functions : units, bonus. : - void setSalesperson() --- to get user input for units. - void cal_salary_sales() --- to calculate nett_salary if units> 500 then bonus = Rm500.00 if units <= 500 then bonus = RM250.00 Formula : nett_salary = salary + bonus - void display --- display all information. (d) In main() (i) (ii) (iii) Ask the user to select between Executive and Salesperson. if the user selects Executive, create and object for class Executive and call the appropriate member functions. if the user selects Salesperson, create and object for class Salesperson and call the appropriate member functions.

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

SAMPLE OUTPUT

Enter [1] for Executive Enter Enter Enter Enter Enter Enter Name ID Age salary OT_hrs Rate : : : : Ali 1234 23 RM 2000.00 : 30 : RM 4.00 Ali 1234 23 RM 2000 30 RM 4 RM 2120

OR

[2] for Salesperson : 1

Name ID Age Salary OT Hours OT Rate Nett Salary

: : : : : : :

Press any key to continue

SAMPLE SOLUTION
#include<iostream> #include<string> using namespace std; class Staff { protected : string name; int id, age; float salary, nett_salary; public: void setdata() { cout<<"Enter Name : "; cin>>name; cout<<"Enter ID : "; cin>>id; cout<<"Enter Age : "; cin>>age; cout<<"Enter salary : RM "; cin>>salary; } }; class Executive : public Staff { private : int OT_hrs; float rate; public:void setExecutive() { cout<<"Enter OT_hrs cin>>OT_hrs; cout<<"Enter Rate cin>>rate; }

: "; : RM ";

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

void cal_salary_exec() { nett_salary = salary + (OT_hrs * rate); } void display() { cout<<"Name cout<<"ID cout<<"Age cout<<"Salary cout<<"OT Hours cout<<"OT Rate cout<<"Nett Salary } }; class Salesperson:public Staff { private : int units; float bonus; public : void setSalesperson() { cout<<"Enter Units cin>>units; }

: : : : : : :

"<<name<<endl; "<<id<<endl; "<<age<<endl; RM "<<salary<<endl; "<<OT_hrs<<endl; RM "<<rate<<endl; RM "<<nett_salary<<endl;

: ";

void cal_salary_sales() { if (units > 500) bonus = 500; else bonus = 250; nett_salary = salary + bonus; } void display() { cout<<"Name cout<<"ID cout<<"Age cout<<"Salary cout<<"Units cout<<"Bonus cout<<"Nett Salary } };

: : : : : : :

"<<name<<endl; "<<id<<endl; "<<age<<endl; RM "<<salary<<endl; "<<units<<endl; RM "<<bonus<<endl; RM "<<nett_salary<<endl;

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

void main() { int choice; cout<<"Enter [1] for Executive cin>>choice; OR [2] for Salesperson : ";

if (choice == 1) { Executive E; E.setdata(); E.setExecutive(); E.cal_salary_exec(); E.display(); } else if (choice == 2) { Salesperson S; S.setdata(); S.setSalesperson(); S.cal_salary_sales(); S.display(); } else cout<<"INVALID ENTRY..."<<endl; }

EXERCISE 2 ( Single Inheritance ) Create a complete program based on the following criteria: (a) Create class Inventory. (i) (ii) data members member functions : price, quantity, title. : - void menu() --- to display the menu int selection() --- return user selection to main(). void setdata() --- get user input for title, quantity and price. void print() --- print title, quantity and price. (b) Create class Books which inherits from class Inventory. (i) (ii) data members member functions : author, isbn, publisher. : - void setBookdata() --- to get user input for isbn, author and publisher. - void print_details() --- to print author, isbn, publisher Call function print() in class Inventory. Display payment (price*quantity) (c) Create class CD which inherits from class Inventory.

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

(i) (ii)

data members member functions

: singer, tracks. : - void setCDdata() --- get user input for singer and tracks. - void print_details() --- to print singer and tracks. Call function print() in class Inventory. Display payment (price*quantity)

(d)

In main() (i) (ii) (ii) Ask the user to select between Books or CD. Create 2 objects for class Books and class CD. Call the appropriate member functions from each class for each object.

SAMPLE OUTPUT

---------------------------------------MENU ---------------------------------------1. Books 2. CD ---------------------------------------Enter Your Selection : 2 Enter Title : Water Enter Quantity : 3 Enter Price : RM24.90 Enter Singer Name : Beegees Enter Tracks : 18 ---------------------------------------BILL ---------------------------------------Singer : Beegees Tracks : 18 Title : Water Quantity : 3 Price : RM24.9 Payment : RM74.7 Press any key to continue

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

SAMPLE SOLUTION

#include<iostream> #include<string> using namespace std; class Inventory { protected : float price; int quantity; string title; public : void menu() { cout<<"-----------------------------------"<<endl; cout<<" MENU "<<endl; cout<<"-----------------------------------"<<endl; cout<<" 1. Books "<<endl; cout<<" 2. CD "<<endl; cout<<"-----------------------------------"<<endl; cout<<"\n"; }

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

int selection() { int selection; cout<<"Enter Your Selection "<<endl; cin>>selection; return selection; } void setdata() { cout<<"Enter Title cin>>title; cout<<"Enter Quantity cin>>quantity; cout<<"Enter Price cin>>price; } void print() { cout<<" Title cout<<" Quantity cout<<" Price } }; class Books : public Inventory { private : string author, isbn, publisher; public : void setBookdata() { cout<<" Enter Author Name cin>>author; cout<<" Enter ISBN No cin>>isbn; cout<<" Enter Publisher cin>>publisher; }

: "; : "; : RM";

: "<<title<<endl; : "<<quantity<<endl; : RM"<<price<<endl;

: "; : "; : ";

void print_details() { cout<<"---------------------------------"<<endl; cout<<" BILL "<<endl; cout<<"---------------------------------"<<endl; cout<<" Author : "<<author<<endl; cout<<" ISBN : "<<isbn<<endl; cout<<" Publisher : "<<publisher<<endl; Inventory::print(); cout<<" Payment : RM"<<price*quantity<<endl; } };

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

class CD : public Inventory { private : string singer; int tracks; public : void setCDdata() { cout<<" Enter Singer Name cin>>singer; cout<<" Enter Tracks cin>>tracks; }

: "; : ";

void print_details() { cout<<"---------------------------------"<<endl; cout<<" BILL "<<endl; cout<<"---------------------------------"<<endl; cout<<" Singer : "<<singer<<endl; cout<<" Tracks : "<<tracks<<endl; Inventory::print(); cout<<" Payment : RM"<<price*quantity<<endl; } }; void main() { int choice; Inventory In; In.menu(); choice = In.selection(); if (choice ==1) { Books B; B.setdata(); B.setBookdata(); B.print_details(); } else { if (choice == 2) { CD C; C.setdata(); C.setCDdata(); C.print_details(); } else cout<<"Program terminated !!!....."<<endl; } }

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

EXERCISE 3 ( Multiple Inheritance ) Create a complete program based on the following criteria: (a) Create class Student. (i) (ii) (b) data members (protected): name, location, age. member functions : - constructor to display Student Details

Create class Marks. (i) (ii) data members (protected): marks[3], grade. member functions : - void mark_entry() --- get user input for marks. - void average() --- calculate average. Call function set_grade(..). - void set_grade(..) --- calculate grade. If average >= 80, grade = A If average >= 50, grade = B If average >= 0, grade = C

(c)

Create class Result which inherits from class Student and class Marks. (i) (ii) data members member functions : semester. : - void setStudent() --- get user input for name, location, age, semester - void display() --- display name, location, age,. Semester, all the marks and grade.

(d)

In main() (i) (ii) (ii) (iv) Create an object Stud1 of class Result using the new operator. Call setStudent(), mark_entry(), average() and display(). Delete the object. Repeat the steps above by getting the number of students in main() during runtime. ( here you have to use Dynamic Memory Allocation)

MULTIMEDIA UNIVERSITY

10

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

SAMPLE OUTPUT

----------------------------------------STUDENT DETAILS ----------------------------------------Enter Name : Ali Enter Location : Selangor Enter Age : 19 Enter Semester : 3 ----------------------------------------MARKS DETAILS ----------------------------------------Enter Marks : 23.3 Enter Marks : 89 Enter Marks : 77.5 ----------------------------------------RESULT SLIP ----------------------------------------Name : Ali Location : Selangor Age : 19 Semester : 3 Marks 1 : 23.3 Marks 2 : 89 Marks 3 : 77.5 Grade : C Press any key to continue

SAMPLE SOLUTION
#include<iostream> #include<string> using namespace std; class Student { protected : string name, location; int age; public : Student() { cout<<"----------------------------- "<<endl; cout<<" STUDENT DETAILS "<<endl; cout<<"----------------------------- "<<endl; } }; class Marks { protected : float mark[3]; char grade; public : void mark_entry() { cout<<"----------------------------- "<<endl; cout<<" MARKS DETAILS "<<endl; cout<<"----------------------------- "<<endl;

MULTIMEDIA UNIVERSITY

11

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

for(int i=0;i<3;i++) { cout<<"Enter Marks : "; cin>>mark[i]; } } void average() { double total=0, average=0; for(int i=0;i<3;i++) { total = total + mark[i]; } average = total / 3; set_grade(average); } void set_grade(double avg) { if (avg >= 80 && avg <= 100) grade = 'A'; else if (avg >= 50 && avg < 80) grade = 'B'; else grade = 'C'; } }; class Result : public Student, public Marks { private : int semester; public : void setStudent() { cout<<"Enter Name cin>>name; cout<<"Enter Location cin>>location; cout<<"Enter Age cin>>age; cout<<"Enter Semester cin>>semester; }

: "; : "; : "; : ";

void display() { cout<<"------------------------------ "<<endl; cout<<" RESULT SLIP "<<endl; cout<<"------------------------------ "<<endl; cout<<"Name : "<<name<<endl; cout<<"Location : "<<location<<endl; cout<<"Age : "<<age<<endl; cout<<"Semester : "<<semester<<endl; for(int i = 0; i<3; i++) { cout<<"Marks "<<i+1<<" : "<<mark[i]<<endl; } cout<<"Grade : "<<grade<<endl; } };

MULTIMEDIA UNIVERSITY

12

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 8

void main() { Result *Stud1; Stud1 = new Result; Stud1->setStudent(); Stud1->mark_entry(); Stud1->average(); Stud1->display(); delete Stud1; }

SOLUTION FOR (d)(IV)


void main() { int SIZE; cout<<"Enter Amount of Student : "; cin>>SIZE; Result *Stud; for (int i = 0; i < SIZE; i++) { Stud = new Result[SIZE];

Stud[i].setStudent(); Stud[i].mark_entry(); Stud[i].average(); Stud[i].display(); } delete [] Stud; }

MULTIMEDIA UNIVERSITY

13

You might also like