Object Oriented Programming Interview Questions - Part - 4

1. Explain the Structure of C++ programs with example.
A typical C++ program would contain four sections as shown in the following fig.
The include files section or link section provides instructions to the compiler to link functions from the system library. In class declaration section we can declare variables and functions of the class. The member function definitions section contains all the member functions of the class. Every C++ programs must have one main() function section. The main() function section contains two parts, declaration part and executable part. The declaration part declares all the variables used in the executable part. There isatleast one statement in the executable part. All statements in the declaration and executable part end with a semicolon. All sections, except the main function section may be absent when they are not required.
------------------------------------------------------------------------------------------------------
2. Simple C++ program:
The following program shows a simple C++ program that prints a string on the
screen.
#include<iostream.h> //include header file
void main()
{
cout<<”C++ is better than C\n; //C++ structure
}
-----------------------------------------------------------------------------------------------------
Include files
Class declaration
Member function definition
Main function program
3. Explain comment statements in C++.
Comments:
Comments used to write information about the program. The compiler ignores comments, so they do not add to the file size or execution time of the executable program. There are two types of comments in C++, they are
1. Single line comment
2. Multi line comment
Single line comment:
Single line comments start with a double slash symbol(//) and terminate at the end of the line. This comment may start anywhere in the line, and whatever follows till the end of the line is ignored. There is no closing symbol. Single line comments can be written as follows:-
// add two numbers
Multi line command /* …… */

1. Explain Constructors with Example?
A constructor is a special member function for automatic initialization of an object. Whenever an object is created, the special member function will be executed automatically.
The following rules are used for writing a constructor function:
1. A constructor name must be the same as class name.
2. It is declared with no return type.
3. It may be static.
4. It may not be virtual.
5. It should have public or protected access within a class.
The general syntax is
Class user_name
{
private:
__________
protected:
__________
public:user_name();
__________
};
user_name::user_name()
{
__________
}
The following example illustrates the constructor declaration in a class definition
Class integer
{
int m,n;
public:
integer();
……….
};
integer::integer()
{
m=0;
n=0;
}
void main()
{
integer int1;
…………..
}
2. What is parameterized constructor with example?
The constructor that can take arguments are called parameterized constructor. The following examples show the parameterized constructors.
class integer
{
int m,n;
public:
integer(int x,int y); //Parameterized constructor
};
integer::integer(int x,int y)
{
m=x;
n=y;
}
We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways.
1. By calling the constructor explicitly.
2. By calling the constructor implicitly.
The declaration for calling the constructor explicitly is
integer int1=integer(10,20);
This statement causes an integer object int1 and passes the values 10 and 20 to it.
The declaration of calling the constructor implicitly is
integer int1(10,20);
This method, sometimes called the shorthand method.
Example:
#include<iostream.h>
class integer
{
int m,n:
public:
integer(int,int);
void display()
{
cout<<”m=”<<m<<endl;
cout<<”n=”<<n<<endl;
}
};
integer::integer(int x,int y)
{
m=x;
n=y;
}
voidmain()
{
integer int1(10,20);
integer int2=integer(50,100);
int1.display();
int2.display();
}
Output:
m=10
n=20
m=50
n=100
3. Explain Multiple Constructors in a Class (Constructor Overloading)?
We can use more than one constructor in a same class. This is known as Multiple
Constructor. For example we could define a class as follows.
Class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
}
integer(int x)
{
m=x;
n=x;
}
integer(int x, int y)
{
m=x;
n=y;
}
};
This declares three constructors for an integer object. The first constructor receives no argument, the second receives one argument and the third receives two arguments. The declaration
integer I1;
Would automatically invoke the first constructor and set both m and n of I1 to 0. The declaration,
Integer I2(10);
Would call the second constructor and set both m and n of I2 to 10.Finally the statements
Integer I3(10,20);
Would call the third constructor and set m to 10 and n to 20.
4. What is Dynamic Initialization of objects?
The initial values of an object may be provided during run time. This is known as dynamic initialization. The advantages of dynamic initialization is that we can provide various initializations using overloaded constructors. For example
#include<iostream.h>
class integer
{
int m,n,a;
public:
integer()
{
m=0;
n=0;
a=0;
}
integer(int x,int y)
{
m=x;
n=y;
a=x+y;
}
};
voidmain()
{
integer int1(10,20);
int1.display();
}
5. What is Copy Constructor?
The constructor that creates a new class object from an existing object of the same class. The copy constructors are used in the following situations:
1 The initialization of object by another object of the same class.
2 Return of objects as a function value
The general format of copy constructor is
Class_name::class_name(class_name &ptr);
The symbolic representation of the above format is
x::x(x &ptr)
where x is user defined class name and ptr is pointer to a class object x.
Normally the copy constructor take an object of their own class as argument and produce
such an object. The copy constructor usually does not return a function value.
Example:
Class alpha
{
int data;
public:
alpha()
{}
alpha(int d)
{
data a=d;
}
alpha(alpha &a)
{
data=a.data;
}
void display()
{
cout<<data;
}
};
void main()
{
alpha a1(48);
alpha a2(a1);
cout<<”a1:”;
a1.display();
cout<<”a2:”;
a2.display();
}
In this example the statement
Alpha a2(a1);
Invokes the copy constructor.

