0% found this document useful (0 votes)
167 views13 pages

OOP in C++ Short Quentions

OOP in C++ document discusses the core concepts of object-oriented programming in C++ including objects, classes, abstraction, encapsulation, inheritance, polymorphism, overloading, and more. The key points are: 1. C++ adds object-orientation to the C programming language by allowing programmers to create objects that have properties and methods. 2. The core OOP concepts in C++ are objects, classes, abstraction, encapsulation, inheritance, and polymorphism. Classes are blueprints that define objects, while objects are instances of classes. 3. Abstraction hides background details and shows only essential information. Encapsulation bundles data and functions that work on that data together within classes. Inher

Uploaded by

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

OOP in C++ Short Quentions

OOP in C++ document discusses the core concepts of object-oriented programming in C++ including objects, classes, abstraction, encapsulation, inheritance, polymorphism, overloading, and more. The key points are: 1. C++ adds object-orientation to the C programming language by allowing programmers to create objects that have properties and methods. 2. The core OOP concepts in C++ are objects, classes, abstraction, encapsulation, inheritance, and polymorphism. Classes are blueprints that define objects, while objects are instances of classes. 3. Abstraction hides background details and shows only essential information. Encapsulation bundles data and functions that work on that data together within classes. Inher

Uploaded by

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

OOP in C++

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.

C++ supports operator overloading and function overloading.


 Operator Overloading: The process of making an operator to exhibit different behaviors in
different instances is known as operator overloading.
 Function Overloading: Function overloading is using a single function name to perform
different types of tasks.
Polymorphism is extensively used in implementing inheritance

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

2. What is constructor overloading?

 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.

4. Explain the static data member of a class.

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?

S.NO Single inheritance Multiple inheritance

Single inheritance is one in which Whereas multiple-inheritance is one in


the derived class inherits the which the derived class acquires two or
1. single base class. more base classes.

In single inheritance, the derived While in multiple inheritance, the derived


class uses the features of the class uses the joint features of the
2. single base class. inherited base classes.

Single inheritance requires small While multiple inheritance requires more


run time as compared to multiple run time time as compared to single
3. inheritance due to less overhead. inheritance due to more overhead.

Single inheritance is a lot of close In contrast, multiple inheritance is a lot of


4. to specialization. close to generalization.

While multiple inheritance is


implemented as Class
Single inheritance is implemented DerivedClass_name: access_specifier
as Class DerivedClass_name: Base_Class1, access_specifier
5. access_specifier Base_Class {};. Base_Class2, ….{}; .

6. If class A is a Friend of class B, which member function in class A have to access to


the private member function of B?

Let's see how to make a class a friend of another.

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.

7. Differentiate between compile-time binding and run-time binding?

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.

8. What is operator overloading?

Answer: Operator overloading is a compile-time polymorphism in which the operator is


overloaded to provide the special meaning to the user-defined data type. This means C++ has
the ability to provide the operators with a special meaning for a data type, this ability is
known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using +. Some or all operators like +, = or == are treated as
polymorphic functions and as such have different behaviors depending on the types of its
arguments.

9. Explain the Concept of ‘this’ Pointer.


What is this keyword and its purpose?

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?

Answer: Inheritance is the OOPS concept that can be used as reuse


mechanism. Inheritance allows you to reuse your already written code by inheriting the
properties of written code into other parts of the code, hence allowing you to reuse the
already written code. Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.

11. What is template class? Define the proper syntax of template class?
12. What are all the operators that cannot be overloaded?

Operators which 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)

13. What is a Default constructor and its values in parameter?

A default constructor is a constructor that either has no parameters, or if it has


parameters, all the parameters have default values. In computer programming languages, the
term default constructor can refer to a constructor that is automatically generated by the
compiler in the absence of any programmer-defined constructors (e.g. in Java), and is
usually a nullary constructor.

14. Default access-specifier?

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.

15. How a virtual function is created? When it is used?

A virtual function is a member function in the base class that we expect to


redefine in derived classes. Basically, a virtual function is used in the base
class in order to ensure that the function is overridden. This especially
applies to cases where a pointer of base class points to an object of a
derived class. When we declare a function as virtual in a class, all the sub
classes that override this function have their function implementation as virtual
by default.

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

