UNIT 1 OOP Notes
UNIT 1 OOP Notes
A class contains data members (variables) and member functions. These member functions
are used to manipulate the data members inside the class.
Abstraction
Abstraction helps in the data-hiding process. It helps in displaying the essential features
without showing the details or the functionality to the user. It avoids unnecessary information
or irrelevant details and shows only that specific part that the user wants to see. The benefits
of Abstraction includes:
1. Simplified Interface: Abstraction allows hiding complex implementation details and
exposing a simplified interface to the user. This promotes ease of use and reduces
complexity, as users can interact with high-level abstractions without needing to understand
the underlying complexities.
2. Code Maintenance: By focusing on essential features and hiding unnecessary
implementation details, abstraction improves code maintainability. Changes made to the
underlying implementation are isolated, reducing the impact on other parts of the codebase.
3. Flexibility: Abstraction enables the creation of generalized interfaces, which can be
implemented differently for various use cases. This provides flexibility and allows developers
to adapt and extend the functionality of the code without affecting the code using abstraction.
Encapsulation
The wrapping up of data and functions together in a single unit is known as encapsulation. It
can be achieved by making the data members' scope private and the member function’s scope
public to access these data members. Encapsulation makes the data non-accessible to the
outside world. The benefits of encapsulation include:
1. Data Protection: Encapsulation hides the internal implementation details of an object,
protecting the data from unauthorized access or modification. It provides data integrity and
maintains the consistency of the object's state.
2. Code Organization: Encapsulation helps organize code by grouping related data and
functions together within a class. This improves code readability and maintainability by
providing a clear structure and reducing code clutter.
3. Code Reusability: Encapsulation facilitates code reusability by encapsulating functionality
within objects. Objects can be easily reused in different parts of the code or in different
projects, promoting efficient development and reducing redundant code.
Inheritance
Inheritance is the process in which two classes have an is-a relationship among each other
and objects of one class acquire properties and features of the other class. The class which
inherits the features is known as the child class, and the class whose features it inherited is
called the parent class. For example, Class Vehicle is the parent class, and Class Bus, Car,
and Bike are child classes. The benefits of Inheritance include:
1. Code Reusability: Inheritance allows classes to inherit properties and behavior from a base
class, promoting code reuse. Derived classes can extend and specialize the functionality of
the base class, eliminating the need to rewrite common code.
2. Polymorphism: Inheritance enables polymorphism, where objects of derived classes can be
treated as objects of their base class. This allows for more flexible and dynamic
programming, as different derived classes can be used interchangeably through a common
interface.
3. Hierarchical Organization: Inheritance establishes a hierarchical relationship among
classes, reflecting real-world or conceptual hierarchies. This improves code organization,
understandability, and maintainability by representing relationships and dependencies in a
structured manner.
Polymorphism
Polymorphism means many forms. It is the ability to take more than one form. It is a feature
that provides a function or an operator with more than one definition. It can be implemented
using function overloading, operator overload, function overriding, and virtual functions. The
benfits include:
1. Code Flexibility: Polymorphism allows different objects to be treated uniformly through a
common interface. This promotes code flexibility, as functions or algorithms can work with
objects of different types without needing to know their specific implementations.
2. Code Extensibility: Polymorphism enables the addition of new derived classes that adhere
to the same interface without modifying existing code. This extensibility simplifies code
maintenance and allows for easy integration of new features.
3. Code Readability: Polymorphism enhances code readability by expressing intent and
behavior through the use of abstract interfaces. It allows for more concise and expressive
code, as the focus is on what needs to be done rather than how it is done.
Advantages of OOPs
There are various advantages of object-oriented programming.
OOPs provide reusability to the code and extend the use of existing classes.
In OOPs, it is easy to maintain code as there are classes and objects, which helps in
making it easy to maintain rather than restructuring.
It also helps in data hiding, keeping the data and information safe from leaking or getting
exposed.
Object-oriented programming is easy to implement.
Troubleshooting is easier with the OOP language.
Code Reusability.
Productivity.
Data Redundancy.
Code Flexibility.
Solving problems.
Security.
The four pillars of OOPs (object-oriented programming) are data abstraction,
encapsulation, inheritance, and polymorphism.
1. Data abstraction is the process of hiding unnecessary details of an object’s internal
structure. By abstracting an object’s data, its structure and behavior can be kept separate
and more easily understood.
2. Encapsulation is the process of wrapping data and related functions into a single unit
(object). Encapsulation limits access to object data and methods, preventing their misuse
and ensuring their proper functioning.
3. Inheritance is the ability to create a new class (child class) from an existing one (parent
class). The child class typically inherits the attributes (members and methods) of the
parent class, although it can also redefine them.
4. Polymorphism is the ability of an object to take on multiple forms. This allows objects of
different classes to be used interchangeably, as long as they implement a certain interface
(have methods of the same name).
Four pillars of OOPs enable developers to create robust and maintainable code, by
grouping related data and behavior into objects that can be easily understood and reused.
By implementing these pillars in object-oriented programming languages, developers can
create reliable and scalable software solutions.
How to Use the Four Pillars of OOPs in Your Projects
Object-oriented programming (OOP) uses Four pillars of OOPs to create efficient software
systems: encapsulation, abstraction, inheritance, and polymorphism. These four pillars
provide the foundation for writing maintainable and scalable code.
1. Encapsulation is a way to keep code organized and separate from other codes in the
system. It is done by using classes that contain all the code related to a specific feature or
subject.
2. Abstraction is the process of hiding internal details or processes from the user. This is
done by creating classes that represent real-world objects and their attributes.
3. Inheritance is when a class inherits the methods and properties of another class. This
allows developers to reuse code and easily add new features.
4. Polymorphism allows developers to create multiple methods with the same name, but
with different implementations. This makes it easier to create flexible systems that are
easier to understand and maintain.
When used together, these four pillars of OOPs can help developers create robust, efficient,
and maintainable software systems. By using OOP, developers can create software with
fewer bugs, better performance, and a shorter development cycle.
The Benefits of Incorporating the Four Pillars of OOPs
Object-Oriented Programming (OOP) is a powerful programming methodology that is widely
used in software development. Four pillars of OOPs that work together to create robust and
efficient software applications: encapsulation, abstraction, inheritance, and polymorphism.
Encapsulation refers to the process of binding data and the code which performs operations
on the data into a single unit. This allows developers to hide the data from other parts of the
program, making the code more secure and reliable.
Abstraction is the process of representing the essential features of an object without
specifying the underlying implementation details. This allows developers to create high-level
abstractions of complex tasks, making the code easier to work with.
Inheritance allows developers to define a class once, and then create other classes which are
based on the original class. This allows developers to create a class hierarchy, making it
easier to modify and extend the code.
Polymorphism is the ability to have multiple implementations of a method depending on the
data type. This allows developers to create code that can handle objects of different types.
By incorporating these four pillars, developers can create more robust, efficient, and secure
software applications. The pillars provide developers with a powerful and flexible toolset to
work with, making applications easier to maintain and extend.
the Four pillars of OOPs, object-oriented programming (OOP) is a programming language
model that focuses on representing objects and their interactions. It is one of the most popular
programming paradigms and has been around since the 1960s. OOP is based on the idea of
creating objects that contain both data and functions that can manipulate that data. This
makes software development more efficient and helps to avoid code duplication.
The four pillars of OOPs are abstraction, encapsulation, inheritance, and polymorphism.
Abstraction provides the ability to hide details, allowing for simpler representations of
objects. Encapsulation allows for data and functions to be stored in one place, increasing
security and allowing for easy access.
Inheritance is the ability to define properties and methods from a parent class to a child class.
Finally in the Four pillars of OOPs, polymorphism allows for objects to take on different
forms, which is particularly useful when dealing with data of varying types.
Together, these Four pillars of OOPs form the foundation and make it easier for developers to
create maintainable and reusable code. Abstraction helps to manage complexity and makes it
easier to develop complex software, while encapsulation and inheritance allow for code to be
reused and reduces the amount of development time needed. Polymorphism makes it easier to
develop programs that can deal with different types of data more efficiently.
Overall in the Four pillars of OOPs, the four pillars of OOPs help to reduce complexity,
improve maintainability, and make software development easier. By using abstraction,
encapsulation, inheritance, and polymorphism, software developers can create more efficient
and maintainable code that can be reused and modified more easily.
Structure of C++ programming
A C++ program is structured in a specific and particular manner. In C++, a program is
divided into the following three sections:
1. Standard Libraries Section
2. Main Function Section
3. Function Body Section
For example, let’s look at the implementation of the Hello World program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Standard libraries section
include <iostream>
using namespace std
#include is a specific preprocessor command that effectively copies and pastes the
entire text of the file, specified between the angle brackets, into the source code.
The file <iostream>, which is a standard file that should come with the C++ compiler,
is short for input-output streams. This command contains code for displaying and
getting an input from the user.
namespace is a prefix that is applied to all the names in a certain set. iostream file
defines two names used in this program - cout and endl.
This code is saying: Use the cout and endl tools from the std toolbox.
Main function section
int main() {}
The starting point of all C++ programs is the main function.
This function is called by the operating system when your program is executed by the
computer.
{ signifies the start of a block of code, and } signifies the end.
Function body section
cout << "Hello World" << endl;
return 0;
The name cout is short for character output and displays whatever is between
the << brackets.
Symbols such as << can also behave like functions and are used with the
keyword cout.
The return keyword tells the program to return a value to the function int main
After the return statement, execution control returns to the operating system
component that launched this program.
Execution of the code terminates here.
C++ Data Types
All variables use data type during declaration to restrict the type of data to be stored.
Therefore, we can say that data types are used to tell the variables the type of data they can
store. Whenever a variable is defined in C++, the compiler allocates some memory for that
variable based on the data type with which it is declared. Every data type requires a
different amount of memory.
C++ supports a wide variety of data types and the programmer can select the data type
appropriate to the needs of the application. Data types specify the size and types of values
to be stored. However, storage representation and machine instructions to manipulate each
data type differ from machine to machine, although C++ instructions are identical on all
machines.
C++ supports the following data types:
1. Primary or Built-in or Fundamental data type
2. Derived data types
3. User-defined data types
Data Types in C++ are Mainly Divided into 3 Types:
1. Primitive Data Types: These data types are built-in or predefined data types and can be
used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive
data types available in C++ are:
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
2. Derived Data Types: Derived data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. These can be of four types namely:
Function
Array
Pointer
Reference
3. Abstract or User-Defined Data Types: Abstract or User-Defined data types are defined
by the user itself. Like, defining a class in C++ or a structure. C++ provides the following
user-defined datatypes:
Class
Structure
Union
Enumeration
Typedef defined Datatype
Primitive Data Types
Integer: The keyword used for integer data types is int. Integers typically require 4
bytes of memory space and range from -2147483648 to 2147483647.
Character: Character data type is used for storing characters. The keyword used for the
character data type is char. Characters typically require 1 byte of memory space and
range from -128 to 127 or 0 to 255.
Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean
variable can store either true or false. The keyword used for the Boolean data type
is bool.
Floating Point: Floating Point data type is used for storing single-precision floating-
point values or decimal values. The keyword used for the floating-point data type
is float. Float variables typically require 4 bytes of memory space.
Double Floating Point: Double Floating Point data type is used for storing double-
precision floating-point values or decimal values. The keyword used for
the double floating-point data type is double. Double variables typically require 8
bytes of memory space.
void: Void means without any value. void data type represents a valueless entity. A
void data type is used for those function which does not return a value.
Wide Character: Wide character data type is also a character data type but this data
type has a size greater than the normal 8-bit data type. Represented by wchar_t. It is
generally 2 or 4 bytes long.
sizeof() operator: sizeof() operator is used to find the number of bytes occupied by
a variable/data type in computer memory.
Derived Data types in C++
Derived Data Types are data types that are created by combining primitive or built-in
datatypes. There are four different types of derived data types. These are :
1. Function
A function is a code segment or a block of code defined to accomplish a specific purpose. A
function is often designed to spare the user from repeatedly writing the same lines of code for
the same input. All the lines of code are combined into a single function that may be invoked
from anywhere. Every C++ application includes a default function called main().The function
also has a return type, which is used to specify the type of data the function would return
when its execution is complete. The function's return type could be any data type, including
the void, which states that there is no need to return anything once the execution of that
function is complete.
Syntax:
function_return_type function_name(parameters) {
}
Example:
int sum(int num1, int num2) {
return (num1 + num2);
}
Here, the return type of the sum function is an integer, and the function is used to calculate
the sum of 2 numbers.
2. Array
An array is a set of elements that are kept in memory in a continuous manner and also have
the same type of data present within the array. The purpose of an array is to store a lot of data
in a single variable name and sequential order.
Syntax:
datatype array_name[size_of_array];
Example:
int arr[4]={0,1,2,3};
Here, we have defined an integer array of size 4, which can continuously store four integer
variables in memory.
Pointer
Pointers are symbolic representations of addresses. Pointers store the addresses of the
variables having the same datatype as that of the pointer. The size of the pointer is either 4
bytes or 8 bytes, no matter what the data type is. They enable programs to create and change
dynamic data structures, as well as to imitate call-by-reference. In C/C++, its generic
declaration looks like this:
Syntax:
data_type* variable_name;
Example:
int* point_int;
point_int holds the address of a variable of an integer datatype.
4. Reference
When we declare a variable as a reference, it becomes an alternate name for an existing
variable. By adding '&' to a variable's declaration, it can be declared as a reference.
Example:
int val = 1526;
int &ref = val;
Here ref becomes the reference to integer val, and now any changes in one would be
automatically reflected in the other as they both represent the same memory location.
Abstract or User-Defined Data types in C++
The Abstract datatype, also known as user-defined data type, is a type of data defined by the
user.
Examples of these are:
1. Class
A Class is a C++ building piece that leads to Object-Oriented programming. It's a user-
defined data type with its own set of data members and member functions that can be
accessed and used by establishing a class instance. A class defines the blueprint
for a data type.
Example:
#include <bits/stdc++.h>
using namespace std;
class scaler {
public:
string student_name;
void print_name() {
cout << "Student name is: " << student_name << endl;
}
};
int main() {
scaler student1, student2;
student1.student_name = "Shivam Singla";
student1.print_name();
student2.student_name = "Sachin Singla";
student2.print_name();
Output :
Student name is: Shivam Singla
Student name is: Sachin Singla
In the above example, the scaler is the name of the class. We can include the data members,
which are the class variables. Similarly, member functions are added to the class; for
example, print_name(), here is the member function, and student_name is the data member.
Here, the student1 and student2 are the class scaler objects.
2. Structure
A structure datatype is a user-defined data type that combines objects of potentially different
data types into a single type.
Example:
struct student {
char name[15];
char roll_no[10];
int marks;
};
Here, different data types, such as an array of characters and integer data types, are combined
to make a new data type according to the user's need.
3. Union
Union is similar to Structures as it is also used to combine the different types of data into a
single user-defined data type. All members of a union have access to the same memory. In
the below-shown example, we can combine the integer data type and the character data type
into a single data type called test. In this case, as both the data types, integer, and character
have different data sizes, we would take the larger data type as the size of the new user-
defined data type test. We can see how changes in num are reflected in var if we adjust num.
Example:
union test {
int num;
char var;
};
Here, num and var share the same memory. Therefore, if we change any variables, the
changes will automatically reflect in another variable.
4. Enumeration
In C++, an enumeration (or enum) is a data type that the user creates. It's primarily used to
give integral constant names, making the program easier to comprehend and maintain. In
enumeration, if we do not provide the integral values explicitly to the strings, then, in that
case, the strings automatically start assigning the integral values starting from value 0, the
same as the case of 0-based indexing.
Example:
enum result {pass = 100, fail = 0};
Here, we have given the integer value 100 to be pass and 0 as fail; therefore, if we write,
enum result res;
res = pass;
Then, the value of res would automatically be 100.
5. Typedef defined DataType
The term typedef in C++ allows you to declare explicit new data type names. Using
typedef does not create a new data class; instead, it gives an existing type a name. Because
just the typedef statements would need to be updated, a program's portability might be
improved by making minimal changes. By permitting descriptive terms for the standard data
types, typedef can aid in self-documenting code.
Example:
typedef long int ll;
Now we can use ll to define the long int data type in the code.
For Example,
ll val;
val = 123;
Datatype Modifiers in C++
DataType modifiers can be used to change some of the core data types further. In C++, there
are four modifiers. int, double, and char are the data types that can be modified using these
modifiers. They are as follows:
1. Signed
2. Unsigned
3. Short
4. Long
1. There are three data types in C++ which are primitive data types, abstract data types,
and derived data types.
2. Primitive data types include integer, floating-point, character, boolean, double
floating-point, valueless or void, and wide character.
3. User-defined or Abstract Data types include class, enumeration, union, structure, and
typedef defined data type.
4. Derived Data types include array, function, pointer, and reference.
5. Different data modifiers are short, long, signed, and unsigned, which we could apply
to data types like int, double, char, etc.
Data Types Memory Size Range
float 4 byte
double 8 byte