Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bahria University: Lahore Campus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Bahria University Total Pages: 2

Lahore Campus
Mid Term Exam (Fall-2020 Semester)
Department of Computer Sciences

Solution Uploaded on LMS


Date & Time: 18/05/2021 11:00 pm

Due Date:
04/20/2021

Instructor Name: Mr. Irfan Latif Program/Semester: BSCS 2B


Course Code: CSL 210 Course Title: Object Oriented Programming
Lab
Max Marks: 30 Weightage: 30%
Student Name: ___UMAIR RAZA______ Enrolment: __03-134202-105__

Instructions:
Read out the Instructions carefully.
I. Attempt all questions
II. Attempt all questions on Mars Simulator.
___________________________________________________________________________
________________

Q1: Write a program in C++ for Complex numbers Calculation. Create a class
complex having X and Y data members, also includes member functions
addition, subtraction, multiplication and division.

Make a display function to display the calculations by using discussed member


functions. 10 Marks

Solution:-

#include<iostream>

using namespace std;

class complex

private:
int real, imag;

public:

void getvalue()

cout << "Enter real number:";

cin >> real;

cout << "Enter imaginary number:";

cin >> imag;

complex operator+(complex a)

complex temp;

temp.real = real + a.real;

temp.imag = imag + a.imag;

return(temp);

complex operator-(complex b)

complex temp;

temp.real = real - b.real;

temp.imag = imag - b.imag;

return(temp);

complex operator * (complex c)

complex temp;

temp.real = real * c.real;


temp.imag = imag * c.imag;

return(temp);

complex operator /(complex d)

complex temp;

temp.real = real/d.real;

temp.imag = imag/d.imag;

return(temp);

void displayAdd()

cout<<"Sum is: ";

cout << real << "+" << "(" << imag << ")" << "i"<<endl;

void displaySub()

cout<<"Difference is: ";

cout << real << "-" << "(" << imag << ")" << "i"<<endl;

void displayMul()

cout<<"Product is: ";

cout << real << "x" << "(" << imag << ")" << "i"<<endl;
}

void displayDiv()

cout<<"Division is: ";

cout << real << "/" << "(" << imag << ")" << "i"<<endl;

};

int main()

complex c1, c2, c3, c4,c5,c6;

c1.getvalue();

c2.getvalue();

c3 = c1 + c2;

c4 = c1 - c2;

c5 = c1 * c2;

c6 = c1/c2;
c3.displayAdd();

c4.displaySub();

c5.displayMul();

c6.displayDiv();

return 0;

Output:-

Q2: Create a class that includes a data member that holds a “serial number” for
each object created from the class. That is, the first object created will be
numbered 1, the second 2 and so on.

To do this, you will need another data member that records a count of how many
object have been created so far. (This member should apply to the class as a
whole; not to individual objects. What keyword specifies this?) Then, as each
object is created, its constructor can examine this count member variable to
determine the appropriate serial number for the new object.

Add a member function that permits an object to report its own serial number.
Then write a main () program that creates three objects and quires each one
about its serial number. They should respond I am object number 2, and so on.
10 Marks

Solution:-
#include <iostream>

using namespace std;

class count {//class

private:

static int serial;

int num;

public:

count()

num = ++serial;

};

void show()

cout << "Object number " << num << endl;

};

};
int count::serial = 0;

int main() //main function

count o1;

count o2;

count o3;

o1.show();

o2.show();

o3.show();

Output:-

Q3: Write a program that substitutes an overloaded += operator for the


overloaded + operator. This operator should allow statements like:

obj1 + = obj2;

Where obj2 is added (concatenated) to obj1 and the result is left in obj1. The
operator should also permits the results of the operation to be used in other
calculations, as in
obj3 = obj1 + = obj2;
10 Marks

Solution:-
#include <iostream>
using namespace std;

class Time//class
{
int h, m, s;
public: //to public
void set(int h2, int m2, int s2)

{
seth(h2);
setm(m2);
sets(s2);
}

void seth(int h3)

{
if (h3 >= 0 && h3 <= 23)
h = h3;
}

void setm(int m3)


{
if (m3 >= 0 && m3 <= 59)
m = m3;
}

void sets(int s3)

{
if (s3 >= 0 && s3 <= 59)
s = s3;
}

int geth()const { return h; }


int getm()const { return m; }
int gets() const { return s; }
void show()

{
cout << h << ":" << m << ":" << s << endl;
}
Time()

{
h = m = s = 0;
}

Time(int h1, int m1, int s1)

{
h = m = s = 0;
set(h1, m1, s1);
}

Time operator +=(const Time &par){


this->h += par.h;
this->m += par.m;
this->s += par.s;
return *this;
}

};

int main(){//main function


Time t1, t2(5, 6, 7), t3(10, 20, 30);

t1 = t3 += t2;

t1.show();

return 0;

}
Output:-

   BEST OF LUCK   

You might also like