0% found this document useful (0 votes)
17 views

OOP Quest - Answers

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

OOP Quest - Answers

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

OOP

1. What does OOP stands for?


Object-Oriented Programming.

2. What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm based on
the concept of objects that contain both data and functions.

3. What are Advantages of OOP?


• OOP is faster and easier to execute.
• OOP provides a clear structure for the programs.
• OOP helps to keep the C++ code DRY “Don’t Repeat Yourself”.
• OOP makes the code reusability and easier to maintain, modify.

4. What is the “Don’t Repeat Yourself” (DRY) principle?


(DRY) principle is about reducing the repetition of code by placing common
codes at a single place and reuse them instead of repeating it.

5. What are Classes and Objects?


Classes and objects are the two main aspects of object-oriented
programming.
• A class: is a blueprint for creating objects.
• It contains the attributes and functions that that operate on the data
members.
• Object: An object is an instance of a class.

6. What are the main features (principles)


of OOPs?

There are 4 principles of OOPs are as


follows:
1. Encapsulation
2. Inheritance
2. Data Abstraction
3. Polymorphism
7. How many ways to define functions that belongs to a class?
There are two ways:
1. Inside class definition
2. Outside class definition

8. What is constructor?
A constructor in C++ is a special method that is automatically called
when an object of a class is created.

9. What are types of Constructors?


• Default Constructor: The constructor that takes no argument is called
default constructor.
• Parameterized Constructor: This type of constructor takes the arguments to
initialize the data members.
• Copy Constructor: Copy constructor creates the object from an already
existing object by copying it.
10. What is Destructor?
is another special member function that is called by the compiler when the
scope of the object ends.

11. What is Encapsulation?


Encapsulation is the binding of data and methods
that manipulate them into a single unit “class”
and restricting direct access to some
components.

12. How to achieve Encapsulation?


you must declare class variables/attributes
as private (cannot be accessed from outside the
class). If you want others to read or modify the
value of a private member, you can provide
public get and set methods.

13. What are properties of Encapsulation?


Data Protection: by keeping its data members
private and access them through class’s public
methods.
Information Hiding: hides the internal
implementation details of a class from external
code.
Features of Encapsulation
Below are the features of encapsulation:
1. We cannot access any function from the class directly. We need an
object to access that function that is using the member variables of
that class.
2. The function which we are making inside the class must use only
member variables, only then it is called encapsulation.
3. If we don’t make a function inside the class which is using the member
variable of the class, then we don’t call it encapsulation.
4. Encapsulation improves readability, maintainability, and security by
grouping data and methods together.
5. It helps to control the modification of our data members.

14. What is the role of access modifier?


Access modifier specifiers facilitate Data Hiding in C++ programs by restricting
access to the class member.

15. What are access modifiers?


➢ Private: Private access specifier means that the data member can only be
accessed in the same class.
➢ Protected: A protected access specifier means that the data member can be
accessed in the same class or by derived classes.
➢ Public: Public access specifier means that the data member can be accessed
by any code.
➢ By default, all data members and member functions of a class are
made private by the compiler.

16. What is Abstraction?


Abstraction is similar to data encapsulation and is very important in OOP. It
means showing only the necessary information and hiding the other
irrelevant information from the user. Abstraction is implemented using
classes and interfaces.

17. What is Inheritance?


Inheritance allows a new class (child class) to get properties and functions
from an existing class (parent class). This promotes code reuse and makes
it easy to extend existing functionality without modifying the original code.
➢ Child Class: The class that
inherits properties from another
class is called Sub class or
Derived Class.

➢ Parent Class: The class whose


properties are inherited by a
sub-class is called Base Class
or Superclass.

• 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.

18. What are types of inheritance?


There are 5 types of inheritance:
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
1. Single inheritance

In single inheritance, a class is allowed to inherit from only one class. i.e.
one child class inherits one base class only.

2. Multiple inheritance

In multiple inheritance, a class is allowed to inherit from multiple classes.


i.e. one child’s class inherits more than one base class.

3. Multilevel inheritance

In this type of inheritance, a derived class is created from another derived


class and that derived class can be derived from a base class or any other
derived class. There can be any number of levels.
4. Hierarchical inheritance

In this type of inheritance, more than one subclass is inherited from a single
base class. i.e. more than one derived class is created from a single base
class.
5. Hybrid inheritance

Hybrid Inheritance is implemented by combining more than one type of


inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance will create hybrid inheritance.

19. What are Modes of Inheritance in C++?


There are 3 modes of inheritance:
• Public Mode
• Protected Mode
• Private Mode

Public inheritance: makes public members of the base class public in the
derived class, and the Protected members of the base class Protected in the
derived class.

Protected Inheritance: makes the Public and Protected members of the base
class Protected in the derived class.

Private Inheritance: makes the Public and Protected members of the base
class Private in the derived class.

You might also like