Bahria University: Lahore Campus
Bahria University: Lahore Campus
Bahria University: Lahore Campus
Lahore Campus
Mid Term Exam (Fall-2020 Semester)
Department of Computer Sciences
Due Date:
04/20/2021
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.
Solution:-
#include<iostream>
class complex
private:
int real, imag;
public:
void getvalue()
complex operator+(complex a)
complex temp;
return(temp);
complex operator-(complex b)
complex temp;
return(temp);
complex temp;
return(temp);
complex temp;
temp.real = real/d.real;
temp.imag = imag/d.imag;
return(temp);
void displayAdd()
cout << real << "+" << "(" << imag << ")" << "i"<<endl;
void displaySub()
cout << real << "-" << "(" << imag << ")" << "i"<<endl;
void displayMul()
cout << real << "x" << "(" << imag << ")" << "i"<<endl;
}
void displayDiv()
cout << real << "/" << "(" << imag << ")" << "i"<<endl;
};
int main()
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>
private:
int num;
public:
count()
num = ++serial;
};
void show()
};
};
int count::serial = 0;
count o1;
count o2;
count o3;
o1.show();
o2.show();
o3.show();
Output:-
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);
}
{
if (h3 >= 0 && h3 <= 23)
h = h3;
}
{
if (s3 >= 0 && s3 <= 59)
s = s3;
}
{
cout << h << ":" << m << ":" << s << endl;
}
Time()
{
h = m = s = 0;
}
{
h = m = s = 0;
set(h1, m1, s1);
}
};
t1 = t3 += t2;
t1.show();
return 0;
}
Output:-
BEST OF LUCK