Consider the code given below.
[Assume all the header files and namespaces included.]
How many times the following are invoked:
I) Default constructor
II) Parameterized constructor
III) Copy constructor
IV) Destructor
class test
{ int a;
public:
test(){a=0;}
test(int x) {a=x;}
test(const test &T){a=T.a;}
~test(){ }
test add(test X)
{ test T;
T.a=a+X.a;
return 0;
}
int main()
{ test t1(5),t2(10),t3;
t3=t1.add(t2);
return 0;
}
Consider a 20 *5 two-dimensional array marks which has its base address = 1000 and the size of an element = 2. Now compute the address of the element, marks[18][ 4] assuming that the elements are stored in row major order.
Complete the given code to get the output shown below:-
Base1
Base2
#include<iostream>
using namespace std;
class Base1 {
public:
void display()
{ cout<<"Base1"<<endl; } };
class Base2 {
public:
void display()
{ cout<<"Base2"<<endl"; } };
class Derived: public Base1, public
Base2 {
public:
};
int main()
{
Derived d;
return 0;
}
Write the parameterized constructor for all the classes in the hierarchy.
class A
{ int a;}
Class B:public virtual A
{int b;}
Class C:public virtual A
{int c; }
Class D:public b,public C
{int d;}
Consider the class hierarchy shown below.
class A
{ int a;
protected:
int b;
public:
int c; };
class B: protected A
{ };
Class C: public B
{ };
Which data members of class A are accessible
- in class B
-in class C
-in main function
Justify your answer.
Include the function calls at appropriate places in the given program, to get the
output as shown below
In base
In derived
#include<iostream>
using namespace std;
class base
{ int b;
protected:
void display(){cout<<"In base\n";}
};
class derived:public base
{ int d;
public:
void display(){ cout<<"In derived\n";}
};
int main()
{ derived D;
return 0;
}
Consider following code and state the order of execution constructors and order
of execution of destructors.
class C: public A, virtual public B
{ public:
C( ):A( ),B( )
{
cout<<”c class constructor”;
}
};
Consider following code and state the order of execution constructors and order
of execution of destructors.
class C: public A, virtual public B
{ public:
C( ):A( ),B( )
{
cout<<”c class constructor”;
}
};
Include the definition for the function ‘max_age’, in the program given below. The
function compares the data member age of the objects P1 and P2 and returns the
object having higher value for age.
class person
{ int age;
public:
person(int a){age=a;}
};
int main()
{ person P1(1.5);
person P2(2.5);
person P3=P1.max_age(P2);
}
Find the errors in the following program and correct them.
[Assume the necessary header files and namespaces included]
class test
{ static int x;
int y;
public:
test( ){x=0;y=0;}
static void display()
{ cout<<x<<”\t”<<y<<endl; }
void output()
{cout<<x<<”\t”<<y<<endl;}
};
int main()
{ test T1;
T1.display();
test::output();
return 0;
}