Conversion Operators(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option A. -> convertedIn this program, We casted the string to the object of the class.
Output:
$ g++ con4.cppp
$ a.out
converted
Question 5.
1.
#include
2.
#include
3.
using namespace std;
4.
class Complex
5.
{
6.
private:
7.
double real;
8.
double imag;
9.
public:
10.
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
11.
{}
12.
double mag()
13.
{
14.
return getMag();
15.
}
16.
operator double ()
17.
{
18.
return getMag();
19.
}
20.
private:
21.
double getMag()
22.
{
23.
return sqrt(real * real + imag * imag);
24.
}
25.
};
26.
int main()
27.
{
28.
Complex com(3.0, 4.0);
29.
cout
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
class Complex
5.
{
6.
private:
7.
double real;
8.
double imag;
9.
public:
10.
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
11.
{}
12.
double mag()
13.
{
14.
return getMag();
15.
}
16.
operator double ()
17.
{
18.
return getMag();
19.
}
20.
private:
21.
double getMag()
22.
{
23.
return sqrt(real * real + imag * imag);
24.
}
25.
};
26.
int main()
27.
{
28.
Complex com(3.0, 4.0);
29.
cout
Explanation:-
Answer: Option A. -> 5 5In this program, we are calculating the magnitude value by two ways.
Output:
$ g++ con3.cpp
$ a.out
55
Question 6.
1.
#include
2.
using namespace std;
3.
class sample
4.
{
5.
public:
6.
sample(int i) : m_i(i) { }
7.
public:
8.
int operator()(int i = 0) const
9.
{
10.
return m_i + i;
11.
}
12.
operator int () const
13.
{
14.
return m_i;
15.
}
16.
private:
17.
int m_i;
18.
friend int g(const sample&);
19.
};
20.
int f(char c)
21.
{
22.
return c;
23.
}
24.
int main()
25.
{
26.
sample f(2);
27.
cout
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class sample
4.
{
5.
public:
6.
sample(int i) : m_i(i) { }
7.
public:
8.
int operator()(int i = 0) const
9.
{
10.
return m_i + i;
11.
}
12.
operator int () const
13.
{
14.
return m_i;
15.
}
16.
private:
17.
int m_i;
18.
friend int g(const sample&);
19.
};
20.
int f(char c)
21.
{
22.
return c;
23.
}
24.
int main()
25.
{
26.
sample f(2);
27.
cout
Explanation:-
Answer: Option B. -> 4In this program, we are adding its value with it itself, So only we got the output as 4.
Output:
$ g++ con1.cpp
$ a.out
4
Question 8.
1.
#include
2.
using namespace std;
3.
class sample1
4.
{
5.
float i, j;
6.
};
7.
class sample2
8.
{
9.
int x, y;
10.
public:
11.
sample2 (int a, int b)
12.
{
13.
x = a;
14.
y = b;
15.
}
16.
int result()
17.
{
18.
return x + y;
19.
}
20.
};
21.
int main ()
22.
{
23.
sample1 d;
24.
sample2 * padd;
25.
padd = (sample2*) &d;
26.
cout 27.
return 0;
28.
}
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class sample1
4.
{
5.
float i, j;
6.
};
7.
class sample2
8.
{
9.
int x, y;
10.
public:
11.
sample2 (int a, int b)
12.
{
13.
x = a;
14.
y = b;
15.
}
16.
int result()
17.
{
18.
return x + y;
19.
}
20.
};
21.
int main ()
22.
{
23.
sample1 d;
24.
sample2 * padd;
25.
padd = (sample2*) &d;
26.
cout 27.
return 0;
28.
}
Explanation:-
Answer: Option D. -> c or bAs it assigns to a reference to an object of another incompatible type using explicit type-casting.
Output:
$ g++ con.cpp
$ a.out
14032334