0% found this document useful (0 votes)
21 views93 pages

C++ Oop To Advanced

The document provides an overview of Object-Oriented Programming (OOP) concepts, including programming languages, classes, objects, constructors, destructors, and inheritance. It explains the differences between machine, assembly, and high-level languages, and details the advantages of OOP at both management and technical levels. Additionally, it covers access control modifiers and the various types of inheritance in C++.

Uploaded by

haroonsadiq331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views93 pages

C++ Oop To Advanced

The document provides an overview of Object-Oriented Programming (OOP) concepts, including programming languages, classes, objects, constructors, destructors, and inheritance. It explains the differences between machine, assembly, and high-level languages, and details the advantages of OOP at both management and technical levels. Additionally, it covers access control modifiers and the various types of inheritance in C++.

Uploaded by

haroonsadiq331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 93

Watch videos on YouTube.

Channel Name: Zarif Bahaduri

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

• Programming languages allow


programmers to code software.
• The three major families of languages are:
• Machine languages
• Assembly languages
• High-Level languages
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

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

• Assembly languages are a step towards easier


programming.
• Assembly languages are comprised of a set of
elemental commands which are tied to a
specific processor.
• Assembly language code needs to be
translated to machine language before the
computer processes it.

• 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

A Class is a 3-Compartment Box 2

• A class can be visualized as a three-compartment box,


• Classname (or identifier): identifies the class.
• Data
Members or Variables (or attributes, states, fields):
contains the static attributes of the class.
• Member Functions (or methods, behaviors, operations):
contains the dynamic operations of the class.
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

Creating Instances(object) of a Class 2

To create an instance of a class:


• Declare an instance identifier (name) of a particular class.
• Invoke a constructor to construct the instance (for example
allocate storage for the instance and initialize the variables).
• For examples, suppose that we have a class called Circle, we
can create instances of Circle as follows:
// Construct 3 instances of the class Circle: c1,
c2, and c3
Circle c1(1.2, "red"); // radius, color
Circle c2(3.4); // radius, default color
Circle c3; // default radius and color
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

Dot (.) Operator 2

• To reference a member of a object (data member or function),


you must:
1. First identify the instance you are interested in, and then
2. Use the dot operator (.) to reference the member, in the form of
instanceName.memberName.
• For example, suppose that we have a class called Circle, with two data
members (radius and color) and two functions (getRadius() and
getArea()). We have created three instances of the class Circle, namely,
c1, c2 and c3. To invoke the function getArea(), you must first identity
the instance of interest, says c2, then use the dot operator, in the form
of c2.getArea(), to invoke the getArea() function of instance c2.
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

Data member (Variable) & Member2


Functions
• A data member (variable) has a name (or identifier) and a
type; and holds a value of that particular type.
• A member function
1. Receives parameters from the caller,
2. Performs the operations defined in the function body, and
3. Returns a piece of result (or void) to the caller.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

OOP Practical Example 2


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

• A constructor is a special function that has the function


name same as the classname.
// Constructor has the same name as the class
Circle(double r = 1.0, string c = "red") {
radius = r;
color = c; Constructor object Creating and passing values
}
Circle c1(1.2, "blue");
Circle c2(3.4); // default color
Circle c3; // default radius and color
// Take note that there is no
empty bracket ()
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

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

and Copy Constructor


Constructor
Overloading

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

These are OOP main building blocks

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

Access modifiers in Inheritance


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 Overriding Function


Overridin
g

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.

This-keyword Channel Name: Zarif Bahaduri

“this” keyword is used when


• If Local variable have same name as data member.
• When a reference to a local object is returned, the returned reference can
be used to chain function calls on a single object
• delete this
• It kills the object, we recommend you not to use “delete this” in your
program.
Watch videos on YouTube.

Encapsulation Channel Name: Zarif Bahaduri

• Encapsulation is an Object Oriented Programming


concept that binds together the data and functions
and keeps both safe from external attack and misuse.
Data encapsulation led to the important OOP concept
of data hiding.
Advantage of Encapsulation
• The main advantage of using of encapsulation is to
secure the data from other methods, when we make
a data private then these data only use within the
class, but these data not accessible outside the class.
• 1) Make all the data members private.
2) Create public setter and getter functions for
each data member in such a way that the set
function set the value of data member and get
function get the value of data member.
Watch videos on YouTube.

Encapsulation Channel Name: Zarif Bahaduri

• Real time example


