Object-Oriented Programming Object-Oriented Programming (COM357) Lab Week 7 Exercise 1: Array of Objects //In week 6, you learnt how to define a class, methods and create objects. In this exercise you will learn how to create an array of objects. It is just like any other array you learnt to use in the previous weeks. // class person { char name; int account; float balance; public: void getdata(void); //define the method outside the class definition void display(void); //define the method outside the class definition }; void person::getdata(void) { cout << "Enter name: "; cin >> name; cout << "Enter account no: "; cin >> account; cout << "Enter balance amount: "; cin >> balance; } void person::display(void) { cout << "\nName: " << name; cout << "\nAccount: " << account; cout << "\nBalance: " << balance; } void main() { …………; //create an array of 3 objects of type person for(int i=0;i<3;i++) http://www.infm.ulst.ac.uk/~siddique Object-Oriented Programming { cout << "\nGet details of person " << (i+1) << ": \n"; ……..; //call the member method getdata() here } cout<<"\n"; for(int i=0;i<3;i++) { cout << "\nDetails of person " << (i+1) << ": \n"; ……..; //call the member method display() } cout<<"\n"; getche(); } Exercise 2: Constructors This exercise will give you the basic concepts of constructor. A constructor has the name same as the class, constructor has no type and constructor can have parameter. class solution { int a; int b; int c; public: void indata(void) //inside declaration { cout << "Enter a: "; cin >> a; cout << "Enter b: "; cin >> b; cout << "Enter c: "; cin >> c; } //Define a constructor for the method above void outdata(void) {cout<<a<<b<<c<<endl;} }; void main() { solution x; x.indata(); //create an object and initialize the constructor by replacing the two lines //above http://www.infm.ulst.ac.uk/~siddique Object-Oriented Programming x.outdata(); getche(); } Exercise 3: Friend Functions Friend function has been discussed in week 6. Please read the section of lecture once again. A friend function is not a member method of a class but have access to the data. It is declared in a class and can be defined anywhere in the program like any function. It can only access data via an object. Write a friend function mean() which will have access to the data items of the class. The method mean() should display the mean of the data values a and b in the class. class sample { float a; float b; public: void input() { a=25.00; b=40.00; } friend float mean(sample s); }; float mean(sample s) { ……………………// Define the function here } void main() { //write main program here…. //See the example in exercise 5…. //Create object………. //Call methods……….. } http://www.infm.ulst.ac.uk/~siddique Object-Oriented Programming Exercises 4-5 are for super-mouse team Exercise 4: Friend Functions with quadratic equation A complex number consists of two parts – real part and an imaginary part written as x jy , where x and y are real values. j is imaginary quantity defined as j 1 . /* Friend functions*/ class complex //a+jb { float a; float b; public: void input(float x, float y) { a=x; b=y; } friend complex sum(complex, complex); void display(complex); }; complex sum(complex c1, complex c2) { complex c3; c3.a=c1.a+c2.a; c3.b=c1.b+c2.b; return (c3); } void complex::display(complex c) { if(c.b>0) cout << c.a << "+j" << c.b <<"\n"; else cout << c.a << "-j" << c.b <<"\n"; //There will be a problem here when you run the program. Find it and correct it! } http://www.infm.ulst.ac.uk/~siddique Object-Oriented Programming void main() { complex A,B,C; //object of type complex A.input(3.0,5.5); ………………… //Enter the values of B C=sum(A,B); // See how it is called //Calculate the sum of the complex no cout << "\n A = "; A.display(A); cout << "\n B = "; B.display(B); ………………………………….. //Display the complex number C cout<<"\n"; getche(); } Exercise 5: This is an extension of the exercise from last week. Two roots of the quadratic equation ax 2 bx c 0 is calculated by using the formula: b b 2 4ac 2a Where a, b and c are real values. The term (b 2 4ac) is called discriminant. There can be three different situations for the discriminant such as (b 2 4ac) 0 , (b 2 4ac) 0 and (b 2 4ac) 0 . If (b 2 4ac) 0 , we get x an imaginary number for the term b 2 4ac , thus leading to a complex solution. A complex number consists of two parts – real part and an imaginary part, written as x jy , where x and y are real values. j is defined as j 1 . For the discriminant (b 2 4ac) 0 , the imaginary part is derived as follows b 2 4ac = p = 1. p = jy . Define a class for the complex number. Define a function (with parameters and return value) for calculation of the discriminant. Write further functions (with parameters/no parameters and return value/no return value) for the solutions of the quadratic equation for (b 2 4ac) 0 , (b 2 4ac) 0 and (b 2 4ac) 0 . Test the program for given values of a, b, c as follows. http://www.infm.ulst.ac.uk/~siddique Object-Oriented Programming 1 2 3 for (b 2 4ac) 0 1 2 1 for (b 2 4ac) 0 1 3 2 for (b 2 4ac) 0 Depending on the value of the discriminant, it will call different functions described below: If the discriminant (b 2 4ac) 0 , it will display two complex solutions in the following form: ----------------------------------------------------Discriminant (b^2 – 4*a*c) = ? Quadratic equation has two complex solutions X1= (-b+ j [sqrt(b^2 – 4*a* c)]/2*a = X2= (-b- j [sqrt(b^2 – 4*a* c)]/2*a = ----------------------------------------------------If the discriminant (b 2 4ac) 0 , it will display two identical solutions in the following form: -------------------------------------------------Discriminant (b^2 – 4*a*c) = 0 The two real solutions are: X1= (-b+sqrt(b^2 – 4*a* c)/2*a = X2= (-b-sqrt(b^2 – 4*a* c)/2*a = -------------------------------------------------If the discriminant (b 2 4ac) 0 , it will display two real solutions in the following form: -------------------------------------------------Discriminant (b^2 – 4*a*c) = ? The two real solutions are: X1= (-b+sqrt(b^2 – 4*a* c)/2*a = X2= (-b-sqrt(b^2 – 4*a* c)/2*a = -------------------------------------------------Do the exercises 1-3, compile the programs and run it. Show any two programs to the demonstrator and get signed in. Exercises 4-5 are for super-mouse team. You can choose exercises 1-3 or exercises 4-5. You will need the programs for the lab submission in week 11. http://www.infm.ulst.ac.uk/~siddique