Mona Abdelmonem
Mona Abdelmonem
Mona Abdelmonem
com
What is OOPs?
It stands for Object-Oriented Programming.
It is based on objects
It follows Bottom-up programming approach.
It is based on real world.
It provides data hiding so it is very secure.
It provides reusability feature.
What is a class?
A class is a collection of objects. Classes don’t consume any space in the memory.
It is a user defined data type that act as a template for creating objects
of the identical type.
A large number of objects can be created using the same class. Therefore, Class is
considered as the blueprint for the object.
What is an object?
An object is a real world entity which have properties and functionalities.
Object is also called an instance of class. Objects take some space in memory.
For eg .
Fruit is class and its object s are mango ,apple , banana
Furniture is class and its objects are table , chair , desk
Class Object
1. It is a collection of objects. It is an instance of a class.
4. Classes are declared just once Objects can be declared as and when required
Class Structure
1.Class is a collection of objects. Structure is a collection of variables of
different data types under a single unit
2. Class is used to combine data and methods Structure is used to grouping data.
together.
3. Class's objects are created on the heap Structure's objects are created on the
memory. stack memory.
Inheritance Abstraction
OOP
Encapsulation Polymorphism
The main advantage of encapsulation is that data is hidden and protected from randomly access
by outside non-member methods of a class.
In encapsulation, data(variables) are declared as private and methods are declared as public.
There are three types of most common access specifiers, which are following.
• Private
• Public
• Protected
Public Modifiers :
means that class, variable or method is accessible throughout from within or outside
the class, within or outside the package, etc.
Private Modifiers :
means that class, variable or method is not accessible from within or outside the
class, within or
outside the package, etc.
Protected Modifiers :
means that class, variable or method is accessible from classes in the same package,
sub-classes in
the same package, subclasses in other packages but not accessible from classes in
other packages.
Allows to hide unnecessary data from the user. This reduces program complexity
efforts.
it displays only the necessary information to the user and hides all the internal
background details.
Eg.
-All are performing operations on the ATM machine like cash withdrawal etc.
but we can't know internal details about ATM
Abstract methods are those methods which have only declaration not the
implementation.
Inheritance is the procedure in which one class inherits the attributes and methods
of another class.
In other words It is a mechanism of acquiring properties or behaviors of existing
class to a new class
The Base Class, also known as the Parent Class is a class, from which other classes are
derived.
The Derived Class, also known as Child Class, is a class that is created from an existing
class
When more than class inherit properties and behaviour of only one class
In Hierarchical Inheritance there are only one parent and many child class
Multi-Level Inheritance
In this type of inheritance, a derived class is created from another derived class
When a class inherits the properties and the behaviour of more than one class
Java, C#, most of high level language don’t support Multiple Inheritance
Hybrid Inheritance
Ambiguity Around The Diamond Problem Multiple inheritance does complicate the
design and creates problem during casting, constructor chaining etc.
Polymorphism
Many Forms
polymorphism is mainly divided into two types:
• Compile time Polymorphism (CTP)
Function overloading:
When there are multiple functions with same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by change in number of
arguments or/and change in type of arguments
multiple methods of same names performs different tasks within the same class.
2.Runtime polymorphism:
Runtime polymorphism refers to the process when a call to an overridden process is
resolved at the run time.
This type of polymorphism is achieved by Function Overriding.
Function Overriding:
on the other hand, occurs when a derived class has a definition for one of the member
functions of the base class.
methods having same name which can have different functionalities.
That base function is said to be overridden.
Static methods can not be overridden. They are stored in heap space of the memory.
Virtual function defined in the base class and overridden in the inherited class.
The Virtual function cannot be private, as the private functions cannot be overridden. It is
used to achieve runtime polymorphism.
A pure virtual function have not definitions but we must override that function in the
derived class, otherwise the derived class will also become abstract class.
What is Constructor?
Constructor is a special type of member function which is used to initialize an object.
It is similar as functions but it's name should be same as its class name and must have no
explicit return type.
It is called when an object of the class is created. At the time of calling constructor,
memory for the object is allocated in the memory.
We use constructor to assign values to the class variables at the time of object creation.
• Default constructor
• Parameterized constructor
• Copy constructor
• Static constructor
• Private constructor
It copy variables from another object of the same class to create a new object.
What is destructor?
Destructor is a type of member function which is used to destroy an object.
It is called automatically when the object goes out of scope or is explicitly destroyed by a
call to delete.
A destructor has the same name as the class, preceded by a tilde (~).
Shallow copy
In the case of Shallow copy, it will create the new object from the existing object and then copying the
value type fields of the current object to the new object.
But in the case of reference type, it will only copy the reference, not the referred object itself.
Therefore the original and clone refer to the same object in the case of reference type. In order to
understand this better, please have a look at the following diagram.
Deep Copy
In the case of deep copy, it will create the new object from the existing object and then copying the
fields of the current object to the newly created object. If the field is a value type, then a bit-by-bit copy
of the field will be performed. If the field is a reference type, then a new copy of the referred object is
created.
In C++ we can pass arguments into a function in different ways. These different ways are
Call by Value
Call by Reference
Call by Address
Sometimes the call by address is referred to as call by reference, but they are different in C++. In call by
address, we use pointer variables to send the exact memory address, but in call by reference we pass the
reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer
to get that effect. In this section we will see what are the advantages of call by reference over call by value,
and where to use them
In call by value, the actual value that is passed as argument is not changed after performing
some operation on it. When call by value is used, it creates a copy of that variable into the
stack section in memory. When the value is changed, it changes the value of that copy, the
actual value remains the same.
Example Code
#include<iostream>
using namespace std;
void my_function(int x) {
x = 50;
cout << "Value of x from my_function: " << x << endl;
}
main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 10
In call by reference the actual value that is passed as argument is changed after performing
some operation on it. When call by reference is used, it creates a copy of the reference of
that variable into the stack section in memory. Is uses a reference to get the value. So
when the value is changed using the reference it changes the value of the actual variable.
#include<iostream>
using namespace std;
main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 50