OOPs Using C++ Notes
OOPs Using C++ Notes
Oriented
Programming
(OOP)
Syllabus
Module 1
Introduction to C++: Object Oriented Technology, Advantages of OOP, Input-output in C++, Tokens, Keywords,
Identifiers, Data Types C++, Derives data types. The void data type, Type Modifiers, Typecasting, Constant,
Operator, Precedence of Operators, Strings.
Module 2
Control Structures and Functions: Decision making statements like if-else, Nested if-else, goto, break, continue,
switch case, Loop statement like for loop, nested for loop, while loop, do-while loop. Parts of Function, User-
defined Functions, Value- Returning Functions, void Functions, Value Parameters, Function overloading, Virtual
Functions.
Module 3
Classes and Data Abstraction: Structure in C++, Class, Build-in Operations on Classes, Assignment Operator and
Classes, Class Scope, Reference parameters and Class Objects (Variables), Member functions, Accessor and
Mutator Functions, Constructors, default Constructor, Destructors.
Module 4
Overloading, Templates and Inheritance: Operator Overloading, Function Overloading, Function Templates,
Class Templates. Single and Multiple Inheritance, virtual Base class, Abstract Class, Pointer and Inheritance,
Overloading Member Function.
Module 5
Pointers, Arrays and Exception Handling: Void Pointers, Pointer to Class, Pointer to Object, Void Pointer, Arrays.
The keywords try, throw and catch. Creating own Exception Classes, Exception Handling Techniques (Terminate
the Program, Fix the Error and Continue, Log the Error and Continue), Stack Unwinding.
Prerequisites
• C++ basic
• Input output
• Data types
• Structure
• Function
• declaration
• definition
• Calling
In This Course
• Notes pdf
• Theory + Practical
• Theory on Board
• Practical on laptop
Let’s start
• C++
• C++ was developed by Bjarne Stroustrup
• Extension to the C language.
• C++ gives programmers a high level of control over system resources
and memory.
Introduction Of OOP
Procedural Oriented Programming :-
• Procedural programming is a programming paradigm that focuses on
procedures or functions that perform specific tasks. It is a step-by-step
approach to programming, where the program is divided into smaller
procedures or functions, each with its own specific task
• Function is a prime focus and very little attention was given to data that
are be used by function
• For every long program it is very difficult to track that what data is used by
which function
• Data movement is open
• Function modifier data from one form to another form
C structure Vs C++ structure
C structure C ++ structure
Syntax of class :-
class class_name
{
data;
Fun( );
};
Some points about Class
• Extension of structure
• Logical entity / blueprint (space not occupy). (Object physical space occupy)
• No actual memory holding
• One class can have as many object as it needed
• Class acts as a data type for its object
• Class is a collection of objects of similar type
Example
Object :-
• An object is an instance of a class . It represent a specific entity or thing
that is created based on the blueprint provided by the class
Some points about object
• Object is a real world entity
• Object has a state and behaviour
• Object state is a set of property value of particular instant
• Object physical space occupy
cin >> extraction operator
cout << insertion operator
:: scope resolution
Example
C++ structure Vs C++ class
C++ structure C++ class
User define User defined
Collection of variable and function Collection of variable and function
Member public or private Member public or private
By default public By default private
• Note: This access through inheritance can alter the access modifier of
the elements of base class in derived class depending on the mode of
Inheritance.
Example
Friend function
• Friend function is a non member function of the class which have
access to the private member of that class
• It is not in the scope of the class to the class to which it has be declare
as friend so it cant be called using the object of that class
• It can be called like a normal function
• It can’t the access the member by name directly but it has to use an
object name and dot membership operator with each member name
• It can be declare either in public or private section of a class without
affecting its meaning
• Usually friend function takes object as an argument
Advantages of Friend Function
• The friend function allows the programmer to generate more efficient
codes.
• It allows the sharing of private class information by a non-member
function.
• It accesses the non-public members of a class easily.
• It is widely used in cases when two or more classes contain the
interrelated members relative to other parts of the program.
• It allows additional functionality that is not used by the class
commonly.
Constructor & destructor
Constructor
• Constructor is a special member function used to initialized the object at time of
its creation
• They should be declared in public section
• Constructor is called automatically when object are created
• Do not have return type
• They can not be inherited but a derived class constructor can call the base class
constructor
• They have default argument
• Constructor can not be virtual
• When constructor is declared for a class then initialization of the class object
becomes mandatory
• Constructor name is class name
There are three type of constructor
✓ constructor without any arguments or with the default value for every
argument is said to be the Default constructor.
✓ A constructor that has zero parameter list or in other sense, a constructor that
accepts no arguments is called a zero-argument constructor or default
constructor.
✓ copy constructor needed when the class has one or more data
members that are pointers or references. Also, when the class has
dynamically allocated memory.
Destructor
• A destructor is use to destroy objects that have been created by a constructor
• Destructor is also a member function whose name is same as class name but is
preceded by a tilde (~) symbol
• Destructor is called automatically when object goes out of scope that means
when function end, program end, or a block can containing local variable ends
• Only one destructor is possible in a class
• Destructor takes no argument and it also not have any return type
• If programmer do not supply destructor in a class then compiler created
default destructor
• When a class contain a pointer to memory allocated in class we should write a
destructor to release memory before the class instance ( object) is destroyed
by doing this we can avoid memory leak
Static variable & Static member function
• Static variable are use to maintain values common to the entire class
• Static variable are initialize to zero when 1st object of its class is created
• Only one copy of static variable created for entire class
• It is visible only within the class but its life time is the entire program
• Static member must be define outside the class
• Static member are also known as class variable
• Only one copy of static variable shared with all object
• Static member function can have access to only other static members
(variable & function)
• Can we called using class name ( class_name :: fun )
Encapsulation
• Encapsulation defined as the
wrapping up of data and information
in a single unit.
• Encapsulation is defined as binding
together the data and the functions
that manipulate them.
Two Important property of Encapsulation
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.
• Syntax
return_type class_name :: operator operator_symbol (argument_list)
{
//body
} keyword
• Operator overloading does not change the of an operator
• Semantics of an operator can be extended but can’t change its syntax, the grammatical
rule such as number of operands, precedence & associativity
• Operator overloading achieve through a special function called operator function
• Operator function must be either member function (non static) or friend function
• Member function will have no argument for unary operator but have single argument for
binary operator
• This is because object invoking member function passed implicitly to the member
function ( through this pointer )
• Friend function will have one argument for unary operator & two for binary operator
because friend are non member function show this point is not available for it
➢Friend function is used to overload binary operator where left hand
operand of built in data type and right hand operand is object
➢Some of operator that can have not overload
• sizeof()
• Membership operator .
• Pointer to member . *
• Scope resolution ::
• Conditional ? :
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Single inheritance
• In single inheritance, a class is
allowed to inherit from only
one class. i.e. one subclass is
inherited by one base class
only.
Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Multiple inheritance
Syntax
Derived_class_constructor ( arg_list1,arg_list2,……arg_listN, Arg_list_D) : base_1(arg_list1),base_2(arg_list2)…..
{
//body of derived class constructor
}
Pointer to derived class
• Pointer to object of a base class are type compatible with pointer to object of a
derived class so base class pointer can be used to point the to the derived class
object
• Base class pointer can point to a derived class object but while accessing the
public member of derive classes be have to careful because through base class
pointer we can access only those member which are inherited from base class
and not the member that are originally belong to derived class
When member of derive class has the some name as one of the member of base
class then any reference to that member by base class pointer will always access
base class member
So, in this case for accessing the member of derived class we have to typecast the
base type pointer into the derived type
((derived*)bptr)
• Base class pointer even when it is made to contain the address of a
derived class always executes the function in the base class because
the compiler simply ignore the contains of the pointer and choose the
member that matches the type pointer
Virtual function
• Virtual function is a member function that is declared within the base
class and can be redefined by the derived class.
• Virtual is a keyword
• When fun is made virtual, C++ determines which function to use at
runtime based on the type of object pointed to by the base pointer ,
rather than the type of the pointer thus by the making of base pointer
to point different object it is possible to executes different version of
the virtual function and hence achieving run time polymorphism
• Run time polymorphism is achieve only when a virtual function is
accessed through base pointer
Rule for virtual function
• Virtual function must be member of some class
• Virtual function can not be static
• Virtual function can be friend of another class
• Virtual function in base class must be defined even through it may not be used
• We can not have virtual constructor but we can have virtual destructor
• When a base pointer points to derived class incrementing or decrementing it
will not make it to point to the next object of derived class
• It is incrementing and decrementing only relative to its base class
Abstract class
• A class which is not used to create object is called abstract class
• Abstract class design only to act as a base class
• Class containing pure virtual function can’t be used to declare any
object of its own such type of class are called abstract base class
Interfaces
• Interfaces are nothing but a way to describe the behavior of a class
without committing to the implementation of the class.
• In C++ programming there is no built-in concept of interfaces.
• In order to create an interface, we need to create an abstract class
which is having only pure virtual methods.
• In C++, Interfaces are also called pure abstract classes.
Pure virtual function
• A pure virtual function is a virtual function that has no definition
within the class.
• A pure virtual function is a function declared in the base class that has
no definition related to the base class
• Virtual void show()=0;
• In this case the compiler requires each derived class to either defined
the function or redeclared it as a pure virtual function
• Class containing pure virtual function can’t be used to declare any
object of its own such type of class are called abstract base class
Exception Handling
• What is Exception :- An exception is an unexpected problem that arises
during the execution of a program , our program terminates suddenly
with some errors/issues. Exception occurs during the running of the
program (runtime).
✓catch
The catch statement represents a block of code that is executed when a particular exception is thrown
from the try block.
The code to handle the exception is written inside the catch block.
✓ throw
An exception thrown using the throw keyword.
When a program encounters a throw statement, then it immediately terminates the current function and
starts finding a matching catch block to handle the thrown exception.
Note: Multiple catch statements can be used to catch different type of exceptions thrown by try block.
The try and catch keywords come in pairs:
We use the try block to test some code and If the code throws an exception we will handle it in our
catch block.
Advantages
•Improved Error Handling: Exception handling in C++ provides a clean and efficient way
to handle runtime errors in a program. It separates error-handling code from normal code,
making the code cleaner and easier to maintain.
•Better Program Structure: Exception handling helps to improve the structure of a
program by allowing developers to separate error-handling code from the main logic of the
program. This makes the code more readable and easier to understand.
•Reduced Complexity: Exception handling can reduce the complexity of a program by
providing a unified mechanism for handling different types of errors. This allows developers to
write simpler and more maintainable code.
•Enhanced Performance: Exception handling can enhance the performance of a program
by providing a fast and efficient way to handle errors. It is faster than using traditional error-
handling methods, such as error codes, which can slow down a program’s execution.
•Increased Flexibility: Exception handling in C++ provides increased flexibility by allowing
developers to define their own exceptions and custom error-handling routines. This allows
developers to tailor the error-handling mechanism to the specific needs of their program.
Thank You!!