[go: up one dir, main page]

0% found this document useful (0 votes)
101 views8 pages

Oops Detailed Notes

The document provides an overview of object-oriented programming concepts in C++ including classes and objects, allocation strategies, access modifiers, constructors, destructors, and the four pillars of OOP - inheritance, encapsulation, polymorphism, and abstraction. It defines classes as templates that encapsulate data and functions into objects. It describes static and dynamic allocation of objects and the public, private, and protected access modifiers. It also explains constructors, destructors, and the key concepts behind each pillar of OOP.

Uploaded by

Kiara
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)
101 views8 pages

Oops Detailed Notes

The document provides an overview of object-oriented programming concepts in C++ including classes and objects, allocation strategies, access modifiers, constructors, destructors, and the four pillars of OOP - inheritance, encapsulation, polymorphism, and abstraction. It defines classes as templates that encapsulate data and functions into objects. It describes static and dynamic allocation of objects and the public, private, and protected access modifiers. It also explains constructors, destructors, and the key concepts behind each pillar of OOP.

Uploaded by

Kiara
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/ 8

Object Oriented Programming

notes by @rithik_codez

1) What are classes and objects?


In C++, a class is a user-defined data type that encapsulates data and functions (also known as member
variables and member functions) into a single entity. It serves as a blueprint or template for creating
objects.

Here's an example of a class in C++:

In the above code, we define a class called Rectangle . It has two private member variables: width
and height , which represent the dimensions of the rectangle. These variables are only accessible
within the class itself.

The class also has two public member functions: setDimensions() and calculateArea() .
The setDimensions() function is used to set the values of width and height , while the
calculateArea() function calculates and returns the area of the rectangle.

Now, we can create objects of the Rectangle class. An object is an instance of a class that can hold
its own data and perform functions defined in the class.
In the above main() function, we create an object called rect1 of the Rectangle class using the
default constructor. Then, we use the object to call the setDimensions() function to set the
dimensions of the rectangle to 5 and 10. Finally, we call the calculateArea() function to
calculate the area of the rectangle and store it in the area variable.

Classes and objects allow us to organize our code and represent real-world entities or concepts in a
structured manner. They provide a way to encapsulate data and related operations, making code more
modular, reusable, and easier to understand and maintain.

2) Types of allocation strategies for objects?


a) Static object allocation is done at compile-time.

• The memory for the object is allocated on the stack.


• Static objects have a fixed lifetime, determined by their scope (e.g., local variables in a function
or global variables).
• Memory for static objects is automatically allocated and deallocated by the system.
• Objects allocated statically cannot be explicitly destroyed or deallocated.

b) Dynamic object allocation is done at run-time.


Dynamic Object Allocation:

• Dynamic object allocation is done at run-time using the new operator.


• The memory for the object is allocated on the heap.
• Dynamic objects have a dynamic lifetime and can be created and destroyed dynamically during
the execution of the program.
• Memory for dynamic objects must be explicitly deallocated using the delete operator to
avoid memory leaks.
• Dynamic object allocation is useful when we need objects that persist beyond the scope in
which they are created or when we need to manage objects dynamically.
3) Access modifiers in object oriented programming
In C++, access modifiers are keywords used to specify the accessibility or visibility of class
members (variables and functions) from different parts of the program. There are three
access modifiers in C++: public , private , and protected .

Public Access Modifier:

• The public access modifier allows the class members to be accessible from anywhere in the
program.
• Public members can be accessed by objects of the class, as well as from outside the class.
• They provide the interface through which external code can interact with the class.

Private Access Modifier:

• The private access modifier restricts the access of class members to within the class only.
• Private members cannot be accessed by objects of the class or from outside the class.
• They are encapsulated within the class and can only be accessed by member functions of the
same class.
• By default, all class members are private if no access modifier is specified.

Protected Access Modifier:

• The protected access modifier is similar to the private modifier, but with a slight
difference.
• Protected members can only be accessed within the class itself and its derived classes.
• They are useful when we want to allow access to certain members for derived classes, but not
for objects of the base class or from outside the class hierarchy.

4) Constructors and types of constructor?


In C++, a constructor is a special member function of a class that is automatically called when
an object of that class is created. It is used to initialize the object's data members and perform
any necessary setup tasks.

Constructors have the same name as the class and do not have a return type, not even void .
They can have parameters or no parameters, depending on the specific requirements.
There are several types of constructors in C++:
Default Constructor:

• A default constructor is a constructor that takes no parameters.


• It is automatically generated by the compiler if no constructor is explicitly defined.
• The default constructor initializes the object's data members with default values (e.g., 0 for
numeric types, empty string for std::string , etc.).
Parameterized Constructor:

• A parameterized constructor is a constructor that takes one or more parameters.


• It allows you to initialize the object's data members with specific values passed as arguments
when creating the object.

Copy Constructor:

• A copy constructor is a constructor that creates a new object by initializing it with an existing
object of the same class.
• It is used to create a copy of an object, allowing you to pass objects by value or return objects
by value.

5) What is destructor?

