Sie sind auf Seite 1von 9

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

LAB 5

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

Implement the main concepts in object-oriented programming. Introduction to default constructors, parameterized constructors Create classes and objects.

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

EXERCISE 1 (Introduction to constructor) Constructor, which allow us to initialize an objects data members when an object is defined. 1. The constructor name is the same as the class name. 2. Constructor declaration: class ClassName{ private: /.. public: //constructor ClassName (parameter_list); };

(a)

Enter the following codes in your C++ compiler and observe the output.
#include<iostream.h> class Point { public: double x, y, z; Point(); Point(double, double, double); }; Point :: Point() { x = y = z = 1.0; } Point :: Point(double xx, double yy, double zz ){ x=xx ; y=yy ; z=zz ; } void main() { Point p; cout << "Hello " << p.x << << p.y << << p.z << endl; Point q(5.0,6.0,7.0); cout << "Hello " << q.x << << q.y << << q.z << endl; }

Solution: Hello 1 1 1 Hello 5 6 7

EXERCISE 2 (a) Write a complete code based on the following information. (i) Create a class called Staff

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

(ii) (iii)

Data member (set to private): name of string type, salary of float type, tax_amount of float type Public Member functions. (a) void setData() To set the name and salary based on user input in the main function. (b) void calcTaxAmount() To calculate the tax amount based on the staffs salary. (c) If the staffs salary is < RM2000, tax amount =1% of the salary If the staffs salary is > =RM2000, tax amount=5% of the salary; void display() To display the staffs particulars (name, salary and tax_amount) (d) Staff() A constructor that prints a welcome header and initializes the name and salary to Allen and 2000 respectively and the tax amount to 0.01 of the salary. Next, print out the values. Example output from the constructor:
============================= Welcome ============================= Initial name : Allen Initial salary : 2000 Initial tax_amount : 20

(iv) (v)

In main() function, create an object of class Staff called st. In main() function invoke the objects functions in the following order: 1. setData() 2. calcTaxAmount() 3. display()

(vi)

In main() function, create another object of class Staff called a1. Invoke the display function only on this object.

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

See sample input output below: Sample Output:


============================= Welcome ============================= Initial name : Allen Initial salary : RM2000 Initial tax_amount : RM20 Enter name :Eileen Ong Enter salary :2400

============================= Display Details ============================= Name : Eileen Ong Salary : RM2400 Tax amount : RM120 ============================= Welcome ============================= Initial name : Allen Initial salary : RM2000 Initial tax_amount : RM20 ============================= Display Details ============================= Name Salary Tax amount Press any key to : Allen : RM2000 : RM20 continue

(b)

Include a parameterized constructor in (a) to accept 2 arguments which is for the name and salary. Create another object in main that the parameterized constructor should be invoked. Passed the objects parameters which is Timothy and 2500 through the constructor. Invoke the calcTaxAmount() and display() function for this object. See sample output. ============================= Display Details ============================= Name Salary Tax amount Press any key to : Timothy : RM2500 : RM125 continue

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

Solution: #include <iostream> #include<string> using namespace std; class Staff { private: string name; float salary, tax_amount; public: void setData(string n, float s) { name=n; salary=s;

void calcTaxAmount() { if (salary<2000) tax_amount=0.01*salary; else tax_amount=0.05*salary; } void display() { cout<<"\n============================="<<endl; cout<<" Display Details "<<endl; cout<<"============================="<<endl; cout<<"\nName\t\t : "<<name<<endl; cout<<"Salary\t\t : RM"<<salary<<endl; cout<<"Tax amount\t : RM"<<tax_amount<<endl; } Staff() { name="Allen"; salary=2000; tax_amount=0.01*salary; cout<<"============================="<<endl; cout<<" Welcome "<<endl; cout<<"============================="<<endl; cout<<"Initial name\t\t : "<<name<<endl; cout<<"Initial salary\t\t : RM"<<salary<<endl; cout<<"Initial tax_amount\t : RM"<<tax_amount<<endl; }

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

Staff(string n, float s) { name=n; salary=s; }

}; void main() { Staff st; string name; float salary; cout<<"Enter name :"; getline(cin, name); cout<<"Enter salary : RM"; cin>>salary; st.setData(name,salary); st.calcTaxAmount(); st.display(); Staff a1; a1.display(); Staff n("Timothy",2500); n.calcTaxAmount(); n.display(); } EXERCISE 3 (a) Write a complete code based on the following information. (i) (ii) (iii) Create a class called Marks that keeps some information for a subjects quiz marks(total number of quizzes is 5). Data member (set to private): points[5] of float type, subject_name of string type. Member functions. (a) (b) (c) void setData() To set all the data members based on user input. string getSubjectName() Returns the subject_name; float* getPoints() Returns the address of the points array. (d) float average()

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