2. Define a class and an object. Identify five classes in the hall in which you are writing the exam. What are the data members and member functions of any three classes you have identified?
Class:
A class is defined as a template or a model for creating objects. The class is created using the keyword class. A class declaration defines a new type that links code and data. This new type is then used to declare objects.
Object:
Objects are the instances of the class. Objects are the basic runtime entities that have data and code to manipulate the data. The syntax used to create object is no different than the syntax used to declare a variable.
Example:  sample x;
The five classes that are identified as,
1. Student.
2. Invigilator.
3. Class_room.
4. Exam_details.
5. Class_type.
class  student
{
 int stud_no,age;
 char sname[15],sex[7];
 char course[10],dept[10];
public:
void getdetails( );
void displaydetails( );
};
class invigilator
{
  int inv_no;
  char inv_name[15];
   char designation[15],dept[15];
public:
void getdata( );
void displaydata( );
};
class class_room
{
int no_of_studs;
int no_of_chairs, no_of_fans, no_of_tubes;
public:
void getroom( );
void displayroom( );
};
3. Explain the following with example:
(a) Friend functions.
Friend functions are the functions that are declared inside the class with the preceded keyword friend. The definition of the function is given somewhere else. Using this friend function the private functions inside the class can be made accessible to outside the class also.(ie. It has full access rights to private members of the class). The function definition does not use the keyword friend or the scope resolution operator (::).
Example:
class sample {
int a,b;
public:
friend int sum(sample x);
void set(int i,int j );
};
void sample::set (int i,int j)
{
a=i;
b=j;
}
int sum(sample x)
{
return x.a + x.b;
}
void  main()
{
sample n;
n.set(10,15);
cout<<sum(n);
}
(b) Friend classes
It is possible for one class to be a friend of another class. When this is the case the friend class and all its member functions have access to the private member is defined within other class. The friend class is declared inside the class with the keyword friend preceded to class name.
Example:
class values{
int a,b;
public:
values(int i, int j)
{ a=i;b=j;}
friend class minimum;
};
class minimum {
public:
int minval(values x);
};

int minimum::minval(values x)
{
return x.a<x.b?x.a:x.b;
}
void main( )
{
values g(10,20);
minimum m;
cout<<m.minimum(g);
}
(c) Static data members.
Static data members are normally used to maintain the values common to the entire class. It is initialized to zero when the first object of that class is created. Individual copy of the static data member is not made for each object. Only one copy of that member is created for the entire class and shared by all the objects of that class. It is visible within the class and its lifetime is entire program.
Example:
class test {
static int a;
int b;
public:
void set(int i, int j)
{ a=i;b=j; }
void show( );
};

int test::a; //define a

void test::show( )
{
cout<<”This is static data:”<<a;
cout<<endl<<”This is nonstatic data:”<<b;
cout<<endl;
}
void main( )
{
test x,y;

x.set(2,3);
x.show( );
y.set(4,5);
y.show( );
x.show( );
}
4. Write a program to create Employee class. Identify suitable members and member functions. Calculate salary for n employees using dynamic memory allocation.
#include<iostream.h>
#include<conio.h>
class employee
{
int eno,age;
char name[20],desg[20];
float bpay,hra,da,ta,pf,gpay,npay;
public:
void get();
void cal();
void disp();
};