In C++, a destructor is a special member function of a class that is automatically called when an object
of that class is destroyed or goes out of scope. The destructor is responsible for releasing any resources
allocated by the object and performing any necessary cleanup tasks.
A destructor is defined using the same name as the class, preceded by a tilde (~), and does not take any
parameters. It does not have a return type, not even void . A class can have only one destructor.

Four pillars of OOPS


1) Inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you
to create new classes (derived classes) based on existing classes (base classes). Inheritance
promotes code reuse, hierarchical modeling, and the organization of classes into a parent-
child relationship.

In C++, inheritance is implemented using the class keyword followed by a colon ( : ) and the access
specifier ( public , private , or protected ) followed by the name of the base class.

Here's an example of inheritance in C++


In the above example, we have a base class Animal and a derived class Dog . The derived
class Dog inherits publicly from the base class Animal . This means that the public members
of the base class (such as eat() ) are accessible in the derived class.

The derived class Dog adds its own member function bark() .

You can create objects of both the base and derived classes:

Encapsulation
Encapsulation is one of the core principles of object-oriented programming (OOP) and
involves bundling data and the methods that operate on that data into a single unit called a
class. It provides a way to hide the internal details of an object and restricts access to the
data and methods through well-defined interfaces. Encapsulation promotes data abstraction,
information hiding, and modular design.

In C++, encapsulation is achieved by defining classes and using access specifiers ( public ,
private , and protected ) to control the visibility and accessibility of class members (data and
member functions) from outside the class.
Here are the key aspects of encapsulation in OOP:
Data Hiding:

• Data hiding is the practice of hiding the internal details (data members) of a class from external
access.
• Data members are typically declared as private, making them inaccessible from outside the
class.
• Access to these private members is controlled through public member functions (getters and
setters) that provide controlled and validated access to the data.

Access Control:

• Access specifiers ( public , private , and protected ) control the visibility and
accessibility of class members.
• Public members are accessible from anywhere, including outside the class.
• Private members are accessible only from within the class itself. They are encapsulated
and hidden from external access.
• Protected members are accessible within the class and its derived classes.

Information Hiding:

• Encapsulation allows you to hide the internal implementation details of a class.


• External code interacts with objects of the class through the public interface (public
member functions) without needing to know the implementation details.
• This provides a level of abstraction and protects the internal state and implementation of
the class, allowing changes to be made to the internal implementation without affecting
the external code.

Encapsulation helps in achieving modular and maintainable code by providing clear interfaces,
encapsulating data, and enforcing controlled access to the data and behavior of objects. It
promotes data integrity, code reusability, and separation of concerns.

Abstraction
abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and hiding the
details. Data abstraction refers to providing only essential information about the data to the
outside world, hiding the background details or implementation. 
Data abstraction – This type only shows the required information about the data and hides the
unnecessary data.
1.Control Abstraction – This type only shows the required information about the
implementation and hides unnecessary information.
Abstraction using Classes
We can implement Abstraction in C++ using classes. The class helps us to group data
members and member functions using available access specifiers. A Class can decide which
data member will be visible to the outside world and which is not. 
Abstraction in Header files
One more type of abstraction in C++ can be header files. For example, consider the pow()
method present in math.h header file. Whenever we need to calculate the power of a number,
we simply call the function pow() present in the math.h header file and pass the numbers as
arguments without knowing the underlying algorithm according to which the function is
actually calculating the power of numbers.

Abstraction using Access Specifiers


Access specifiers are the main pillar of implementing abstraction in C++. We can use access
specifiers to enforce restrictions on class members. For example:
•Members declared as public in a class can be accessed from anywhere in the program.
•Members declared as private in a class, can be accessed only from within the class. They
are not allowed to be accessed from any part of the code outside the class.
We can easily implement abstraction using the above two features provided by access
specifiers. Say, the members that define the internal implementation can be marked as
private in a class. And the important information needed to be given to the outside world can
be marked as public. And these public members can access the private members as they are
inside the class. 

Polymorphism
The word “polymorphism” means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. A real-life
example of polymorphism is a person who at the same time can have different characteristics.
A man at the same time is a father, a husband, and an employee. So the same person
exhibits different behavior in different situations. This is called polymorphism. Polymorphism is
considered one of the important features of Object-Oriented Programming.

Compile time polymorphism


A. Function Overloading
When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded, hence this is known as Function Overloading. Functions
can be overloaded by changing the number of arguments or/and changing the type of
arguments. In simple terms, it is a feature of object-oriented programming providing many
functions that have the same name but distinct parameters when numerous tasks are listed
under one function name. There are certain Rules of Function Overloading that should be
followed while overloading a function.

B. Operator Overloading

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 make use of the addition operator (+)
for string class to concatenate two strings. We know that the task of this operator is to add
two operands. So a single operator ‘+’, when placed between integer operands, adds them
and when placed between string operands, concatenates them. 

Run-time polymorphism
A virtual function is a member function that is declared in the base class using the
keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to
perform late binding where the compiler matches the object with the right called function and
executes it during the runtime. This technique falls under Runtime Polymorphism.

You might also like