0% found this document useful (0 votes)
4 views

GP_OOPS_C++_Abstract_Interview_Questions

Uploaded by

nm.nielit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

GP_OOPS_C++_Abstract_Interview_Questions

Uploaded by

nm.nielit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Interview Questions

1. Does every virtual function need to be always overridden?


No, It is not always mandatory to redefine a virtual function. It can be used as it
is in the base class.

2. What is an abstract class?


An abstract class is a class that has at least one pure virtual function in its
definition. An abstract class can never be instanced (creating an object). It can
only be inherited, and the methods could be overwritten.

3. Can we have a constructor as Virtual?


Constructors cannot be virtual because they need to be defined in the class.

4. What is a pure virtual function?


A pure virtual function (or abstract function) in C++ is a virtual function for which
we don’t have an implementation. We only declare it. A pure virtual function is
declared by assigning 0 in the declaration. See the following example.

5. What are the characteristics of Friend Function?


★ A friend function is not in the scope of the class, in which it has been
declared as friend.
★ It cannot be called using the object of that class.
★ It can be invoked like a normal function without any object.
★ Unlike member functions, it cannot use the member names directly.
★ It can be declared in public or private parts without affecting its meaning.
★ Usually, it has objects as arguments.

6. What is the output of this program?

#include <iostream>
using namespace std;
class Box
{
double width;

1
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;
}

Answer: 20
Explanation:
We are using the friend function for print width and multiplied the width value by
2, So we got the output as 20

You might also like