void employee::get()
{
cout<<endl<<"Employee Records"<<endl;
cout<<endl<<"Enter the employee number :";
cin>>eno;
cout<<endl<<"Enter the employee name :";
cin>>name;
cout<<endl<<"Enter the employee age :";
cin>>age;
cout<<endl<<"Enter the employee designation :";
cin>>desg;
cout<<endl;
}
void employee::cal()
{
cout<<endl<<"Enter the employee basic pay :";
cin>>bpay;
hra=bpay*0.1;
da=bpay*0.2;
ta=bpay*0.3;
pf=bpay*0.4;
gpay=bpay+hra+da+ta;
npay=gpay-pf;
}
void employee::disp()
{
cout<<endl<<"Employee records";
cout<<endl<<"................"<<endl<<endl;
cout<<"Employee Number         "<<eno<<endl;
cout<<"Employee Name           "<<name<<endl;
cout<<"Employee Age            "<<age<<endl;
cout<<"Employee Designation    "<<desg<<endl;
cout<<"Employee Basic pay      "<<bpay<<endl;
cout<<"Hra                    "<<hra<<endl;
cout<<"Da                "<<da<<endl;
cout<<"Ta                      "<<ta<<endl;
cout<<"Pf                      "<<pf<<endl;
cout<<"Gross pay               "<<gpay<<endl;
cout<<"Net pay                 "<<npay<<endl;
}
void main()
{
employee *p;
int n,i;
clrscr();
cout<<endl<<"Please enter how many employees:";
cin>>n;
p=new employee[n];
for(i=0;i<n;i++)
{
(p+i)->get();
(p+i)->cal();
}
for(i=0;i<n;i++)
(p+i)->disp();
getch();
}

Define constructor
A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it
Constructs the values of data members of the class
Eg:
Class integer
{
……
public:
integer( );//constructor
………
}
A
B
C
Student
Test
Result
Sports
The different types of constructor are
i. default constructor
The constructor with no arguments is called default constructor
Eg:
Class integer
{
int m,n;
Public:
Integer( );
…….
};
integer::integer( )//default constructor
{
m=0;n=0;
}
the statement
integer a;
invokes the default constructor
ii. parameterized constructor
constructor with arguments is called parameterized constructor
Eg;
Class integer
{ int m,n;
public:
integer(int x,int y)
{ m=x;n=y;
}
To invoke parameterized constructor we must pass the initial values as arguments to the
constructor function when an object is declared. This is done in two ways
1.By calling the constructor explicitly
eg:
integer int1=integer(10,10);
2.By calling the constructor implicitly
eg:
Integer int1(10,10);
iii. Default argument constructor
The constructor with default arguments are called default argument constructor
Eg:
Complex(float real,float imag=0);
The default value of the argument imag is 0
The statement
complex a(6.0)
assign real=6.0 and imag=0
the statement
complex a(2.3,9.0)
assign real=2.3 and imag=9.0
iv. Copy constructor
A copy constructor is used to declare and initialize an object from another object. It
takes a reference to an object of the same class as an argument
Eg: integer i2(i1);
would define the object i2 at the same time initialize it to the values of i1.
Another form of this statement is
Eg: integer i2=i1;
The process of initializing through a copy constructor is known as copy initialization.
v. Dynamic constructor
Allocation of memory to objects at time of their construction is known as dynamic
constructor. The memory is allocated with the help of the NEW operator
Eg:
Class string
{
char *name;
int length;
public:
string( )
{
length=0;
name=new char[length +1];
}
void main( )
{
string name1(“Louis”),name3(Lagrange);
}
use delete to free that memory.

9. Explain about Multiple constructors (constructor overloading)?
The class that has different types of constructor is called multiple constructors
Eg:
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer( ) //default constructor
{
m=0;n=0;
}
integer(int a,int b) //parameterized constructor
{
m=a; n=b;
}
integer(&i) //copy constructor
{
m=i.m;
n=i.n;
}
void main()
{
integer i1; //invokes default constructor
integer i2(45,67);//invokes parameterized constructor
integer i3(i2); //invokes copy constructor
}
The special characteristics of constructor are
1 They should be declared in the public section
2 They are invoked automatically when the objects are created
3 They do not have return types, not even void and therefore, and they cannot return values
4 They cannot be inherited, though a derived class can call the base class
5 They can have default arguments
6 Constructors cannot be virtual function