Calculate the average of the 5 quizzess points and returns the average (e) Marks(string, float[]) Parameterized constructor that accepts 2 arguments when an object is created and initializes the data members with it. (iv) (v) In main() function, create an object of class Marks called m. In main() function for this m object: i. get input from the user to set the data members using the setData() function. ii. Print out the subject name using the getSubjectName()) function iii. print out the details using the getPoints() function.[tip: use a pointer to get the value returned by this function and use a loop to access elements]. iv. Print out the average of the points by calling the average() function (vi) (vii) In main() function,declare and initalize an float array of 5 elements with the following values: 10.0,13.0,14.5,19.0,14.0, and a string variable to Maths . Continue from (vi), create an object called math, passing the float array and string variables in the correct order to invoke the paremeterized constructor. Next, repeat step(v) ii, iii, and iv on this math object. See the sample input output below:
================================ Entering quiz marks ================================ Enter subject name : OS Enter marks for quiz [1] : 13 Enter marks for quiz [2] : 15.5 Enter marks for quiz [3] : 19.5 Enter marks for quiz [4] : 18 Enter marks for quiz [5] : 17 ================================ Quiz marks and average ================================ Subject :OS quiz 1 : 13 quiz 2 : 15.5 quiz 3 : 19.5 quiz 4 : 18 quiz 5 : 17 Average : 16.6 Creating another object that invokes the parameterized constuctor ================================ Quiz marks and average ================================ Subject :Maths quiz 1 : 10 quiz 2 : 13 quiz 3 : 14.5 quiz 4 : 19 quiz 5 : 14 Average : 14.1 Press any key to continue

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

Solution: #include<iostream> using namespace std; #include<string> class Marks { private: string subject_name; float points[5]; public: Marks(string n, float p[]) { subject_name=n; for(int i=0;i<5;i++) points[i]=p[i]; } Marks() { cout<<"================================"<<endl; cout<<" Entering quiz marks"<<endl; cout<<"================================"<<endl; } void setData() { cout<<"Enter subject name : "; cin>>subject_name; for(int i=0;i<5;i++) { cout<<"Enter marks for quiz ["<<i+1<<"] : "; cin>>points[i]; } } float* getPoints() { return points; } float average() { float sum=0.00; for(int i=0;i<5;i++) { sum=sum+points[i];

MULTIMEDIA UNIVERSITY

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 5

} return(sum/5); } string getSubjectName() { return subject_name; } }; void main() { Marks m; m.setData(); float *testm; testm=m.getPoints(); cout<<"================================"<<endl; cout<<" Quiz marks and average "<<endl; cout<<"================================"<<endl; cout<<"Subject :"<<m.getSubjectName()<<endl; for(int i=0; i<5; i++) cout<<"quiz "<<i+1<<" : "<<testm[i]<<endl; cout<<"Average : "<<m.average(); cout<<"\n\nCreating another object that invokes the parameterized constuctor"<<endl; float qm[5]={ 10.0,13.0,14.5,19.0,14.0}; string n="Maths"; Marks math(n,qm); testm=math.getPoints(); cout<<"================================"<<endl; cout<<" Quiz marks and average "<<endl; cout<<"================================"<<endl; cout<<"Subject :"<<math.getSubjectName()<<endl; for(i=0; i<5; i++) cout<<"quiz "<<i+1<<" : "<<testm[i]<<endl; cout<<"Average : "<<math.average()<<endl; }

MULTIMEDIA UNIVERSITY

Das könnte Ihnen auch gefallen