Lab 3 Ans
Lab 3 Ans
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
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) (ii) (iii) Create a class called Student. The data members are : name(char), location(char), ic(char) and age(int). All data members must be declared as private. The member functions are : 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
LAB 3
3.
: : : :
4. 5. 6. 7. SOLUTION
In function main(), create an object of class Student called S1. From main(), call both member functions set_data() and print(). Save and compile your codes, correct any errors encountered. Execute your program.
#include<iostream.h> #include<string.h> 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<<"--------------------------------"<<endl; cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl; cout<<"--------------------------------"<<endl; S1.set_data(); cout<<"\n"; cout<<"--------------------------------"<<endl; cout<<"STUDENT INFOMATION"<<endl; cout<<"--------------------------------"<<endl; S1.print(); }
MULTIMEDIA UNIVERSITY
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 for loop for this program.
SOLUTION
#include<iostream.h> #include<cstring> 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<<"--------------------------------"<<endl; cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl; cout<<"--------------------------------"<<endl; for(int i = 0; i < 3; i++) { S1[i].set_data(); } cout<<"\n"; cout<<"--------------------------------"<<endl; cout<<"STUDENT INFOMATION"<<endl; cout<<"--------------------------------"<<endl; for(i = 0; i < 3; i++) { S1[i].print(); } }
: : : :
MULTIMEDIA UNIVERSITY
LAB 3
EXERCISE 2 (a) Write a complete code based on the following information. (i) (ii) Create a class called Purchase. 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) (b) (c) (iii) (iv) (v) (vi) set_data() To set all the data to the appropriate variables. calculate() Calculate the total amount of payment to be made. print() Display all the information on screen. In main() function, create an object of class Purchase called p. 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. In main() function call function calculate() to calculate the total amount to be paid by a customer. 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
LAB 3
SOLUTION
#include<iostream.h> #include<string.h> 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<<"========================"<<endl; cout<<" RECEIPT "<<endl; cout<<"========================"<<endl; cout<<"Name cout<<"Quantity cout<<"Price cout<<"Payment : : : : "<<name<<endl; "<<qty<<endl; RM "<<price<<endl; RM "<<total<<endl;
void main() { char name[30]; int qty; float price; Purchase p; cout<<"========================"<<endl; cout<<" WELCOME "<<endl; cout<<"========================"<<endl; cout<<"Enter name : "; cin.getline(name,30); cout<<"Enter quantity : "; cin>>qty; cout<<"Enter price : RM "; cin>>price; p.set_data(name,qty,price); p.calculate(); p.print(); }
MULTIMEDIA UNIVERSITY
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.
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
LAB 3
SOLUTION
#include<iostream.h> #include<string.h> 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<<"========================"<<endl; cout<<" RECEIPT "<<endl; cout<<"========================"<<endl; cout<<"Name : "<<name<<endl; cout<<"Quantity : "<<qty<<endl; cout<<"Price : RM "<<price<<endl; cout<<"Payment : RM "<<total<<endl; } void main() { char name[30], choice; int qty; float price; Purchase p; cout<<"Would u like to quit ? [Y/N] :"; cin>>choice; while(choice!='Y') { cin.getline(name,30); //or cin.ignore(); cout<<"========================"<<endl; cout<<" WELCOME "<<endl; cout<<"========================"<<endl; cout<<"Enter name : "; cin.getline(name,30); cout<<"Enter quantity : "; cin>>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
LAB 3
EXERCISE 3 (a) Write a complete program based on the information given below. (i) (ii) Create a class called Marks. 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) (c) Get name from user and set the value to name.
float set_gpa() Get the gpa from the user and set the value to gpa.
float set_grade() Calculate the Grade based on the following data. o o o GPA 3.00 to 4.00 = grade A. GPA 2.00 to 2.99 = grade B. GPA 0.00 to 1.99 = grade F.
Create an object called M in main() function. Call all the three functions from main(). Print the name, gpa and grade in main() function. 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
LAB 3
SOLUTION
#include<iostream.h> #include<string.h> 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<<"==================================="<<endl; cout<<" ENTER INFORMATION "<<endl; cout<<"==================================="<<endl; strcpy(name, M.set_name()); gpa = M.set_gpa(); grade = M.set_grade(); cout<<"==================================="<<endl; cout<<" RESULT SLIP "<<endl; cout<<"==================================="<<endl; cout<<"Name : "<<name<<endl; cout<<"GPA : "<<gpa<<endl; cout<<"Grade : "<<grade<<endl; }
: ";
MULTIMEDIA UNIVERSITY
10