Unit 2 (Oops)
Unit 2 (Oops)
Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. Now,
The finance section handles all the financial transactions and keeps records of all the data
related to finance.
Similarly, the sales section handles all the sales-related activities and keeps records of all
the sales.
1. Data Protection: Encapsulation protects the internal state of an object by keeping its data
members private. Access to and modification of these data members is restricted to the
class’s public methods, ensuring controlled and secure data manipulation.
2. Information Hiding: Encapsulation hides the internal implementation details of a class from
external code. Only the public interface of the class is accessible, providing abstraction and
simplifying the usage o f the class while allowing the internal implementation to be modified
without impacting external code.
Output- 5
Object identity
Object identity is a property of data that is created in the context of an object data model,
where an object is assigned a unique internal object identifier, or oid. The object identifier is
used to define associations between objects and to support retrieval and comparison of
object-oriented data based on the internal identifier rather than the attribute values of an
object.
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.
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.
C++ program to show function overloading or compile-time polymorphism:
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.
2. Runtime Polymorphism
This type of 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.
where,
class: keyword to create a new class
derived_class_name: name of the new class, which will inherit the base
class
access-specifier: Specifies the access mode which can be either of
private, public or protected. If neither is specified, private is taken as
default.
base-class-name: name of the base class.
Example : Program to Demonstrate the Simple
Inheritance of a Class
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:
1. Single inheritance
2. Multilevel inheritance
3. Multiple 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
base class is inherited by one derived class only.
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
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 in C++
There is no particular syntax of hybrid inheritance. We can just combine two of
the above inheritance types.
Example:
Below image shows one of the combinations of hierarchical and multiple
inheritances:
Class in C++
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.
For 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 behaviour of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage, etc,
and member functions can be applying brakes, increasing speed, etc.
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.
syntax:
Object in C++
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 to Create an Object
We can create an object of the given class in the same way we declare the variables of any
other inbuilt data type.
Types of Exceptions
Checked Exception
Unchecked Exception
2. User-Defined Exceptions
Let us discuss the above-defined listed exception that is as follows:
1. Built-in Exceptions
Built-in exceptions are the exceptions that are available in Java libraries. These
exceptions are suitable to explain certain error situations.
Checked Exceptions: Checked exceptions are called compile-time
exceptions because these exceptions are checked at compile-time by the
compiler.
Types of templates-2
a) Function Templates
b) Class Templates
a) Function templates:We write a generic function that can be used for
different data types.
Ex-sort(),max(),min(),printArray().
program
#include<iostream>
using namespace std;
template <class T>
T add(T a,T b)
{
cout<<"Addition="<<a+b;
}
main()
{
add(4.2,7.3);
}
Output
11.5
11.5
Class templates: A class template starts with the keyword template followed by
template parameter(s) inside <> which is followed by the class declaration.
Syntax
template <class T>
class className {
private:
T var;
... .. ...
public:
T functionName(T arg);
... .. ...
};
Program
#include<iostream>
using namespace std;
template <class T>
class CT
{
private:
T num1,num2;
public:
CT(T n1,T n2)
{
num1=n1;
num2=n2;
}
void check()
{
if(num1>num2)
{
cout<<num1<<" is the largest number\n";
}else{
cout<<num2<<" is the largest number";
}
}
};
int main()
{
CT c(5,8);
c.check();
return 0;
}
Memory management
Whenever a program is run some memory is allocated to that program.This
memory logically has 4 sections-
1) Stack
2) Heap
3) Data
4) Instructions
Program
main()
{
int *p;
p=(int*)calloc(5,4);
}
Access values-
*(P+0)=34
*(P+1)=55
2. realloc():It means reallocation.When we want to change the size
of a memory block which is created with the help of malloc() &
calloc() function,then we use reallock().
syntax
calloc(pointer returned by malloc()/calloc(),size of memory block);
Example
Void realloc(void *block,int size)
o In c++ programming, these variables are created by using some
keywords/operators
1. new
2. delete
1. new-It denotes a request for memory allocation on the free
storage.If sufficient memory is available,a new operator initializes
the memory and retuned the address of newly allocated &
initialized memory to the pointer variable.
Syntax
Pointer_variable=new data-type;
Example-
Float *p=new float;
Int *ptr=new int[5];
2. delete-It is an operator which is used to destroy array & non-
array(pointer) objects which are dynamically created by new
operator.
delete can be used by either the delete operator or delete[]
operator.
new operator is used for dynamic memory allocation which stores
variables on heap memory.It means delete operator deallocates
memory from the heap.
The pointer to the object is not destroyed,the value or memory
block pointed by the pointer is destroyed.
It has void return type which means it doesn’t return any value.
Syntax
delete pointer_name;
Or
delete[] array_name;