Trace The Following Program To Find Out Its Output

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

The Hashemite University

Computer Engineering Department


Data Structure

Dr.Samer Khasawneh Exercise 2 Fall23/24

Trace the following program to find out its output.

#include <iostream>
using namespace std;

int var = -10;

class C1 {
public:
int x, y, z;
C1() { x = 10; y = 20; z = 30; }
C1(int a,int b,int c):x(a),y(b), z(c) { }
C1(const C1& ob) {
x = ob.x;
y = ob.y;
z = ob.z;
cout << "COPY: " << x + this->y + z << endl; }
friend ostream& operator<<(ostream& OUT, const C1& OB);
};
class C2
{
bool A;
char B;
public:
C2(bool a = true, char b = 'a') :A(a),B(b) {}
C2(const C2& ob) {
A = ob.A;
B = ob.B;
cout << "hi : " << A << '\t' << ob.B << endl; }
bool get_A() { return A; }
char get_B() { return B; }
friend ostream& operator<<(ostream& OUT, const C2& OB);
};
C1 fun(C1& ob1, C2 ob2, C1& ob3)
{
if (ob1.x > ob3.x && ob2.get_A() > ob3.z)
return ob1;
else
return ob3;
}
ostream& operator<<(ostream& OUT, const C2& OB)
{
OUT << "Printing C2 info: " << OB.A << OB.B + OB.A + OB.B;
return OUT;
}
ostream& operator<<(ostream& OUT, const C1& OB)
{
OUT << '*' << OB.x + OB.y << "**"<< OB.z << "***"<<OB.y+ var + OB.z;
return OUT;
}
C1& operator+=( C1& L, const C1& R)
{
L.x = R.y + 2;
L.y = R.z;
L.z = R.x + R.y - R.z;
return L;
}

int main() Page 1 of 2


{
C1 one, two(-2, 14, 5), three, four(20, -5, 11);
C2 five, six(0, 'c'), seven(400, 'e');
int main()
{
C1 one, two(-2, 14, 5), three, four(20, -5, 11);
C2 five, six(0, 'c'), seven(400, 'e');

C1 ob = fun(two, seven, three);

one += four;
two += three;

cout << one << endl;


cout << two << endl;
cout << six << endl;

return 0;
}

Page 2 of 2

You might also like