Unit 1 Oops Concepts Part 1
Unit 1 Oops Concepts Part 1
PROGRAMMING CONCEPTS
PART-1
CLASSES AND OBJECTS in C++
CLASSES:
• A class is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating
an instance of that class.
• A C++ class is like a blueprint for an object.
Example:
• Consider the Class of Cars.
• There may be many cars with different names and brands but all of
them will share some common properties like all of them will have 4
wheels, Speed Limit, Mileage range, etc.
• So here, the Car is the class, and wheels, speed limits, and mileage
are their properties.
• A Class is a user-defined data type that has data
members and member functions.
• Data members are the data variables and member
functions are the functions used to manipulate these
variables together, these data members and member
functions define the properties and behavior of the
objects in a Class.
• In this example of class Car, the data member will be
speed limit, mileage, etc, and member functions can
be applying brakes, increasing speed, etc.
NOTE:
• But we cannot use the class as it is. We first have
to create an object of the class to use its
features. An Object is an instance of a Class.
• Here, the access specifier defines the level of access to the class’s
data members.
Example:
OBJECTS
• When a class is defined, only the specification for the object is
defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.
Syntax:
• We can create an object of the given class in the same way we declare
the variables of any other inbuilt data type.
• Example:
• For example, if the name of the object is obj and you want to access
the member function with the name printName() then you will have
to write:
Example of Class and Object in C++
OUTPUT:
METHODS AND MESSAGES
METHODS:
• Class is a blueprint of an object, which has data members and
member functions also known as methods.
• A method is a procedure or function in the oops concept.
• A method is a function that belongs to a class.
• There are two ways to define a procedure or function that belongs to
a class:
• Inside Class Definition
• Outside Class Definition
1. Inside Class Definition
• The member function is defined inside the class definition it can be
defined directly.
• Similar to accessing a data member in the class we can also access the
public member functions through the class object using the dot
operator (.).
• Syntax:
2. Outside Class Definition
• The member function is defined outside the class definition it can be
defined using the scope resolution operator.
• Similar to accessing a data member in the class we can also access the
public member functions through the class object using the dot
operator (.).
Syntax:
MESSAGES
• Message passing in C++ is the communication between two or
more objects using a logical entity called a message.
• A message is a form of request that is made to an object to
perform a function specific to the object to which the request
was made.
• A message is a form of communication between objects that
make an object perform a task for the system.
• The message is an abstract entity and will not hold the
information of the function that is going to be performed and
only plays the role of invoking the function.
• A message is a part of OOPS concepts in C++.
How it Works?
Message passing in C++ works by the implementation of the
following steps:
• Creating a class and object for the class.
• Communication between objects can be made with the help
of a message.
• A message is passed to an object which will invoke the
function in another object.
• Thus, communication between two objects is established
using a message.
Difference between Message Passing and Method Call
Abstraction in C++
• Data abstraction is one of the most essential and important features
of object-oriented programming in C++.
• Abstraction means displaying only essential information and ignoring
the details.
• Data abstraction refers to providing only essential information about
the data to the outside world, ignoring unnecessary details or
implementation.
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.
3. 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.
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.
Advantages of Data Abstraction
• Avoids code duplication and increases reusability.
• Can change the internal implementation of the class
independently without affecting the user.
• Helps to increase the security of an application or
program as only important details are provided to the
user.
• It reduces the complexity as well as the redundancy of
the code, therefore increasing the readability.
• New features or changes can be added to the system
with minimal impact on existing code.
Encapsulation in C++
• Encapsulation in C++ is defined as the wrapping up of data
and information in a single unit.
• In Object Oriented Programming, Encapsulation is defined as
binding together the data and the functions that manipulate
them.
Two Property of Encapsulation:
• Data Protection: Encapsulation protects the internal
state of an object by keeping its data members
private.
• Public Mode
• Protected Mode
• Private Mode
Types Of Inheritance in C++
The inheritance can be classified on the basis of the relationship
between the derived class and the base class. In C++, we have 5 types
of inheritances:
• Single inheritance
• Multiple inheritance
• Multilevel inheritance
• Hierarchical inheritance
• Hybrid inheritance
1. Single Inheritance
• In single inheritance, a class is allowed to inherit from only one class.
i.e. one base class is inherited by one derived class only.
• Syntax:
• Example:
Implementation:
2. Multiple Inheritance
• Multiple Inheritance is a feature of C++ where a class can inherit from
more than one class. i.e one subclass is inherited from more than one
base class.
• Syntax:
Example:
Implementation:
Output:
3. Multilevel Inheritance
• In Multilevel 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.
• Syntax:
Example:
Implementation
• Output:
4. Hierarchical Inheritance
• In Hierarchical 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.
• Syntax:
Example:
Implementation
• Output:
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 in C++
• There is no particular syntax of hybrid inheritance. We can just
combine two of the above inheritance types.
Implementation:
Output:
Constructors and Destructors in Inheritance
• Constructors and Destructors are generally defined by the
programmer and if not, the compiler automatically creates them so
they are present in every class in C++.
• In C++ inheritance, the constructors and destructors are not inherited
by the derived class, but we can call the constructor of the base class
in derived class.
• The constructors will be called by the complier in the order in
which they are inherited. It means that base class constructors will
be called first, then derived class constructors will be called.
• The destructors will be called in reverse order in which the
compiler is declared.
• We can also call the constructors and destructors manually in the
derived class.
Example:
• Output:
Polymorphism in C++
• The word “polymorphism” means having many forms.
• 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.
Types of Polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by Function Overloading or Operator
Overloading.
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.
Example:
Output:
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.
Example:
2. Runtime Polymorphism
• This Runtime Polymorphism is achieved by Function Overriding.
• Late binding and dynamic polymorphism are other names for runtime
polymorphism.
• The function call is resolved at runtime in runtime polymorphism.
• In contrast, with compile time polymorphism, the compiler
determines which function call to bind to the object after deducing it
at runtime.
A. Function Overriding
• Function Overriding occurs when a derived class has a definition for
one of the member functions of the base class. That base function is
said to be overridden.
Runtime Polymorphism with Data Members
• Runtime Polymorphism cannot be achieved by data members in C++.
• Let’s see an example where we are accessing the field by reference
variable of parent class which refers to the instance of the derived
class.
B. Virtual Function
• 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.