• In real live we have a Capsule in which
all the medicine is encapsulate so the
medicine is safe from the external
attack.
• In C++ we can consider the
encapsulation mechanism as a Capsule
in which our Methods and variable are
safe from other classes or other methods
Watch videos on YouTube.

Data Abstraction Channel Name: Zarif Bahaduri

• Data Abstraction is a concept which


shouldn't be confused with Abstract classes,
it is separated.
• Data Abstraction is an OOP concept that
focuses only on relevant data of an object. It
hides the background details and emphasizes
the essential data points for reducing the
complexity and increase efficiency.
Abstraction method mainly focuses on the
idea instead of actual functioning.
• Abstraction hides the irrelevant details found
in the code.
Watch videos on YouTube.
Encapsulation Vs Data Channel Name: Zarif Bahaduri
Abstraction Encapsulation

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.

Passing Argument Channel Name: Zarif Bahaduri

• There are three ways of passing


argument.
1. By passing values
2. By passing reference y=1
x=9
0
3. By passing address, pointer b=1
a=9
• 1:- Passing by values: By default, C++ 0
copies the actual value of an argument z=9
into the formal parameter of the
function. In this case, changes made to
the parameter inside the function have
no effect on the argument. call by value
to pass arguments. In general, this
means that code within a function
cannot alter the arguments used to call
the function.
Watch videos on YouTube.

Passing Argument Channel Name: Zarif Bahaduri

• There are three ways of passing


argument.
1. By passing values
2. By passing reference x y
b=1
3. By passing address, pointer a=9
0
• 2:- Passing by Reference: copies the z=9
reference of an argument into the
formal parameter. Inside the
function, the reference is used to
access the actual argument used in
the call, this means that changes
made to the parameter affect the
passed argument.
Watch videos on YouTube.

Passing Argument Channel Name: Zarif Bahaduri

• There are three ways of passing


argument.
1. By passing values
#00 #01
2. By passing reference 1 0
b=1
3. By passing address, pointer a=9
0
• 3:- Passing by address/pointer:
*x *y
copies the address of an argument #001 #010
into the formal parameter. Inside
the function, the address is used to z=9
access the actual argument used in
the call, this means that change
made to the parameter effect the
passed argument.
Watch videos on YouTube.
Virtual Function & Pure Channel Name: Zarif Bahaduri
Virtual function
• Virtual functions power polymorphism concept, while a
pure virtual functions lead us to the Abstract classes.
• A virtual function is a member function in the base
class that you redefine in a derived class. It is declared
using the virtual keyword.

• you cannot set a non-virtual function=0;


• virtual keyword does not work on class
variables
Watch videos on YouTube.

Abstract classes Channel Name: Zarif Bahaduri

• Are those classes having pure-virtual function


• We can not instantiate or can not make
object/instance of these classes same as when
the constructor of a class is private or
protected.
• The derived class of Abstract class must
implement the pure-virtual function otherwise
object of derived class is an error.
• An Abstract class can have a pure-virtual
function plus other non-virtual functions and
data members.
Watch videos on YouTube.

Interfaces Channel Name: Zarif Bahaduri

• Interface is an Abstract class with NO


member variable, it contain on pure virtual
functions.
• Interfaces are only a concept.
• All the pure-functions of an interfaces must
be implemented in derived classes.
Watch videos on YouTube.

Virtual classes Channel Name: Zarif Bahaduri

• Virtual base classes, used in virtual inheritance,


is a way of preventing multiple "instances" of a
given class appearing in an inheritance
hierarchy when using multiple inheritance.
• An instance of textOffice will be made up of
truck, which includes vehicle, and car which
also includes vehicle. So you have two
"instances" (for want of a better expression) of
vehicle. When you have this scenario, you have
the possibility of ambiguity.
• When you specify virtual when inheriting your
classes, you're telling the compiler that you only
want a single instance.
Watch videos on YouTube.

Virtual classes Channel Name: Zarif Bahaduri

• Virtual base classes, used in virtual inheritance,


