Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

4 Pillars of OOP

Encapsulation Grouping of data member and member


function in a single unit means class.
Private variables of the class will be hidden
from other classes and can be accessed
using member functions of that class. It
means data hiding. Example: private
access specifier.
Abstraction Abstraction keeps code clean, and removes
repetition so you don’t repeat yourself.
Hiding extra details and showing essential
information to the outside world. Example:
Calculator
Polymorphism If object have many forms. For example, a
Static Polymorphism person is father, son, brother, etc.
 Method overloading It allows us to perform single action in
 Operator overloading different ways. Polymorphism is of two
types: Static (compile time) and Dynamic
Dynamic Polymorphism (runtime).
 Method overriding The process of linking a function with an
object during compile time is called static
polymorphism.
Dynamic polymorphism is a process in
which a call to an overridden method is
resolved at runtime, that’s why it is called
runtime polymorphism.
A class have two function with same name
but on the base of different parameters
they take or different return type. Now,
compiler will decide from return type or
number of parameters are passing, which
function is called.
Inheritance Parent Child Relationship or Base class
and drive class relationship.

Relations in OOP
Inheritance “Is- a” relationship, Parent child
relationship.
Association “Using” relationship, exe. A person eat
bread. One object uses other object.
Composition “has a” relationship. A child object cannot
exist without parent object. Strong
Relationship
Aggregation “Part of or a whole”. A part of object can
exist outside the whole is aggregation. For
example Chair and Room. Weak
Relationship.

Other Concepts
Class Class is a blueprint which is used to create
objects.
Object Instance of a class is object.
OOP OOP is a programming paradigm that
relies on classes and objects.
Interface Interface are methods to interact outside
world.
Overriding If derived class defines a method which is
already defined in its base class but
changes implementation.
Overloading More than one function with same name
but with different parameters or return
type. Based on parameters we pass during
function call compiler will decide which
function is called.
Abstract Class Abstract class is the super class or base
class from which we instantiate other
derived or child classes.
Concrete Class The entities we see in real world are
concrete objects and classes made against
these objects are called concrete classes.
Example: vehicle is a base class, and car,
truck and bus are the concrete classes of
vehicle class.
Inline Function Inline function runs faster than normal
function. Because compiler replaces
function call with function code itself and
then compiles the entire code. In other
words, if a function is inline then compiler
places copy of the code of that function at
each point where the function is called at
compile time.
We will make a function inline when it is
too small and often called.
Constructor Main purpose of constructor is to construct
Default Constructor an object of a class or it is used to initialize
Copy constructor all the data members of a class.
Shallow copy Constructor can be called implicitly and
Deep copy explicitly.
Default constructor is a constructor which
have no parameters or if it has parameters
then it has default values in it.
A copy constructor copies the data of one
object into another object. Purpose of copy
constructor is to initialize a new instance
from the values of an existing instance.
Shallow copy copies the references
(pointers etc.) of an object. It is a bit-wise
copy. Bit-wise means you copy the bits of
the source class to the target class using
memcpy() or something similar. A new
object is created that has an exact copy of
values in the original object. If any of the
fields are references to other objects, only
the reference addresses are copied, i.e. only
the memory address is copied.
Deep copy copies the entire object (struc).
Destructor It is a member function that is invoked
automatically when the object goes out of
scope or is explicitly destroyed by a call to
delete. Example destructor for a class
String is ~String().
Accessor Functions To access private data member, accessor
Mutator Functions function is used. These are like get and set
functions in c#.
Accessors are member functions that allow
access to data members.
Member functions that allow for
modification of the data members are
called mutators.
This pointer It is a pointer which is accessible only
within non-static member function of a
class. It points to the object for which the
member function is called.
Constant member function The object called by these functions cannot
be modified. “const” keyword is used
before function name.
Local, Global and Static Variables Local variable is declared, initialized and
used inside of a function. Is is similar to
normal variable. Its lifetime begins when
function is called and ends when function is
completed. We can have local variables of
same name in different functions, because
local variables are only recognized by the
function in which they are declared.

Global variables are defined outside of all


functions, usually on top of the program.
They hold values through out the life time
of program. Global variable can be
accessed by any function. “global”
keyword used to access a global variable
from within function.

Normally, when a function is completed or


executed all of its variables are deleted. But
sometimes, we want a local variable not to
be deleted. We need it for further job.
Static variable reduce the amount of
memory used by program. These variables
are shared among all instances of a class.
Static variable is like global variable and is
available to all methods. Retains its value
even after function terminates.

Non-static variable are specific to that


instance of a class.
Pointer to objects A pointer holds address of an object stored
in memory. The pointer then simply points
to the object. The type of the object must
correspond with the type of the pointer.
Friend Functions Friend functions are part of class interface
and it is declared by the class. Friend
function can access private and protected
data of class. “friend” keyword is used to
before return type and function name to
make it friend function.
Operator Overloading “=” and “&” are already overloaded by
default in c++. To change the working of
operator it is called operator overloading.
We can overload “+” operator in a class
like string so that we can concatenate two
strings by using “+” operator.
Stream Insertion operator It is used for output.
Overloading Stream Insertion operator Cout is an object of ostream class. These
operators must be overloaded as global
function.
Stream extraction operator It is used for input.
Overloading Stream extraction operator Cin is an object of istream class. These
operators must be overloaded as global
function.
Subscript operator Subscript operator “[]” is normally used to
Overloading subscript operator access array elements. This operator can be
overloaded to enhance the existing
functionality of c++ arrays.
Overloading function() operator

Type Conversion Type conversion refers to conversion from


one type to another. Main idea of type
conversion is to make variable of one type
compatible with variable of another type to
perform an operation.
Overloading vs Overriding Overloading means two or more methods
in one class have the same name but
different parameters. Overriding means
two or more methods with same name and
same parameters.
Static vs Non Static member function A static member function can be called,
even when a class is not instantiated. A
static member function cannot have access
to the “this” pointer of the class. A non-
static member function can be declared as
virtual but care must be taken not to
declare a static member function as virtual.

You might also like