C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language

Find output of C++ programs -1 (Mixed Topics)

Here you will find output of C++ programs; you have to find their outputs. Outputs and their explanations are presented here.
Submitted by Amit Shukla, on July 30, 2017

1. Find the output of following C++ program.

#include<iostream>
using namespace std;

class A
{
	public :
		int x=20;
};
class B
{
	public :
		int x=10;
};
int main()
{
	A obj1;
	B obj2;
	obj1 = obj2;
	cout<< obj1.x;
	cout<<endl;
	return 0;
}

Output

The program will not generate output due to compilation error.

Explanation

In this program the program cannot generate output due to compilation error at obj1 = obj 2; because in C++ we cannot use arithmetic operations on different objects of different classes.

The correct code should be:

#include<iostream>
using namespace std;

class A
{
	public :
		int x=20;
};
class B
{
	public :
		int x=10;
};

int main()
{
	A obj1;
	B obj2;
	obj1.x = obj2.x;
	cout<< obj1.x;
	cout<<endl;
	return 0;
}

2. Find the output of following C++ program.

#include<iostream>
using namespace std;

class Test
{
	private :
		int marks = 85;
	public :
		Test(int marks)
		{
			cout<< this->marks;
			cout<<endl;
		}
};

int main()
{
	Test t(95);
	return 0;
}

Output

85

Explanation

We can understand the above program by understand these following steps.

Step 1 –
In main function a variable t is declared of class Test using parametrize constructor by passing an integer value 95.

Step 2 –
In class Test the value of marks is initialized by integer 85.

Step 3 –
In function Test of class Test this pointer is used to print marks. As we know that the value of variable marks passed by constructor is 95 but due to this pointer the value taken of marks is the value which is initialized in class. Hence the output of this program is 85.


3. Find the output of following C++ program.

#include<iostream>
using namespace std;

class A
{
	public :
		A()
		{
			func();
		}
		~A()
		{
			func();
		}
		void func()
		{
			cout<< 3;
			cout<<endl;
		}
		void fun()
		{
			func();
		}
} ;
class B : public A
{
	void func()
	{
		cout<< 2;
		cout<<endl;
	}
} ;

int main()
{
	B b;
	b.fun();
	return 0;
}

Output

3
3
3

Explanation

We can understand the following program in following steps.

Step 1 –
In main function of the program. We initialize the object b of class B.

Step 2 –
In main function we call the fun function using the object b of class B.

Step 3 –
We can see that the class B is derived from class A and we know that derived class is always complete class.

Step 4 –
When we call function fun() then fun() give address of function func().

Step 5 –
We can see that function func() are exists in both classes but the func() of base class opens because both the function fun() and func() are in base class that’s why func() of base class opens. Hence the output of this code is 3.

The result comes three times because of the constructor and destructor present in program.


4. Find the output of following C++ program.

#include<iostream>
using namespace std;

int x=2;
int main()
{
	int x=4;
	{
		int x=8;
		cout<< x;
		cout<<endl;
	}
	cout<< x;
	cout<<endl;
	cout<< ::x;
	cout<<endl;
	return 0;
}

Output

8
4
2

Explanation

We can understand the program in following steps.

Step 1 –
In this step we declare integer x = 2 outside the main function.

Step 2 –
In this step we declare the integer x = 4 inside the main function.

Step 3 –
In this step we declare integer x = 8 inside parenthesis inside main function.

Step 4 –
We print the value of integer x inside the parenthesis.

Step 5 –
We print the value of integer x inside the main function.

Step 6 –
We print the value of integer x which is declared outside the function.

Hence the resultant output of this program is 8 4 2.


5. Find the output of following C++ program.

#include<iostream>
using namespace std;

int fun (int x, int y)
{
	return x + y ;
}
double fun (int x, int y)
{
	return x * y ;
}

int main()
{
	cout<<fun(5 , 10);
	return 0;
}

Output

The program will not generate output due to compilation error.

Explanation

The program terminates due to compilation error because in C++ we cannot overload function by giving both same function name and function parameters.

The correct program should be:

#include<iostream>
using namespace std;
int fun (int x, int y)
{
	return x + y ;
}
double fun (double x, double y)
{
	return x * y ;
}

int main()
{
	cout<<fun(5 , 10);
	return 0;
}








COMMENTS