is a way of preventing multiple "instances" of a
given class appearing in an inheritance
hierarchy when using multiple inheritance.
• An instance of textOffice will be made up of
truck, which includes vehicle, and car which
also includes vehicle. So you have two
"instances" (for want of a better expression) of
vehicle. When you have this scenario, you have
the possibility of ambiguity.
• When you specify virtual when inheriting your
classes, you're telling the compiler that you only
want a single instance.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Static–keyword
• Static is a keyword gives special characteristics to
an element. Static elements are allocated storage
only once in a program lifetime in static storage
area. And they have a scope till the program
lifetime. Static Keyword can be used with.
• Static variable in functions
• Static Variable in class
• Static Class Objects
• Static function in class
1. Static variable in function: are initialized only
once, and then they hold there value even through
function calls, These static variables are stored on
static storage area , not in stack.
• NOTE: java doesn’t allow static locale variable in a
function
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Static–keyword
2. Static variable in class: As the variables
declared as static are initialized only once
and as they are allocated space in separate
static storage so, the static variables in a
class are shared by the objects. There can
not be multiple copies of same static
variables for different objects. Also because
of this reason static variables can not be
initialized using constructors. it must be
initialized explicitly, always outside the class.
If not initialized, it will give error.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Static–keyword
3. Static Class Object: Just like
variables, objects also when declared
as static have a scope till the lifetime
of program. the destructor is invoked
after the end of main. This happened
because the scope of static object is
through out the life time of program.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Static–keyword
• 4. Static function in class: Like static
member or variable of a class static member
functions also does not depend on object of
class. We are allowed to invoke a static
member function using the object and the ‘.’
operator but it is recommended to invoke the
static members using the class name and the
scope resolution operator.
Static member functions are allowed to
access only the static data members or
other static member functions, they can not
access the non-static data members or
member functions of the class. It doesn't have
any "this" keyword which is the reason it
cannot access ordinary members.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri
Static–keyword
• 4. Static function in class: Like static
member or variable of a class static
member functions also does not depend on
object of class. We are allowed to invoke a
static member function using the object and
the ‘.’ operator but it is recommended to
invoke the static members using the class
name and the scope resolution operator.
Static member functions are allowed to
access only the static data members or
other static member functions, they can
not access the non-static data members or
member functions of the class. It doesn't
have any "this" keyword which is the reason
it cannot access ordinary members.
Watch videos on YouTube.
Friend Function Channel Name: Zarif Bahaduri
• Calling a normal function causes overhead(jumping from one point
to another and back to its previous point / stacking arguments). 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.

NORMAL FUNCTION INLINE FUNTION

main () main () Compil


e
{ Overhe time
ad
{
on trol myfun(
//… C //…
r an sfer )
NO
//… t myfun(); Control
{ transfer
myfun(); //… {
//… //…
//… //… //…
//… //.. }
} }
}
Watch videos on YouTube.
Friend Function Channel Name: Zarif Bahaduri
• Calling a normal function causes overhead(jumping from one point
to another and back to its previous point / stacking arguments). 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.

NORMAL FUNCTION INLINE FUNTION

main () main () Compil


e
{ Overhe time
ad
{
on trol myfun(
//… C //…
r an sfer )
NO
//… t myfun(); Control
{ transfer
myfun(); //… {
//… //…
//… //… //…
//… //.. }
} }
}
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

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.

Here we discuss only


the unary operator
and binary operators
in details .
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

Operator

Lets do the practical program for each in Computer Lab


Watch videos on YouTube.
Channel Name: Zarif Bahaduri

Bit Wise Operator

A bitwise operation operates on one or more bit patterns or


binary numerals at the level of their individual bits. It is a fast,
primitive action directly supported by the processor, and is used
to manipulate values for comparisons and calculations.
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

Bitwise Operators
1. | bitwise OR
2. ~ bitwise NOT NOTE:

3. & bitwise AND


To understand completely
bitwise operators you, must
4. ^ bitwise XOR have a clear DLD logical
5. << bitwise left shift gats concept.

6. >> bitwise right shift


Watch videos on YouTube.
Channel Name: Zarif Bahaduri

|, & 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:

0011 = 3 Bitwise AND works similarly. Logical &AND evaluates


& AND to true if both the left and right operand evaluate to
0101 = 5
true. Bitwise AND evaluates to true if both bits in the
0001 = 1
column are 1)
s the bitwise XOR (^), also known as exclusive or. When evaluating two operands, XOR evaluates to true (1) if one and only one of it's operands is true (1). If neither or both are true, it evaluates to 0. Consider the
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

~ , ^ 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.

0011 = 3 The bitwise NOT operator (~) is perhaps the


~ NOT easiest to understand of all the bitwise
1100=12 operators. It simply flips each bit from a 0 to a 1,
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

<<, >> bitwise


3 = 0011
3 << 1 = 0110 = 6
3 << 2 = 1100 = 12 << Bitwise left
3 << 3 = 1000 = 8 shift

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

Pointer and functions


Pointer and strings
Watch videos on YouTube.
Channel Name: Zarif Bahaduri

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;

You might also like