1. Friend Functions
• A friend function of a class is defined outside that class' scope but it has the right to
access all private and protected members of the class.
• Even though the prototypes for friend functions appear in the class definition,
friends are not member functions.
• A friend can be a function, function template, or member function, or a class or
class template, in which case the entire class and all of its members are friends.
Example
class Box
{
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
2. Inline Functions
• C++ inline function is powerful concept that is commonly used with classes.
• If a function is inline, the compiler places a copy of the code of that function at
each point where the function is called at compile time.
• To inline a function, place the keyword inline before the function name and define
the function before any calls are made to the function. The compiler can ignore the
inline qualifier in case defined function is more than a line.
• A function definition in a class definition is an inline function definition, even
without the use of the inline specifier.
• For Example
inline int Max(int x, int y)
{ return (x > y)? x : y;}
3. This Pointer
• Every object in C++ has access to its own address through an important pointer
called this pointer.
• The this pointer is an implicit parameter to all member functions.
• Therefore, inside a member function, this may be used to refer to the invoking
object.
• Friend functions do not have a this pointer, because friends are not members of a
class. Only member functions have a this pointer.
• The ‘this’ pointer is passed as a hidden argument to all non-static member function
calls and is available as a local variable within the body of all nonstatic functions.
• The ‘this’ pointer is a constant pointer that holds the memory address of the current
object.
• The ‘this’ pointer is not available in static member functions as static member
functions can be called without any object (with class name).
• ‘this’ pointer is used when local variable’s name is same as member’s name
5. Static Members
• We can define class members static using static keyword.
• When we declare a member of a class as static it means no matter how many
objects of the class are created, there is only one copy of the static member.
• A static member is shared by all objects of the class. All static data is initialized to
zero when the first object is created, if no other initialization is present
• By declaring a function member as static, you make it independent of any
particular object of the class.
• A static member function can be called even if no objects of the class exist and
the static functions are accessed using only the class name and the scope
resolution operator ::.
• static member function can only access static data member, other static
member functions and any other functions from outside the class.
6. Inheritance
• The capability of a class to derive properties and characteristics from another class
is called Inheritance.
• Inheritance is one of the most important feature of Object Oriented Programming.
• Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
• Super Class: The class whose properties are inherited by sub class is called Base
Class or Super class.
• This existing class is called the base class, and the new class is referred to as the
derived class.
• The base class is also called parent class and derived class is also called child
class.
Inheritance – “IS A” or
“IS A KIND OF” Relationship
7. Inheritance – Advantages
► Reuse
► Less redundancy
► Increased maintainability
Main purpose of inheritance is reuseWe can easily add new classes by
inheriting from existing classes
– Select an existing class closer to the desired functionality
– Create a new class and inherit it from the selected class
– Add to and/or modify the inherited functionality
• Syntax:
class subclass_name : access_mode base_class_name
{ //body of subclass };
8. Modes of Inheritance
Public mode:
• If we derive a sub class from a public base class.
• Then the public member of the base class will become public in the derived class
and protected members of the base class will become protected in derived class.
Protected mode:
• If we derive a sub class from a Protected base class.
• Then both public member and protected members of the base class will become
protected in derived class.
Private mode:
• If we derive a sub class from a Private base class.
• Then both public member and protected members of the base class will become
Private in derived class.
9. Inheritance Types
Single Inheirtance:
• In single inheritance, a class is allowed to inherit from only one class. i.e. one sub
class is inherited by one base class only.
Multiple Inheritance:
• Multiple Inheritance is a feature of C++ where a class can inherit from more than
one classes. i.e one sub class is inherited from more than one base classes.
Multilevel Inheritance:
• In multilevel inheritance, a derived class is created from another derived class.
Hierarchical inheritance
• In Hierarchical inheritance, more than one sub class is inherited from a single base
class.
Hybrid Inheritance
• Hybrid Inheritance is implemented by combining more than one type of
inheritance.
• For example: Combining Hierarchical inheritance and Multiple Inheritance.
10. Polymorphism
• The word polymorphism means having many forms.
• In OOP, Polymorphism means that a call to a member function will cause a
different function to be executed depending on the type of object that invokes the
function.
• Polymorphism is a feature of OOP that allows the object to behave differently in
different conditions.
In OOP we have two types of polymorphism:
• Compile time Polymorphism – This is also known as static (or early) binding.
• Runtime Polymorphism – This is also known as dynamic (or late) binding.
Compile time Polymorphism
• Function overloading and Operator overloading are perfect example of Compile
time polymorphism.
Compile time Polymorphism Example
• In this example, we have two functions with same name but different number of
arguments.
11. Polymorphism
Runtime Polymorphism
Function overriding is an example of Runtime polymorphism.
Function Overriding:
• When child class declares a method, which is already present in the parent class
then this is called function overriding, here child class overrides the parent class.
• In case of function overriding we have two definitions of the same function, one is
parent class and one in child class.
• The call to the function is determined at runtime to decide which definition of the
function is to be called, that's the reason it is called runtime polymorphism.
Function overloading
• Function overloading is a C++ programming feature that allows us to have more
than one function having same name but different parameter list.
• When I say parameter list, it means the data type and sequence of the parameters.
– different type
– number sequence of parameters
12. Polymorphism
Advantages of Function overloading
• The main advantage of function overloading is to the improve the code
readability and allows code reusability.
• In the example 1, we have seen how we were able to have more than one function
for the same task(addition) with different parameters.
• This allowed us to add two integer numbers as well as three integer numbers.
Function Overriding in C++
• Function overriding is a feature that allows us to have a same function in child class
which is already present in the parent class.
• A child class inherits the data members and member functions of parent class, but
when you want to override functionality in the child class then you can use function
overriding.
• It is like creating a new version of an old function, in the child class.
13. Polymorphism
Function Overriding Example
• To override a function you must have the same signature in child class.
• By signature I mean the data type and sequence of parameters.
• Here we don’t have any parameter in the parent function so we didn’t use any
parameter in the child function.
Difference between Function Overloading and Function overriding in C++
• Function Overloading happens in the same class when we declare same functions
with different arguments in the same class. Function Overriding is happens in the
child class when child class overrides parent class function.
• In function overloading function signature should be different for all the overloaded
functions. In function overriding the signature of both the functions (overriding
function and overridden function) should be same.
• Overloading happens at the compile time thats why it is also known as compile
time polymorphism while overriding happens at run time which is why it is known
as run time polymorphism.
14. Polymorphism
Virtual functions in C++: Runtime Polymorphism
• When we declare a function as virtual in a class, all the sub classes that override
this function have their function implementation as virtual by default (whether they
mark them virtual or not).
Why we declare a function virtual?
• To let compiler know that the call to this function needs to be resolved at runtime
(also known as late binding and dynamic linking) so that the object type is
determined and the correct version of the function is called.
15. Data Encapsulation
All C++ programs are composed of the following two fundamental elements:
– Program statements (code): This is the part of a program that performs
actions and they are called functions.
– Program data: The data is the information of the program which affected by
the program functions.
• Encapsulation is an Object Oriented Programming concept that binds together the
data and functions that manipulate the data, and that keeps both safe from outside
interference and misuse.
• Data encapsulation led to the important OOP concept of data hiding.
• Data encapsulation is a mechanism of bundling the data, and the functions that use
them.
• Data abstraction is a mechanism of exposing only the interfaces and hiding the
implementation details from the user.
16. Data Encapsulation
• C++ supports the properties of encapsulation and data hiding through the creation
of user-defined types, called classes.
• We already have studied that a class can contain private, protected and public
members.
• By default, all items defined in a class are private.
• Making one class a friend of another exposes the implementation details and
reduces encapsulation.
• The ideal is to keep as many of the details of each class hidden from all other
classes as possible.
17. Data Abstraction
• Data abstraction refers to, providing only essential information to the outside world
and hiding their background details,
• i.e., to represent the needed information in program without presenting the details.
• Data abstraction is a programming (and design) technique that relies on the
separation of interface and implementation.
• Let's take one real life example of a TV, which you can turn on and off, change the
channel, adjust the volume, and add external components such as speakers, VCRs,
and DVD players.
• BUT you do not know its internal details, that is, you do not know how it receives
signals over the air or through a cable, how it translates them, and finally displays
them on the screen.
• Thus, we can say a television clearly separates its internal implementation from its
external interface and you can play with its interfaces like the power button,
channel changer, and volume control without having zero knowledge of its
internals.