OOP in C++ Short Quentions
OOP in C++ Short Quentions
The prime purpose of C++ programming was to add object orientation to the C
programming language, which is in itself one of the most powerful programming
languages.
The core of the pure object-oriented programming is to create an object, in code, that
has certain properties and methods. While designing C++ modules, we try to see
whole world in the form of objects. For example a car is an object which has certain
properties such as color, number of doors, and the like. It also has certain methods
such as accelerate, brake, and so on.
There are a few principle concepts that form the foundation of object-oriented
programming −
Object
This is the basic unit of object oriented programming. That is both data and function
that operate on data are bundled as a unit called as object.
Class
When you define a class, you define a blueprint for an object. This doesn't actually
define any data, but it does define what the class name means, that is, what an object
of the class will consist of and what operations can be performed on such an object.
Abstraction
Data abstraction refers to, providing only essential information to the outside world and
hiding their background details, i.e., to represent the needed information in program
without presenting the details.
For example, a database system hides certain details of how data is stored and
created and maintained. Similar way, C++ classes provides different methods to the
outside world without giving internal detail about those methods and data.
Encapsulation
Encapsulation is placing the data and the functions that work on that data in the same
place. While working with procedural languages, it is not always clear which functions
work on which variables but object-oriented programming provides you framework to
place the data and the relevant functions together in the same object.
Inheritance
One of the most useful aspects of object-oriented programming is code reusability. As
the name suggests Inheritance is the process of forming a new class from an existing
class that is from the existing class called as base class, new class is formed called as
derived class.
This is a very important concept of object-oriented programming since this feature
helps to reduce the code size.
Polymorphism
The ability to use an operator or function in different ways in other words giving
different meaning or functions to the operators or functions is called polymorphism.
Poly refers to many. That is a single function or an operator functioning in many ways
different upon the usage is called polymorphism.
Overloading
The concept of overloading is also a branch of polymorphism. When the exiting
operator or function is made to operate on new data type, it is said to be overloaded.
Inheritance: 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.
Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class: The class whose properties are inherited by sub class is called Base Class or
Super class.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
1. Differentiate between class and object?
Answer: Class is a group of similar objects. Class is a blueprint or template from which
objects are created. Object is a real world entity such as pen, laptop, mobile, bed, keyboard,
mouse, chair etc.
A class definition starts with the keyword class followed by the class name; and the class
body, enclosed by a pair of curly braces. A class definition must be followed either by a
semicolon or a list of declarations. For example, we defined the Box data type using the
keyword class as follows –
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
We declare objects of a class with exactly the same sort of declaration that we declare
variables of basic types. Following statements declare two objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
A constructor in C++ is a special 'MEMBER FUNCTION' having the same name as that of
its class which is used to initialize some valid values to the data members of an object. It is
executed automatically whenever an object of a class is created.
In C++, We can have more than one constructor in a class with same name, as long as each
has a different list of arguments. This concept is known as Constructor Overloading.
Overloaded constructors essentially have the same name (exact name of the class) and
differ by number and type of arguments.
3. Why destructors are not overloaded in a class?
Answer: No, we cannot overload a destructor of a class in C++ programming. Only one
empty destructor per class should be there. Destructor in C++ neither takes any parameters
nor does it return anything.
So, multiple destructors with different signatures are not possible in a class. Hence,
overloading is also not possible.
Answer: Static data member are class members that are declared using static keyword A
static member has certain special characteristics These are:
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 to zero when the first object of its class is created .No other
initialization is permitted
It is visible only within the class, but its lifetime is the entire program
5. Name the two type of inheritance supported by C++? Differentiate between signal
inheritance and multiple-inheritance?
class A
{
friend class B;
};
class B
{
};
Class B is declared as a friend of class A in the above code. So, now all the member
functions of class B became friend functions of class A. When the function is declared
as a friend, it can access the private and protected data members of the class.
Answer: The source code must be compiled into machine code in order to become and
executable program. This compilation process is referred to as compile time. A compiled
program can be opened and run by a user. When an application is running, it is called
runtime.
Binding means association of program instruction data to the physical memory location.
Compile time binding means association of instruction data to physical memory and it is
done by compiler.
Similarly load time binding is done by loader and run time binding is done by CPU.
Every object in C++ has access to its own address through an important pointer
called this pointer. The ‘this-pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a ‘this’ pointer, because friends are not members of a class.
Only member functions have a ‘this’ pointer.
In C++, this pointer is used to represent the address of an object inside a member
function. For example, consider an object obj calling one of its member function
say method() as obj.method(). Then, this pointer will hold the address of object obj inside the
member function method(). The ‘this’ pointer acts as an implicit argument to all the member
functions.
10. Which OOP concept is used as a reuse mechanism? Explain with example?
11. What is template class? Define the proper syntax of template class?
12. What are all the operators that cannot be overloaded?
?: (conditional)
. (member selection)
.* (member selection with pointer-to-member)
:: (scope resolution)
sizeof (object size information)
typeid (object type information)
static_cast (casting operator)
const_cast (casting operator)
reinterpret_cast (casting operator)
dynamic_cast (casting operator)
By default access to members of a C++ class is private. The private members are not
accessible outside the class; they can be accessed only through methods of the class. The
public members form an interface to the class and are accessible outside the class.
Protected Access Modifier
The protected keyword is used to create protected members (data and function). The
protected members can be accessed within the class and from the derived class.
Why we declare a function virtual? To let compiler know that the call to this
function needs to be resolved at runtime (also known as late binding and
dynamic linking) so that the object type is determined and the correct version
of the function is called.
Write the syntax of pure virtual function having name as getDATA() and return
type is int?
virtual int getDATA()= 0; \\ syntax of pure virtual function
16. What do you know about abstract class and concrete class?
There are two main types of classes: Abstract Class and Concrete Class. The main
difference between the two arises from the level of implementation of their method
functionalities. Concrete Classes are regular classes, where all methods are completely
implemented. An abstract class is exactly what its name suggests. It is where the functions
are not defined, i.e. they are abstract. It is the base class. Once an abstract class is defined,
it ceases to be abstract and becomes a concrete class. A concrete class is where the
implementations for the member functions are provided. A concrete class is derived from the
base class, i.e. abstract class
Methods May contain partially implemented methods All methods are completely implemented
Functions Some or all declared functions are purely virtual No purely virtual functions
Instantiatio
Cannot be instantiated Can be instantiated
n
17. Why member function do not need to call by object and why?
C++ supports dynamic allocation of objects using the new operator. The memory for
the object is allocated using operator new from heap.
You create an abstract class by declaring at least one pure virtual member function. That's a
virtual function declared by using the pure specifier (= 0) syntax.
catch block will be used to handle the exception that occur within try block. A try block is
always followed by a catch block and we can have multiple catch blocks.
finally block is executed after catch block. We basically use it to put some common code
when there are multiple catch blocks. Even if there is an exception or not finally block gets
executed.
throw keyword will allow you to throw an exception and it is used to transfer control from try
block to catch block.
throws keyword is used for exception handling without try & catch block.
The & is a unary operator that returns the memory address of its operand. Unary
operator: are operators that act upon a single operand to produce a new
value.
29. How static data member is different from non-static give example of each?
30. What is function of copy constructor? Write a statement that use copy constructor?
Exception handling is an integral part of the program. It ensures the normal execution of the
program so that when an error or exceptional situation occurs, the program does not die. It
helps maintain the normal, desired flow of the program even when unexpected events occur.
32. Can we override static method? If not than why?
No, we cannot override static methods, because method overriding is based on dynamic
binding at runtime and the static methods are bonded using static binding at compile time.
So, we cannot override static methods.
The object cloning is a way to create exact copy of an object. The clone () method of Object class
is used to clone an object.