C++ Oop To Advanced
C++ Oop To Advanced
Object Oriented
Programming
OOP Concept + OOP Programming
By Zarif Bahaduri
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Chapter 1
Introduction to OOP
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Programming Languages
Machine Language
• Comprised of 1s and 0s
• The native language of a computer
• Difficult to program – one misplaced 1 or 0 will
fail the program to run.
• Example of code:
1110100010101 111010101110
10111010110100 10100011110111
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Assembly Languages 1
• Example:
ADD 1001010, 1011010
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
High-Level Languages
• High-level languages represent a giant leap towards
easier programming.
• The syntax of HL languages is similar to English.
• Historically, we divide HL languages into two groups:
• Procedural languages
• Object-Oriented languages (OOP)
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Procedural Languages
• Early high-level languages are typically called
procedural languages.
• Procedural languages are characterized by
sequential sets of linear commands. The focus of
such languages is on structure.
• Examples include C, COBOL, Fortran, LISP, Perl,
HTML, VBScript
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Object-Oriented Languages
• Most object-oriented languages are high-level
languages.
• The focus of OOP languages is not on structure,
but on modeling data.
• Programmers code using “blueprints” of data
models called classes.
• Examples of OOP languages include C++, Visual
Basic.NET and Java.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
What is OOP ?
• A way to look at a problem to be solved using a software based
solution.
• A problem domain is characterized as a set of objects
• Object have specific attributes and behavior
• Objects are manipulated with a collection of function (method,
operation, services)
• Objects communicates with each other by passing massages
• Objects are divided into classes and subclasses
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Advantage of OOP
• There are two advantage of Object Oriented Programming.
• Advantage at Management Level
• Software is easy to maintain because the structure is inherently decoupled.
• Ease to change
• Less frustration (disappointed) for costumer and software engineers
• Advantage at Technical Level
• Objects are reusable and leads to faster software development and
high quality programs
• Easier to adapt and scale
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Chapter 2
Object and Class
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Classes
• Class: A class is a definition of objects. In other words,
a class is a blueprint for an object, template, or prototype
that defines and describes the static
attributes and dynamic behaviors common to all objects of
the same kind.
• instance: An instance is a realization of a particular item
of a class. All the instances of a class have similar
properties, as described in the class definition. For
example, you can define a class called "Student" and
create three instances of the class "Student" for “ahmad",
“ali" and “walid"
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Class Examples
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Class Definition
• In C++, we use the keyword class to define a class. There
are two sections in the class declaration: private and
public, which will be explained later. For example.
class Circle { // classname
private:
double radius;
Note: public, private and protected // Data members
(variables)
are access modifiers, we will discuss
string color;
it later. public:
double getRadius(); // Member
functions
double getArea();
}
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
For example
// Declare and construct instances c1 and c2 of the class Circle
Circle c1(1.2, "blue");
Circle c2(3.4, "green");
// Invoke member function via dot operator
cout << c1.getArea() << endl;
cout << c2.getArea() << endl;
// Reference data members via dot operator
c1.radius = 5.5;
c2.radius = 6.6;
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
2
Practice on classes
Lab Workshop
Practical
Practice
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Chapter 3
Constructors &
Destructor
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Constructors 3
Constructors
A constructor function is different from an ordinary function
in the following aspects:
• The name of the constructor is the same as the classname.
• Constructor has no return type.
• Constructors takes Arguments.
• Constructor are not inherited.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Constructors
• There are two Types of Constructors
• Default Constructor
• Parameterized Constructors
Both Constructors will describe through examples
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Default Constructor
class MyData{
private:
MyData(){
string ename;
string n="ali";
int salary;
int s=500;
public:
salary=s;
//Other methods
ename=n;
void getdata(int s, string n)
{
cout<<s<<endl<<n;
salary=s;
}
ename=n;
cout<<s<<endl<<n;
}};
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Parameterize Constructor
class MyData{
private:
string ename;
int salary;
MyData(int x=90, int y=0){
public:
salary=x;
//Other methods com=y;
Int show(){ }
Int total=salary+com;
Return total;
}
};
Watch videos on YouTube.
Constructor Overloading
Channel Name: Zarif Bahaduri
Copy
constructor
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
Destructor
• Destructors are usually used to de-allocate memory and do other cleanup
for a class object and its class members when the object is destroyed. A
destructor is called for a class object when that object passes out of scope or
is explicitly deleted.
• Destructors are parameter less functions.
• Name of the Destructor should be exactly same as the class. With prefix of
‘~’.
• Destructor does not have any return type.
• The Destructor of class is automatically called when object goes out of scope.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Access Control 3
Modifiers/Access pacifiers
Data hiding is one of the important features of Object
Oriented Programming which allows preventing the
functions of a program to access directly the internal
representation of a class type. The access restriction to the
class members is specified by the labeled public,
private, and protected sections within the class body. The
keywords public, private, and protected are called access
specifies.
A class can have multiple public, protected, or private
labeled sections. The default access for members and
classes is private.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
Example
Class Test{
Private:
// data members
Public:
// data members
Protected:
// data members
};
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
The Public members
A public member is Class Test{ Main(){
accessible from Public: //access pacifier
anywhere outside int a;
the class but within Public: //access pacifier // code
a program. You can Void show(){
set and get the Cout<<a;
}
value of public }
variables without };
any member
function as shown in
the example:
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
The Private members
• A private member Class Test{
variable or function private: //access pacifier
Main(){
cannot be accessed, or
even viewed from
int salary;
outside the class. Only Public: //access pacifier
the class and friend int tax;
// code
functions(not Member
functions, outside,) can Void setsalary(int x);
access private };
members. Test::setsalary(int x){
}
• By default all the Salary=x;
members of a class
would be private. Cout<<salary;
}
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
The Protected members
• A protected member class Test {
variable or function is protected:
Main(){
very similar to a int tax=10;
private member but it };
class Test2 : Test{
provided one
public:
// code
additional benefit that
they can be accessed int
salary=50;
in child classes which
are called derived int calc(){
}
classes. The derived
int total=salary-tax;
classes will cover
return
later in details. total;
}
};
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Access modifiers in
Inheritance*
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Chapter 4
Inheritance
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
1.Classes
2.Inheritance
3.Polymorphism
4.Encapsulation
5.Abstraction
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Inheritance
• Inheritance is one of the key
feature of object-oriented
programming including C++
which allows user to create a
new class(derived class) from a
existing class(base class). The
derived class inherits all feature
from a base class and it can have
additional features of its own.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Concept of Inheritance
Suppose, you want to calculate either area and
perimeter of a rectangle by taking data(length and
breadth) from user. You can create two different
objects( Area, Perimeter) and asks user to enter length
and breadth in each object and calculate corresponding
data. But, the better approach would be to create a
additional object Rectangle to store value of length and
breadth from user and derive objects Area and
Perimeter from Rectangle base class. It is because, the
two objects Area, Perimeter are related to
object Rectangle and you don't need to ask user the
input data from these two derived objects as this
feature is included in base class.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
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.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Inheritance
In C++ we have To understand it practically watch video on
YouTube, channel name : Zarif Bahaduri
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical
Inheritance
• Hybrid (Virtual)
Inheritance
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Inheritance
In C++ we have To understand it practically watch video on
YouTube, channel name : Zarif Bahaduri
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid (Virtual)
Inheritance
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Inheritance
In C++ we have To understand it practically watch video on
YouTube, channel name : Zarif Bahaduri
• Single Inheritance
• Multiple Inheritance
• Multilevel
Inheritance
• Hierarchical Inheritance
• Hybrid (Virtual)
Inheritance
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Inheritance
In C++ we have To understand it practically watch video on
YouTube, channel name : Zarif Bahaduri
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical
Inheritance
• Hybrid (Virtual)
Inheritance
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Types of Inheritance
In C++ we have To understand it practically watch video on
YouTube, channel name : Zarif Bahaduri
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical
Inheritance
• Hybrid (Virtual)
Inheritance
Watch videos on YouTube.
Function Overloading Channel Name: Zarif Bahaduri
&Function Overriding
• Function Overloading : Whenever same method name is exiting
multiple times in the same class with different number of parameter or
different order of parameters or different types of parameters is known
as Function overloading.
• Function Overriding: If base class and derived class have member
functions with same name and arguments. If you create an object of
derived class and write code to access that member function then, the
member function in derived class is only invoked.
the member function of derived class overrides the member function of
base class. This feature in C++ programming is known as function
overriding.
Watch videos on YouTube.
Function Overloading Channel Name: Zarif Bahaduri
Function
Overloadi
ng
Watch videos on YouTube.
Polymorphism Channel Name: Zarif Bahaduri
• In shopping behave like a
Function Overloading, Overriding and Virtual
Functions customer
• In bus behave like a
• The process of representing one Form in multiple forms passenger
is known as Polymorphism. Here one form represent
original form or original method always resides in base • In school behave like a
class and multiple forms represents overridden method student
which resides in derived classes. • In home behave like a son
• Polymorphism is derived from 2 Greek words: poly and
morphs. The word "poly" means many
and morphs means forms. So polymorphism means
many forms.
• Suppose if you are in class room that time you behave
like a student, when you are in market at that time you
behave like a customer, when you at your home at that
time you behave like a son or daughter, Here one
person have different-different behaviors.
Watch videos on YouTube.
Types of Channel Name: Zarif Bahaduri
Polymorphism
• Compile time polymorphism-: The overloaded
member function are selected for invoking by
matching arguments both type and number. this
information is known to the compile at the compile
time.
• compiler is able to select the appropriate function
for a particular call at the compile time itself. This is
called early binding or static binding or static
linking. Also known as compile time
polymorphism
• Function Overloading & Operator Overloading
are the best examples of compile time
Polymorphism
Watch videos on YouTube.
Types of Channel Name: Zarif Bahaduri
Polymorphism
• We have already discussed Function
overloading . Go back and check the video.
• Lets take a look on operator overloading.
• In C++, it's possible to change the way
operator works (for user-defined types)
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Operator Overloading t a x
Syn
• We have already discussed Function overloading .
Go back and check the video.
• Lets take a look on operator overloading.
• C++ programming allows programmer to redefine
the meaning of an operator (when they operate on class objects) is known as
operator overloading.
• The meaning of an operator is always same for variable of basic types like: int,
float, double etc. For example: To add two integers, + operator is used. However,
for user-defined types (like: objects), you can redefine the way operator works.
• For example: If there are two objects of a class that contains string as its data
members. You can redefine the meaning of + operator and use it to concatenate
those strings.
Watch videos on YouTube.
Operator Overloading Channel Name: Zarif Bahaduri
Lets program it practically, I will use
Structures, and classes in order to
understand Operator overloading
completely.
Watch videos on YouTube.
Operator Overloading Channel Name: Zarif Bahaduri
Lets program it practically, I will use
Structures, and classes in order to
understand Operator overloading completely.
y=2. box1
5 x=
Area =7.5
3 y=5. box3 = box1+box2
5
x=7
Area =38.5
y=3 box2
x=4
Area =12
Watch videos on YouTube.
Types of Channel Name: Zarif Bahaduri
Polymorphism
• Run time Polymorphism -: It is known what
object are under consideration the appropriate
version of the function is invoked since the
function is linked with a particular class much
later after the compilation, this process is termed
as late binding. It is also known as dynamic
binding because this section of the appropriate
function is done dynamically at run time.
• Dynamic binding is a powerful feathers of C++
programming language. This requires to object.
• Function Overriding is the best example of it,
• Lets go for virtual function.
Watch videos on YouTube.
Types of Channel Name: Zarif Bahaduri
Polymorphism
Before using virtual function you must have information about pointer to a class.
• A virtual function is a member function which is
declared within base class and is re-defined
(Overridden) by derived class. When you refer to a
derived class object using a pointer or a reference to the
base class, you can call a virtual function for that object
and execute the derived class’s version of the function.
• Virtual functions ensure that the correct function is
called for an object, regardless of the type of reference
(or pointer) used for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base
class.
• The resolving of function call is done at Run-time.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Pointer to a class
• We have already discussed pointer to
Variable, Arrays and Structures.
• A pointer to a C++ class is done exactly.
To access members of a pointer to a class
you use the member access operator -
> operator. And you must initialize the
pointer before using it.
• Understand the concept of pointer to a
class:
Watch videos on YouTube.
Abstraction
Watch videos on YouTube.
Encapsulation Vs Data Channel Name: Zarif Bahaduri
Abstraction
• Abstraction hides the
irrelevant details found in
the code.
• Encapsulated Code is quite
flexible and easy to change
with new requirements.
• In abstraction, problems
are solved at the design or
interface level.
• In encapsulation, problems
are solved at the
implementation level.
Watch videos on YouTube.
Friend Function
A friend function of a class is defined Friend Function
outside that class, but it has the right Definition
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.
To declare a function as a friend of a
class, precede the function prototype
in the class definition with
keyword friend as follows:
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Operator
Operator is a special symbol that tells the compiler to
perform specific mathematical or logical Operation.
Operator
Bitwise Operators
1. | bitwise OR
2. ~ bitwise NOT NOTE:
|, & bitwise
0011 = 3 If you Remember, logical |OR evaluates to true (1). if
0101 = 5 | OR either the left or the right or both operands are true
0111 = 7 (1). Bitwise OR evaluates to 1 if either bit (or both) is
1. So, 3 | 5 evaluates like this:
~ , ^ bitwise
0011 = 3 Bitwise XOR (^) also known as exclusive or. When
0101 = 5 ^ XOR evaluating two operands, XOR evaluates to true (1) if
0110 = 6 one and only one of it's operands is true (1). If neither
or both are true, it evaluates to 0.
12 = 1100
12 >> 1 = 0110 = 6
12 >> 2 = 0011 = 3 >> bitwise right
12 >> 3 = 0001 = 1 shift
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Important Topics
Friend Function Discussed already
click here
Virtual function Discussed already
click here
Inline function Discussed already
Static Keyword click here
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.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
User Defined Data Types
To declare a new data type in C++ we will discuss only:
• Typedef
• Union
• Structures
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
Typedef
• Typedefs allow you to create an alias for a data type, and
use the aliased name instead of the actual type name. To
declare a typedef, simply use the typedef keyword,
followed by the type to alias, followed by the alias name:
Example
Typedef int
typedef double distance; //typedef alias is declare
numbers;
Main(){
distance a; = double a; numbers a;
a=90;
cout<<a;
}
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
Union
• A union is a user-defined data type
• To begin the declaration of a union with the union
keyword, and enclose the member list in curly braces:
Union Data{
union tag int numbers;
Double dec;
{
Exampl String text;
member-list } ver1;
e Main(){
}declaretor; Ver1.numbers=90
0;
cout<<ver1.numb
ers;
}
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
3
Structures
• A structure is one or a group of variables considered as a (custom) data
type. To create a structure, use the struct keyword followed by a name
for the object, at least followed by a semi-colon. It can be created as
follow
struct data{
//body of structure struct data ver1;
Int num;
Doubl dic; Ver1.text=“your text";
String text;
}; cout<<ver1.text;