Oops Detailed Notes
Oops Detailed Notes
notes by @rithik_codez
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.
• 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.
• 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.
• 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.
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:
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.
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.
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 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.
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.
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.