Upload
devaraj-thanaraaj
View
213
Download
0
Embed Size (px)
Citation preview
7/30/2019 Lab3ans
1/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
LAB 3
Objective
At the end of this lab activity, the students should 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
7/30/2019 Lab3ans
2/10
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 enterand 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:
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 member functions are : void set_data() and void print(). All
member functions must be declared as public.
2. In member function set_data(), get input from the user and set the values to the
data members.
MULTIMEDIA UNIVERSITY 2
--------------------------------WELCOME TO A-PLUS TUITION CENTER--------------------------------Enter Name : Usha VellappanEnter Location : MelakaEnter IC : 750217016680Enter age : 30
--------------------------------STUDENT INFOMATION--------------------------------Name : Usha VellappanIC : 750217016680Location : MelakaAge : 30
Press any key to continue
class Student{
private :
char name[30],location[50], ic[14];
int age;public :
void set_data();
void print();
void set_data(){
cout
7/30/2019 Lab3ans
3/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
3. In member function print(), display all the information on screen.
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
MULTIMEDIA UNIVERSITY 3
void print(){
cout
7/30/2019 Lab3ans
4/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
(b) 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 forloop for this
program.
SOLUTION
MULTIMEDIA UNIVERSITY 4
#include#include
class Student{
private :char name[30],location[50], ic[14];int age;
public :void set_data(){
cin.getline(name,30);cout
7/30/2019 Lab3ans
5/10
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.
MULTIMEDIA UNIVERSITY 5
========================WELCOME
========================Enter name : MaryEnter quantity : 2Enter price : RM 20========================
RECEIPT========================Name : MaryQuantity : 2Price : RM 20Payment : RM 40
Press any key to continue
7/30/2019 Lab3ans
6/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
SOLUTION
MULTIMEDIA UNIVERSITY 6
#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
7/30/2019 Lab3ans
7/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
(b) 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.
MULTIMEDIA UNIVERSITY 7
Would you like to quit ? [Y/N] :N
========================WELCOME
========================Enter name : DavidEnter quantity : 2Enter price : RM 20========================
RECEIPT========================Name : David
Quantity : 2Price : RM 20Payment : RM 40
Would you like to quit ? [Y/N] :N
========================WELCOME
========================Enter name : JohnsonEnter quantity : 1Enter price : RM 100========================
RECEIPT========================
Name : JohnsonQuantity : 1Price : RM 100Payment : RM 100
Would you like to quit ? [Y/N] :Y
Press any key to continue
7/30/2019 Lab3ans
8/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
SOLUTION
MULTIMEDIA UNIVERSITY 8
#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
7/30/2019 Lab3ans
9/10
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()
Get name from user and set the value to name.
(b) float set_gpa()
Get the gpa from the user and set the value to gpa.
(c) 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.
MULTIMEDIA UNIVERSITY 9
===================================ENTER INFORMATION
===================================Enter name : David Copperfield
Enter CGPA : 3.33
===================================RESULT SLIP
===================================Name : David CopperfieldGPA : 3.33Grade : A
Press any key to continue
7/30/2019 Lab3ans
10/10
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 3
SOLUTION
MULTIMEDIA UNIVERSITY 10
#include#include
class Marks{char name[30], grade;float gpa;
public:char *set_name(){
cout