71. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(int cap){
capacity = cap;
}
friend void show();
};
void Box::show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}
int main(int argc, char const *argv[])
{
show();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(int cap){
capacity = cap;
}
friend void show();
};
void Box::show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}
int main(int argc, char const *argv[])
{
show();
return 0;
}72. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("steve jobs is legend");
string::iterator it;
str.erase (str.begin()+ 5, str.end()-7);
cout << str << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("steve jobs is legend");
string::iterator it;
str.erase (str.begin()+ 5, str.end()-7);
cout << str << endl;
return 0;
}73. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int n = 0) : x(n) {};
int& operator[](int n)
{
cout << "0" ;
return x;
}
int operator[](int n) const
{
cout << "1" ;
return x;
}
};
void foo(const A& a)
{
int z = a[2];
}
int main()
{
A a(7);
a[3] = 8;
int z = a[2];
foo(a);
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int n = 0) : x(n) {};
int& operator[](int n)
{
cout << "0" ;
return x;
}
int operator[](int n) const
{
cout << "1" ;
return x;
}
};
void foo(const A& a)
{
int z = a[2];
}
int main()
{
A a(7);
a[3] = 8;
int z = a[2];
foo(a);
return 0;
}74. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
box.width = box.width * 2;
cout << "Width of box : " << box.width << endl;
}
int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
box.width = box.width * 2;
cout << "Width of box : " << box.width << endl;
}
int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}75. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex():real(0), imag(0){}
Complex operator ()(float re, float im)
{
real += re;
imag += im;
return *this;
}
Complex operator() (float re)
{
real += re;
return *this;
}
void display()
{
cout << "(" << real << "," << imag << ")" << endl;
}
};
int main()
{
Complex c1, c2;
c2 = c1(3.2, 5.3);
c1(6.5, 2.7);
c2(1.9);
cout << "c2=";c1.display();
cout << "c2=";c2.display();
return 0;
}
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex():real(0), imag(0){}
Complex operator ()(float re, float im)
{
real += re;
imag += im;
return *this;
}
Complex operator() (float re)
{
real += re;
return *this;
}
void display()
{
cout << "(" << real << "," << imag << ")" << endl;
}
};
int main()
{
Complex c1, c2;
c2 = c1(3.2, 5.3);
c1(6.5, 2.7);
c2(1.9);
cout << "c2=";c1.display();
cout << "c2=";c2.display();
return 0;
}76. What will be the output of the following C++ code?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1(4.0, 3.0);
complex<float> c2(polar(5.0, 0.75));
cout << (c1 += sqrt(c1)) << endl;
return 0;
}
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1(4.0, 3.0);
complex<float> c2(polar(5.0, 0.75));
cout << (c1 += sqrt(c1)) << endl;
return 0;
}77. What does the dereference operator will return?
78. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
return 0;
}79. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class B
{
int b;
public:
B(){}
B(int i){
b = i;
}
int show(){
return b;
}
};
class C
{
B b;
public:
C(int i){
b = B(i);
}
friend void show(){
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;
}
};
int main(int argc, char const *argv[])
{
show();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class B
{
int b;
public:
B(){}
B(int i){
b = i;
}
int show(){
return b;
}
};
class C
{
B b;
public:
C(int i){
b = B(i);
}
friend void show(){
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;
}
};
int main(int argc, char const *argv[])
{
show();
return 0;
}80. Give the function prototype of the operator function which we need to define in this program so that the program has no errors.
#include <iostream>
#include <string>
using namespace std;
class Box{
int capacity;
public:
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
if(b1 == b2){
cout<<"Equal";
}
else{
cout<<"Not Equal";
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Box{
int capacity;
public:
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
if(b1 == b2){
cout<<"Equal";
}
else{
cout<<"Not Equal";
}
return 0;
}Read More Section(Classes and Objects in C plus plus)
Each Section contains maximum 100 MCQs question on Classes and Objects in C plus plus. To get more questions visit other sections.
