Q1: Circle the correct answer for each of the following:
1. We can have only public inheritance
A. TRUE B. FALSE
2. Derived classes can access protected members of the base class
A. TRUE B. FALSE
3. While creating an object of a derived class, only its constructor is called. Base class has no
role to play.
A. TRUE B. FALSE
4. By default, all members of a class are public
A. TRUE B. FALSE
5. The default access specifier for the class members is
A. public B. private C. protected D. None of the above.
6. Members which are not intended to be inherited are declared as:
A. Public members Protected members C. Private members D. Private or Protected
members
7. If a derived class object is created, which constructor is called first?
A. Base class B. Derived class C. Depends on how we D. Not possible
constructor constructor call the object
1 2 3 4 5 6 7
Q2: What will be the output of the given code snippet below?
#include<iostream> #include<iostream>
using namespace std; using namespace std;
class P { class P{
public: protected: int w, h;
void print() { cout <<" Inside P"; } public:
}; void set_values (int a, int b)
{ w=a; h=b;}
class Q : public P { };
public: class R: public P{
void print() { cout <<" Inside Q"; } public:
}; int area () { return w * h; }
};
1
class T: public P
{
public:
int area ()
class R: public Q { };
{ return w * h / 2; }
};
void main(void)
void main () {
{ R r;
R rect; T trgl;
[Link](); }
rect.set_values (4,7);
trgl.set_values (5,10);
cout << [Link]() << '\n';
cout << [Link]() << '\n';
}
Q3: Programming
1. Write a C++ program to swap two numbers using "call by 2. Write a C++Program to compute absolute value
reference". (3 pts.) works both for integer and float (4 pts.)
#include <iostream>
using namespace std;
int absolute(int);
float absolute(float);
void swap(int&, int&); void main() {
void main() int a = -5;
{ float b = 5.5;
int a = 1, b = 2;
swap(a, b); cout << "Absolute value of " << a << " = " <<
} absolute(a) << endl;
cout << "Absolute value of " << b << " = " <<
void swap(int& n1, int& n2) absolute(b);
{ system("pause");
int temp; }
temp = n1;
n1 = n2; int absolute(int var) {
n2 = temp; if (var < 0)
var = -var;
}
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}