Professional Documents
Culture Documents
OOPS Guess Paper
OOPS Guess Paper
Q3.Define Class?
A Class is a collection of objects of similar type.
The classes are user-defined data types and behave like built-in types of a
programming language.
A class is a way to bind the data and its associated functions together.
A class is a user-defined data type with a template that serves to define its properties.
A class is a blueprint that defines the variables & the methods common to all objects
of a certain kind.
Q7.Define polymorphism?
Polymorphism means the ability to take more than one form. For example, an
operation may exhibit different behavior in different instances. The behavior depends
upon the types of data used in the operation.
Example
#include<iostream>
using namespace std;
class Employee
{
int id;
char name[30];
public:
void getdata();//Declaration of function
void putdata();//Declaration of function
};
void Employee::getdata(){//Defining of function
cout<<"Enter Id : ";
cin>>id;
cout<<"Enter Name : ";
cin>>name;
}
void Employee::putdata(){//Defining of function
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}
int main(){
Employee emp; //One member
emp.getdata();//Accessing the function
emp.putdata();//Accessing the function
return 0;
1.Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public members of
a class can be accessed from anywhere in the program using the direct member
access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed directly
by any object or function outside the class. Only the member functions or
the friend functions are allowed to access the private data members of the class.
Unit 2
Part I
Q1.Define constructor.?
of its class. It is special because its name is same as class name. The
class
Q2.Differentiate Structure and Classes?
Structures Classes
1. By default the members 1.By default the members of
of the Class are
structure are public. private.
2. Data hiding is not 2.Data hiding is possible
possible
3.sturcture data type 3.Objects can be treated like
cannot be built-in-
treated like built-in-type types by means of operator
overloading.
4.Structure are used 4. Classes are used to hold
only for data and
holding data functions
5.Keyword „struct‟ 5.Keyword „class‟
// Driver Code
int main()
{
// Statement 1
cout << sum(10, 15) << endl;
// Statement 2
cout << sum(10, 15, 25) << endl;
// Statement 3
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
Q6.What is a destructor?
Destructor is an instance member function that is invoked automatically whenever an
object is going to be destroyed. Meaning, a destructor is the last function that is going
to be called before an object is destroyed.
A destructor is also a special member function like a constructor. Destructor
destroys the class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the constructor.
Hence destructor can-not be overloaded.
Destructor neither requires any argument nor returns any value.
It is automatically called when an object goes out of scope.
Destructor release memory space occupied by the objects created by the
constructor.
In destructor, objects are destroyed in the reverse of an object creation.
Part-II
Q1.Difference between Constructor and Destructor?
S.
No. Constructor Destructor
Function overloading means we can use the same function name to create
Eg: An overloaded add ( ) function handles different data types as shown below.
// Declarations
iv. int add( int a, int b); //add function with 2 arguments of same type
v. int add( int a, int b, int c); //add function with 3 arguments of same type
vi. double add( int p, double q); //add function with 2 arguments ofdifferent type
(3 , 4);
A friend class can access private and protected members of other classes in which it
is declared as a friend. It is sometimes useful to allow a particular class to access
private and protected members of other classes. For example, a LinkedList class may
be allowed to access private members of Node.
#include <iostream>
using namespace std;
class GFG {
private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}
// Driver code
int main()
{
GFG g;
F fri;
fri.display(g);
return 0;
}
Friend Function
Like a friend class, a friend function can be granted special access to private and
protected members of a class in C++. They are the non-member functions that can
access and manipulate the private and protected members of the class for they are
declared as friends.
If a function is defined as a friend function in C++, then the protected and private data
of a class can be accessed using the function.
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the
body of a class starting with the keyword friend.
Function overloading
Operator overloading
Function Overloading is defined as the process of having two or more function with the
same name, but different in parameters is known as function overloading in C++. In
function overloading, the function is redefined by using either different types of
arguments or a different number of arguments. It is only through these differences
compiler can differentiate between the functions.
1. #include <iostream>
2. using namespace std;
3. class Cal {
4. public:
5. static int add(int a,int b){
6. return a + b;
7. }
8. static int add(int a, int b, int c)
9. {
10. return a + b + c;
11. }
12. };
13. int main(void) {
14. Cal C; // class object declaration.
15. cout<<C.add(10, 20)<<endl;
16. cout<<C.add(12, 20, 23);
17. return 0;
18. }
C++ Operators Overloading
Operator overloading is a compile-time polymorphism in which the operator is
overloaded to provide the special meaning to the user-defined data type. Operator
overloading is used to overload or redefines most of the operators available in C++. It
is used to perform the operation on the user-defined data type. For example, C++
provides the ability to add the variables of the user-defined data type that is applied to
the built-in data types.
Rules for Operator Overloading
o Existing operators can only be overloaded, but the new operators cannot be
overloaded.
o The overloaded operator contains atleast one operand of the user-defined data
type.
o We cannot use friend function to overload certain operators. However, the
member function can be used to overload those operators.
o When unary operators are overloaded through a member function take no explicit
arguments, but, if they are overloaded by a friend function, takes one argument.
o When binary operators are overloaded through a member function takes one
explicit argument, and if they are overloaded through a friend function takes two
explicit arguments.
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
function body
class. It supports the concept of hierarchical classification. It provides the idea of reusability.
We can add additional features to an existing class without modifying it by deriving a new
An abstract class is one that is not used to create objects. An abstract class is
designed only to act as a base class. It is a design concept is program development
and provides a base upon which other classes may be built.
Part-II
Q1.Explain Inheritance and its Types?
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class” and
the existing class is known as the “base class” or “parent class”. The derived class
now is said to be inherited from the base class.
When we say derived class inherits the base class, it means, the derived class
inherits all the properties of the base class, without changing the properties of base
class and may add new features to its own. These new features in the derived class
will not affect the base class. The derived class is the specialized class for the base
class.
Sub Class: The class that inherits properties from another class is called Subclass
or Derived Class.
Super Class: The class whose properties are inherited by a subclass is called
Base Class or Superclass.
Types of Inheritance
C++ Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited
from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.
When one class inherits another class which is further inherited by another class, it is
known as multi level inheritance in C++. Inheritance is transitive so the last derived
class acquires all the members of all its base classes.
Classes having virtual functions are not Base class containing pure virtual function
abstract. becomes abstract.
Syntax:
Syntax:
virtual<func_type><func_name>()
{ virtual<func_type><func_name>()
// code = 0;
Base class having virtual function can be Base class having pure virtual function becomes
instantiated i.e. its object can be made. abstract i.e. it cannot be instantiated.
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Q4.How ambiguity is resolved in inheritance?
In the inheritance when more than one class has a function with the same name and same type signature, then there exists
ambiguity. It can be solved by using the class resolution operation with the function
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
Unit 4
Part-I
Q1.Define Runtime Polymorphism?
At runtime, when it is known what class objects are under consideration, the
appropriate version of the function is invoked. Since the function is linked with a
particular class much later after its compilation, this process is termed as „late binding‟.
It is also known as dynamic binding because the selection of the appropriate function is
done dynamically at run time. This runtime polymorphism can be achieved by the use
of pointers to objects and virtual functions
Q2.What is dynamic binding?
Dynamic binding in C++ is a practice of connecting the function calls with the function
definitions by avoiding the issues with static binding, which occurred at build time.
Because dynamic binding is flexible, it avoids the drawbacks of static binding, which
connected the function call and definition at build time.
Q3.Explain virtual function?
A virtual function (also known as virtual methods) is a member function that is
declared within a base class and is re-defined (overridden) by a derived class. When
you refer to a derived class object using a pointer or a reference to the base class,
you can call a virtual function for that object and execute the derived class’s version
of the method.
Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for the function call.
They are mainly used to achieve Runtime polymorphism.
Functions are declared with a virtual keyword in a base class.
The resolving of a function call is done at runtime.
C++ has the ability to provide the operators with a special meaning for a data
type. This mechanism of giving such special meanings to an operator is known
as Operator overloading. It provides a flexible option for the creation of new
definitions for C++ operators.
The operators that cannot be overloaded are.
When we define the data member of a class using the static keyword, the data
members are called the static data member. A static data member is similar to the
static member function because the static data can only be accessed using the static
data member or static member function. And, all the objects of the class share the
same copy of the static member to access the static data.
Part-II
Q1.Explain Constant data member and member function?
Constant member functions are those functions that are denied permission to change
the values of the data members of their class. To make a member function constant,
the keyword const is appended to the function prototype and also to the function
definition header.
Like member functions and member function arguments, the objects of a class can
also be declared as const. An object declared as const cannot be modified and
hence, can invoke only const member functions as these functions ensure not to
modify the object. A const object can be created by prefixing the const keyword to the
object declaration. Any attempt to change the data member of const objects results in
a compile-time error.
Important Points
When a function is declared as const, it can be called on any type of object, const
object as well as non-const objects.
Whenever an object is declared as const, it needs to be initialized at the time of
declaration. however, the object initialization while declaring is possible only with
the help of constructors.
A function becomes const when the const keyword is used in the function’s
declaration. The idea of const functions is not to allow them to modify the object on
which they are called.
It is recommended practice to make as many functions const as possible so that
accidental changes to objects are avoided.
It takes place at compile time which is referred It takes place at runtime so it is referred to as
to as early binding late binding
Execution of static binding is faster than Execution of dynamic binding is slower than
dynamic binding because of all the information static binding because the function call is not
needed to call a function. resolved until runtime.
Real objects never use static binding Real objects use dynamic binding
Q4. Explain static data member in detail?
Static data members are class members that are declared using static keywords. A
static member has certain special characteristics which are as follows:
Only one copy of that member is created for the entire class and is shared by all
the objects of that class, no matter how many objects are created.
It is initialized before any object of this class is created, even before the main
starts.
It is visible only within the class, but its lifetime is the entire program .
The static is a keyword in the C and C++ programming language. We use the
static keyword to define the static data member or static member function inside
and outside of the class. Let's understand the static data member and static
member function using the programs.
class B {
static A a;
public:
B()
{
cout << "B's Constructor Called " <<
endl;
}
};
int main()
{
B b;
return 0;
}
Q5.What are the different types of binding of object with pointer?
When you have a pointer to an object, the object may actually be of a class that is
derived from the class of the pointer.
Thus there are two types:
1.Static binding 2. Dynamic binding
Unit 5
Part I
Q1.Differentiate between function template & function overloading.
2. Here the data type upon which the function 2.Here the function can operate on only one
operates is passed as parameter data type. For another data type we have to
redefine the function with same
name with different argument list.
Part II
Q1.What is templates?
A template is a simple yet very powerful tool in C++. The simple idea is to pass the
data type as a parameter so that we don’t need to write the same code for different
data types. For example, a software company may need to sort() for different data
types. Rather than writing and maintaining multiple codes, we can write one sort()
and pass the datatype as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘type name’. The
second keyword can always be replaced by the keyword ‘class’.
Function Templates
We write a generic function that can be used for different data types. Examples of
function templates are sort(), max(), min(), printArray().
Class Templates
Class templates like function templates, class templates are useful when a class
defines something that is independent of the data type. Can be useful for classes like
LinkedList, BinaryTree, Stack, Queue, Array, etc.
#include <iostream>
private:
T* ptr;
int size;
public:
void print();
};
template <typename T> Array<T>::Array(T arr[], int s)
size = s;
ptr[i] = arr[i];
int main()
int arr[5] = { 1, 2, 3, 4, 5 };
a.print();
return 0;
}
Q2.What is the difference between function overloading and templates?
Both function overloading and templates are examples of polymorphism features of
OOP. Function overloading is used when multiple functions do quite similar (not
identical) operations, templates are used when multiple functions do identical
operations.
File handling in C++ is a mechanism to store the output of a program in a file and help
perform various operations on it. Files help store these data permanently on a storage
device.
To read and write from a file we are using the standard C++ library called fstream. Let
us see the data types define in fstream library is:
fstream It is used to create files, write information to files, and read information
from files.
ifstream It is used to read information from files.
1. ios class is topmost class in the stream classes hierarchy. It is the base class
for istream, ostream, and streambuf class.
2. istream and ostream serves the base classes for iostream class. The
class istream is used for input and ostream for the output.
3. Class ios is indirectly inherited to iostream class using istream and ostream. To
avoid the duplicity of data and member functions of ios class, it is declared as
virtual base class when inheriting in istream and ostream as
1. The ios class: The ios class is responsible for providing all input and output
facilities to all other stream classes.
2. The istream class: This class is responsible for handling input stream. It provides
number of function for handling chars, strings and objects such as get, getline,
read, ignore, putback etc..