Abstract Class Concrete Class

Type Base class Default 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?

Which function can be called without using an object?


Explanation: The member functions can be called using only the dot operator or the
arrow operator. But the static members can be called using directly the class name
followed by the scope resolution operator and static member function name. This
is useful when you don't have any object to call the member. Because static member
functions are not attached to a particular object, they can be called directly by using the
class name and the scope resolution operator. 

How can you call a method without instantiating its object?


1) YES, you can use the methods of a class without creating an instance or object of
that class through the use of the Keyword "Static".
18. Where keyword mutable is used?

The keyword mutable is mainly used to allow a particular data member of


const object to be modified.

19. Why New operator?

C++ supports dynamic allocation of objects using the new operator. The memory for
the object is allocated using operator new from heap.

20. How abstract class is created in c++?

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.

virtual void function_name() = 0; // Pure virtual function.

What is one reason for creating abstract classes C++?

The purpose of an abstract class (often referred to as an ABC) is to provide an


appropriate base class from which other classes can inherit. Abstract classes
cannot be used to instantiate objects and serves only as an interface. Attempting to
instantiate an object of an abstract class causes a compilation error.

21. Differentiate b/w try block and catch block?


22. Can one try block have multiple catch blocks? Purpose of final () block?

try block contains set of statements where an exception can occur.

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.

23. What is wrapper class in java?

Wrapper classes provide a way to use primitive data types (int, boolean,


etc..) as objects.

24. What packages does java provide for graphic programming?


 java. Awt
 javax. swing 
25. Differentiate b/w function overloading and function overriding?

 Function Overriding (achieved at run time)


It is the redefinition of base class function in its derived class with same
signature i.e return type and parameters. It can only be done in derived
class.
1) Function Overloading happens in the same class when we declare same
functions with different arguments in the same class. Function Overriding is
happens in the child class when child class redefines parent class function.

3) Overloading happens at the compile time that’s why it is also known as


compile time polymorphism while overriding happens at run time which is why
it is known as run time polymorphism.
4) In function overloading we can have any number of overloaded functions.
In function overriding we can have only one overriding function in the child
class.

26. Write any two names of unary operators in context of pointers?

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.

Types of unary operators:


o unary minus(-)
o increment(++)
o decrement(- -)
o NOT(!)
o Addressof operator(&)
o sizeof()
27. Which operator is used in order to access member function of a class when dynamic
object is created? Give an example to access a function by using dynamic object
28. Write two necessary conditions for polymorphism?

Polymorphism requires inheritance and an overridden function.

29. How static data member is different from non-static give example of each?

A Static variable will have only one single memory location in a class while


a Non-static variable will have more than one or different memory
location for each instance of class.

A Static member of a class has its value shared between all instances of a


class. You cannot have static and non-static member functions with the
same names and the same number and type of arguments. A static
member function does not have a 'this' pointer.
Static method uses compile time or early binding. Non-static method uses runtime or
dynamic binding. Static method cannot be overridden because of early binding. Non-static
method can be overridden because of runtime binding.

A static data member : class variable


A non-static data member : instance variable

30. What is function of copy constructor? Write a statement that use copy constructor?

The copy constructor is a constructor which creates an object by initializing it with an


object of the same class, which has been created previously.

Line( const Line &obj); // copy constructor

31. Why exception handling is used?

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.

33. What is object serialization?

Serialization is a mechanism to convert an object into a sequence of bytes so that it


can be stored in memory. The byte stream, once created, also can be streamed across a
communication link to a remote receiving end. The reverse of serialization is called
deserialization, where the data in the byte stream is used to reconstruct it to its original
object form.

 JDK is a software development kit whereas JRE is a software bundle that


allows Java program to run, whereas JVM is an environment for executing
bytecode.
 The full form of JDK is Java Development Kit, while the full form of JRE is
Java Runtime Environment, while the full form of JVM is Java Virtual
Machine.

Aggregation in Java is a relationship between two classes that is best described as a


"has-a" and "whole/part" relationship.

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.

A package is a namespace that organizes a set of related classes and interfaces

You might also like