DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
LAB 3
Objective
At the end of this this lab activity, the students should be be able to:
•
Implement the main concepts in object-oriented programming.
•
Use control structures.
•
Create classes and objects.
•
Create arrays of objects.
MULTIMEDIA UNIVERSITY
1
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
EXERCISE 1 (a)
As a programmer, you are requested by A-Plus Tuition Center to develop a simple program to enter and display students’ details. You are required to use classes for this exercise. (For this exercise, you are required to enter information for one student only)
Suggested output:
-------------------------------WELCOME TO A-PLUS TUITION CENTER -------------------------------Enter Name : Usha Vellappan Enter Location : Melaka Enter IC : 750217016680 Enter age : 30 -------------------------------STUDENT INFOMATION -------------------------------Name : Usha Vellappan IC : 750217016680 Location : Melaka Age : 30 Press any key to continue
STEPS 1.
(i)
Create a class called Student.
(ii)
The data members are : name(char),
location(char),
ic(char)
and
age(int). All data members must be declared as private. (iii)
The m ember f unctions a re : void set_data() and void print(). All member functions must be declared as public. class Student { private : char name[30],location[50], ic[14]; int age; public : void set_data(); void print();
2.
In member function set_data(), get input from the user and set the values to the data members. void set_data() { cout<<"Enter Name : "; cin.getline(name, 30); cout<<"Enter Location : "; cin.getline(location, 50); cout<<"Enter IC : "; cin.getline(ic, 13); cout<<"Enter age : "; cin>>age; }
MULTIMEDIA UNIVERSITY
2
DCS 5088 OBJECT ORIENTED PROGRAMMING
3.
LAB 3
In member function print(), display all the information on screen. void print() { cout<<"Name cout<<"IC cout<<"Location cout<<"Age }
: : : :
"<
4.
In function main(), create an object of class Student called S1.
5.
From main(), call both member functions set_data() and print().
6.
Save and compile your codes, correct any errors encountered.
7.
Execute your program.
SOLUTION #include #include class Student { private : char name[30],location[50], ic[14]; int age; public : void set_data() { cout<<"Enter Name : "; cin.getline(name, 30); cout<<"Enter Location : "; cin.getline(location, 50); cout<<"Enter IC : "; cin.getline(ic, 13); cout<<"Enter age : "; cin>>age; } void print() { cout<<"Name cout<<"IC cout<<"Location cout<<"Age }
: : : :
"<
}; void main() { Student S1; cout<<"--------------------------------"<
MULTIMEDIA UNIVERSITY
3
DCS 5088 OBJECT ORIENTED PROGRAMMING
(b)
LAB 3
Modify the program above so that the information of 3 students can be entered. You have to declare object S1 as an array of 3 elements. You are required to use for loop for this program.
SOLUTION #include #include class Student { private : char name[30],location[50], ic[14]; int age; public : void set_data() { cin.getline(name,30); cout<<"Enter Name : "; cin.getline(name, 30); cout<<"Enter Location : "; cin.getline(location, 50); cout<<"Enter IC : "; cin.getline(ic, 13); cout<<"Enter age : "; cin>>age; cout<<"\n"; } void print() { cout<<"Name cout<<"IC cout<<"Location cout<<"Age cout<<"\n"; }
: : : :
"<
}; void main() { Student S1[3]; cout<<"--------------------------------"<
MULTIMEDIA UNIVERSITY
4
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
EXERCISE 2 (a)
Write a complete code based on the following information. (i)
Create a class called Purchase.
(ii)
Data member (set to private) : name(char), qty(int), price(float)
and
total(float) . Member function : declare the member functions outside of the class. (a)
set_data(…) To set all the data to the appropriate variables.
(b)
calculate() Calculate the total amount of payment to be made.
(c)
print() Display all the information on screen.
(iii)
In main() function, create an object of class Purchase called p.
(iv)
In main() function get input from the user and pass the data to method set_data() so that the values can be set to the appropriate variables.
(v)
In main() function call function calculate() to calculate the total amount to be paid by a customer.
(vi)
In main() function call function print() to display all the information as shown below. ======================== WELCOME ======================== Enter name : Mary Enter quantity : 2 Enter price : RM 20 ======================== RECEIPT ======================== Name : Mary Quantity : 2 Price : RM 20 Payment : RM 40 Press any key to continue
MULTIMEDIA UNIVERSITY
5
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
SOLUTION #include #include
class Purchase { char name[30]; int qty; float price, total; public : void set_data(char [], int, float); void calculate(); void print(); }; void Purchase::set_data(char n[], int quantity, float p) { strcpy(name, n); qty = quantity; price = p; } void Purchase::calculate() { total = qty * price; } void Purchase::print() { cout<<"========================"<
: : : :
"<
} void main() { char name[30]; int qty; float price; Purchase p; cout<<"========================"<>qty; cout<<"Enter price : RM "; cin>>price; p.set_data(name,qty,price); p.calculate(); p.print(); }
MULTIMEDIA UNIVERSITY
6
DCS 5088 OBJECT ORIENTED PROGRAMMING
(b)
LAB 3
Modify the program above, so that it will continue to execute until the user enters ‘Y’ to quit. (Use while loop for this program). The suggested output is shown below.
Would you like to quit ? [Y/N] :N ======================== WELCOME ======================== Enter name : David Enter quantity : 2 Enter price : RM 20 ======================== RECEIPT ======================== Name : David Quantity : 2 Price : RM 20 Payment : RM 40 Would you like to quit ? [Y/N] :N ======================== WELCOME ======================== Enter name : Johnson Enter quantity : 1 Enter price : RM 100 ======================== RECEIPT ======================== Name : Johnson Quantity : 1 Price : RM 100 Payment : RM 100 Would you like to quit ? [Y/N] :Y Press any key to continue
MULTIMEDIA UNIVERSITY
7
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
SOLUTION #include #include class Purchase { char name[30]; int qty; float price, total; public : void set_data(char [], int, float); void calculate(); void print(); }; void Purchase::set_data(char n[], int quantity, float p) { strcpy(name, n); qty = quantity; price = p; } void Purchase::calculate() {
total = qty * price;
}
void Purchase::print() { cout<<"========================"<>choice; while(choice!='Y') { cin.getline(name,30); //or cin.ignore(); cout<<"========================"<>qty; cout<<"Enter price : RM "; cin>>price; p.set_data(name,qty,price); p.calculate(); p.print(); cout<<"Would u like to quit ? [Y/N] :"; cin>>choice; } }
MULTIMEDIA UNIVERSITY
8
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
EXERCISE 3 (a)
Write a complete program based on the information given below. (i)
Create a class called Marks.
(ii)
Data member (set to private) : name(char), gpa(float) and grade(char) Member function : declare the member function in the class. (a)
char set_name() •
(b)
float set_gpa() •
(c)
Get name from user and set the value to name.
Get the gpa from the user and set the value to gpa.
float set_grade() •
Calculate the Grade based on the following data.
o
GPA 3.00 to 4.00 = grade ‘A’.
o
GPA 2.00 to 2.99 = grade ‘B’.
o
GPA 0.00 to 1.99 = grade ‘F’.
(iii)
Create an object called M in main() function.
(iv)
Call all the three functions from main().
(v)
Print the name, gpa and grade in main() function.
(iv)
Suggested output is as shown below. =================================== ENTER INFORMATION =================================== Enter name : David Copperfield Enter CGPA : 3.33 =================================== RESULT SLIP =================================== Name : David Copperfield GPA : 3.33 Grade : A Press any key to continue
MULTIMEDIA UNIVERSITY
9
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 3
SOLUTION #include #include class Marks { char name[30], grade; float gpa; public: char *set_name() { cout<<"Enter name : "; cin.getline(name, 30); return name; } float set_gpa() { cout<<"Enter CGPA cin>>gpa;
: ";
return gpa; } char set_grade() { if ((gpa >= 3.00)&&(gpa <= 4.00)) grade = 'A'; else if ((gpa >= 2.00)&&(gpa < 3.00)) grade = 'B'; else grade = 'F'; return grade; } }; void main() { char name[30], grade; float gpa; Marks M; cout<<"==================================="<
MULTIMEDIA UNIVERSITY
10