Complete C++ object oriented interview questions you could imagine

(Last Updated On: May 12, 2010)

Complete C++ object oriented interview questions you could imagine
Object Orientation:
Implementation of Polymorphism in C++?

// Dynamic Allocation of objects and Polymorphism


using namespace std;

class Person {
int i;
virtual void foo() = 0;

class Student : public Person {
void foo() {
cout << "I am a Student\n" ; } }; class Professor : public Person { void foo() { cout << "I am a Professor\n" ; } }; int main(){ Person *person1 = new Student; Person *person2 = new Professor; person1->foo();

delete person1;
delete person2;
return 0;

Polymorphism includes both overloading and overriding.

Pure/ Virtual function (Run Time)
– Function Overloading (Compile Time)
– Operator Overloading (Compile Time)

Static polymorphism (Using templates) was left out.

You have a class whose internal representation is not exposed to the user and can change transparently.

The above scenario is an example of which one of the following?
Choice 1

Choice 2

Choice 3

Choice 4

Choice 5

why not abstraction

What is the difference between class members and instance members?
The members inside class either method or variables which are declared without static keyword known as instance member,and those are declared with static keyword known as class members.

Class members can be accessed without actually creating an object of their class.Identifieed with static keyword.

Instance members need to be accessed via an object.

. Explain OOP,classes,inheritance and polumorphism.
2. Difference between C,C++.

Whats the size of a derived object ? How would you calculate it ? Does it include size of base class size?

*A derived class size also includes the size of the immediate base class/classes.
+ size of all non-static members within the derived class.
+ Byte alignment may play a role true…
Can anyone give an explanation of “byte alignment”?

Other things that would add up to what harish said are
1. virtual functions-4 bytes of virtual table pointer will be added.
if base class A has a virtual function and a class variable of type char the sizeof(A’s obj) is sizeof(Vptr)+sizeof(char).
if there exists a derived class Class B:public Class A with another virtual function and class variable of int type. sizeof(B’s obj)=sizeof(A)+sizeof(int). It wil just change the Vptr to point to its virtual function table, it will not allocate another 4bytes for its virtual function rather use one derived from base class.
divyaC on April 18, 2010 |Edit | Edit

2. Mode of inheritance

courtesy: cprogramming.com

In C++, sometimes we have to use virtual inheritance for some reasons. (One classic example is the implementation of final class in C++.) When we use virtual inheritance, there will be the overhead of 4 bytes for a virtual base class pointer in that class.

class ABase{
int iMem;

class BBase : public virtual ABase {
int iMem;

class CBase : public virtual ABase {
int iMem;

class ABCDerived : public BBase, public CBase {
int iMem;

And if you check the size of these classes, it will be:

* Size of ABase : 4
* Size of BBase : 12
* Size of CBase : 12
* Size of ABCDerived : 24

Because BBase and CBase are dervied from ABase virtually, they will also have an virtual base pointer. So, 4 bytes will be added to the size of the class (BBase and CBase). That is sizeof ABase + size of int + sizeof Virtual Base pointer.

Size of ABCDerived will be 24 (not 28 = sizeof (BBase + CBase + int member)) because it will maintain only one Virtual Base pointer (Same way of maintaining virtual table pointer).
Explanation for mode of inheritance:

now for sizeof(ABCDerived)
size of its iMem(4)+
size of its base classes with only one version of Vptr ie(BBase: size of(B’s iMem)+sizeof(A)= 4+4) +CBase: size of(C’s iMem)+sizeof(A)= 4+4) + 4 (Vptr )
Not “immediate” base class/classes. It should be “all direct or indirect” base classes (parents, grand parents, great grand parents etc).
Helper is correct. It counts all the base classes
But, when you are talking about an immediate base class, you are indirectly including all the parent classes.
Where base has a virtual func foo() that is also implemented in derieved.
What are the implications ? (Note: its opposite of what one does for polymorphism..)
No! you cant do this.
I think you agree with the first one’s comment

pure virtual functions are there in the base class to make sure that the child classes override that(it gives a prototype that has to be implemented)…but here the object created is of base class(even though the the pointer is of child class)…which will give you a compile-time error if you call the pure virtual function…
With this assignment ,d is not complete and it over ridies the basic c++ type mechanism..compiler will throw an error “Invalid conversion from xx to yy”. Though it is possible through dynamic_cast operator.

Why is this not allowed in C++ ? Explain problems with this approach

Derived *d = new Base()
1) user can call some function which is not defined in the base class
2) by the help of casting it is allowed

It is illegal to point to an object of the base class with a pointer that is of the derived class type.

Base *b = new Derived(); // compiles properly

Design OO model for given movie name and zip code to find nearest theaters showing that movie.
Explain the different types of polymorphism in C++

What is encapsulation??
Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object’s operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data’s origin.
What is inheritance?
Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.
What is Polymorphism??
Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors.
You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java.
Base class object’s pointer can invoke methods in derived class objects.
You can also achieve polymorphism in C++ by function overloading and operator overloading.
Which one of the following is NOT polymorphic behavior?
I applied for a Financial Software Development entry level position and when filling my application they asked my strongest programming language among c,c++, and Java. As I had previous experience with Java I selected it as my strongest language. The following day they sent me an email with instructions to take an online skills assessment. Reading this forum, I practiced a few IQ/Intelligence test with good results so I felt confident to take the test. I took the test and to my surprise it was all Java programming questions (syncronisation, inheritance, interfaces, polymorphism, analysing pieces of code, etc). The questions where very specific and not about general OO programming concepts eg. Which one of the following is NOT polymorphic behavior? As I’ve been out of practice a bit in hands on programming I really struggled with the test. Basically I didn’t remember clearly the syntax and got confused with some sentences. My recommendation is that you should study throughly your programming language selection and be ready for tricky questions.

What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a method with the same name.
What are the access privileges in C++? What is the default access level?
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it’s sub-classes. Public members of a class can be accessed by anyone.

What is private inheritance? Why is it useful? Is it different from containment?
What is a nested class? Why can it be useful?
A nested class is a class enclosed within the scope of another class. For example:
// Example 1: Nested class
class OuterClass
class NestedClass
// …
// …
Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if NestedClass is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if NestedClass is private, then only OuterClass’s members and friends can use NestedClass.
When you instantiate as outer class, it won’t instantiate inside class.
What is a local class? Why can it be useful?
local class is a class defined within the scope of a function — any function, whether a member function or a free function. For example:
// Example 2: Local class
int f()
class LocalClass
// …
// …
Like nested classes, local classes can be a useful tool for managing code dependencies.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
Don't miss out!

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.


Check NEW site on stock forex and ETF analysis and automation

Scroll to Top