SlideShare a Scribd company logo
1 of 39
OOPS & C++(UNIT 4)
BY:SURBHI SAROHA
SYLLABUS
• Pointer
• Polymorphism & Inheritance
• Pointer(Pointer to object ,this pointer, pointer to derive class)
• Introduction to polymorphism(Runtime polymorphism, compiletime polymorphism)
• Operator overloading
• Virtual function
• Inheritance(Single inheritance, multiple inheritance,multilevel inheritance,hierarchical inheritance,hybrid
inheritance)
• Virtual Base class
• Abstract class
POINTER
• When a variable is created in C++, a memory address is assigned to the variable. And when we assign a value
to the variable, it is stored in this memory address.
• To access it, use the & operator, and the result will represent where the variable is stored:
• #include <iostream>
• #include <string>
• using namespace std;
• int main() {
• string food = "Pizza";
• cout << &food;
• return 0;
• }
POINTER
• A pointer however, is a variable that stores the memory address as its value.
• A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator.
The address of the variable you're working with is assigned to the pointer:
• #include <iostream>
• #include <string>
• using namespace std;
• int main() {
• string food = "Pizza"; // A string variable
• string* ptr = &food; // A pointer variable that stores the address of food
• // Output the value of food
• cout << food << "n";
CONT……
• // Output the memory address of food
• cout << &food << "n";
• // Output the memory address of food with the pointer
• cout << ptr << "n";
• return 0;
• }
POLYMORPHISM & INHERITANCE
• Polymorphism means "many forms", and it occurs when we have many classes that are related to each
other by inheritance.
• Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from
another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a
single action in different ways.
POLYMORPHISM & INHERITANCE
• // Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound n";
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee n";
}
};
CONT….
• // Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow n";
}
};
POINTER(POINTER TO OBJECT ,THIS POINTER,POINTER
TO DERIVE CLASS)
• A pointer is a variable that stores the memory address of another variable (or object) as its value. A
pointer aims to point to a data type which may be int, character, double, etc.
• Pointers to objects aim to make a pointer that can access the object, not the variables. Pointer to object
in C++ refers to accessing an object.
• There are two approaches by which you can access an object. One is directly and the other is by using a
pointer to an object in C++.
// EXAMPLE USING AN OBJECT POINTER.
• #include <iostream>
• using namespace std;
• class My_Class {
• int num;
• public:
• void set_number(int value) {num = value;}
• void show_number();
• };
• void My_Class::show_number()
• {
• cout << num << "n";
• }
CONT…..
• int main()
• {
• My_Class object, *p; // an object is declared and a pointer to it
• object.set_number(1); // object is accessed directly
• object.show_number();
CONT….
• p = &object; // the address of the object is assigned to p
• p->show_number(); // object is accessed using the pointer
• return 0;
• }
THIS POINTER
• In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3
main usage of this keyword in C++.
•
It can be used to pass current object as a parameter to another method.
• It can be used to refer current class instance variable.
• It can be used to declare indexers.
•
THIS POINTER
• #include <iostream>
• using namespace std;
• class Employee {
• public:
• int id; //data member (also instance variable)
• string name; //data member(also instance variable)
• float salary;
• Employee(int id, string name, float salary)
• {
• this->id = id;
• this->name = name;
• this->salary = salary;
• }
CONT….
• void display()
• {
• cout<<id<<" "<<name<<" "<<salary<<endl;
• }
• };
• int main(void) {
• Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
• Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
• e1.display();
• e2.display();
• return 0;
• }
POINTER TO DERIVE CLASS
• A pointer is a data type that stores the address of other data types. Pointers can be used for base
objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to
the object of the base class are type-compatible (may be used in different ways).
CONT…..
• A derived class is a class that takes some properties from its base class.
• It is true that a pointer of one class can point to another class, but classes must be a base and derived
class, then it is possible.
• To access the variable of the base class, a base class pointer will be used.
• So, a pointer is a type of base class, and it can access all, public function and variables of the base class
since the pointer is of the base class, this is known as a binding pointer.
• In this pointer base class is owned by the base class but points to the derived class object.
• The same works with derived class pointer, values are changed.
INTRODUCTION TO POLYMORPHISM(RUNTIME
POLYMORPHISM, COMPILETIME POLYMORPHISM)
• The word “polymorphism” means having many forms.
• In simple words, we can define polymorphism as the ability of a message to be displayed in more than
one form.
• A real-life example of polymorphism is a person who at the same time can have different
characteristics.
• A man at the same time is a father, a husband, and an employee.
• So the same person exhibits different behavior in different situations. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming.
TYPES OF POLYMORPHISM
FUNCTION OVERLOADING
• When there are multiple functions with the same name but different parameters, then the functions
are said to be overloaded, hence this is known as Function Overloading.
• Functions can be overloaded by changing the number of arguments or/and changing the type of
arguments.
• In simple terms, it is a feature of object-oriented programming providing many functions that have the
same name but distinct parameters when numerous tasks are listed under one function name.
• There are certain Rules of Function Overloading that should be followed while overloading a function.
OPERATOR OVERLOADING
• C++ has the ability to provide the operators with a special meaning for a data type, this ability is known
as operator overloading.
• For example, we can make use of the addition operator (+) for string class to concatenate two strings.
• We know that the task of this operator is to add two operands.
• So a single operator ‘+’, when placed between integer operands, adds them and when placed between
string operands, concatenates them.
RUNTIME POLYMORPHISM
• This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism
are other names for runtime polymorphism. The function call is resolved at runtime in runtime
polymorphism. In contrast, with compile time polymorphism, the compiler determines which function
call to bind to the object after deducing it at runtime.
• A. Function Overriding
• Function Overriding occurs when a derived class has a definition for one of the member functions of the
base class. That base function is said to be overridden.
VIRTUAL FUNCTION
• A virtual function is a member function that is declared in the base class using the keyword virtual and
is re-defined (Overridden) in the derived class.
• Some Key Points About Virtual Functions:
• Virtual functions are Dynamic in nature.
• They are defined by inserting the keyword “virtual” inside a base class and are always declared with a
base class and overridden in a child class
• A virtual function is called during Runtime
VIRTUAL FUNCTION
• A virtual function (also known as virtual methods) is a member function that is declared within a base
class and is re-defined (overridden) by a 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 method.
• Virtual functions ensure that the correct function is called for an object, regardless of the type of
reference (or pointer) used for the function call.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
RULES FOR VIRTUAL FUNCTIONS
• The rules for the virtual functions in C++ are as follows:
• Virtual functions cannot be static.
• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime
polymorphism.
• The prototype of virtual functions should be the same in the base as well as the derived class.
• They are always defined in the base class and overridden in a derived class. It is not mandatory for the
derived class to override (or re-define the virtual function), in that case, the base class version of the
function is used.
• A class may have a virtual destructor but it cannot have a virtual constructor.
// C++ PROGRAM TO ILLUSTRATE
// CONCEPT OF VIRTUAL FUNCTIONS
•
• #include <iostream>
• using namespace std;
•
• class base {
• public:
• virtual void print() { cout << "print base classn"; }
•
• void show() { cout << "show base classn"; }
• };
CONT…..
•
• class derived : public base {
• public:
• void print() { cout << "print derived classn"; }
•
• void show() { cout << "show derived classn"; }
• };
CONT….
• int main()
• {
• base* bptr;
• derived d;
• bptr = &d;
• // Virtual function, binded at runtime
• bptr->print();
• // Non-virtual function, binded at compile time
• bptr->show();
• return 0;
• }
LIMITATIONS OF VIRTUAL FUNCTIONS
• Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult
for the compiler to optimize because it does not know exactly which function is going to be called at
compile time.
• Difficult to Debug: In a complex system, virtual functions can make it a little more difficult to figure out
where a function is being called from.
INHERITANCE(SINGLE INHERITANCE, MULTIPLE
INHERITANCE,MULTILEVEL INHERITANCE,HIERARCHICAL
INHERITANCE,HYBRID INHERITANCE)
• The capability of a class to derive properties and characteristics from another class is called Inheritance.
Inheritance is one of the most important features of Object-Oriented Programming.
• Inheritance is a feature or a process in which, new classes are created from the existing classes. The
new class created is called “derived class” or “child class” and the existing class is known as the “base
class” or “parent class”. The derived class now is said to be inherited from the base class.
• The derived class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
VIRTUAL BASE CLASS
• Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given
class appearing in an inheritance hierarchy when using multiple inheritances.
• Need for Virtual Base Classes: Consider the situation where we have one class A . This class A is
inherited by two other classes B and C. Both these class are inherited into another in a new class D as
shown in figure below.
• As we can see from the figure that data members/function of class A are inherited twice to class D. One
through class B and second through class C. When any data / function member of class A is accessed by
an object of class D, ambiguity arises as to which data/function member would be called? One inherited
through B or the other inherited through C. This confuses compiler and it displays error.
VIRTUAL BASE CLASS
• #include <iostream>
• using namespace std;
•
• class A {
• public:
• void show()
• {
• cout << "Hello form A n";
• }
• };
•
CONT……
• class B : public A {
• };
•
• class C : public A {
• };
•
• class D : public B, public C {
• };
•
CONT…..
• int main()
• {
• D object;
• object.show();
• }
HOW TO RESOLVE THIS ISSUE?
• To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base class
by placing a keyword virtual as :
• Syntax for Virtual Base Classes:
• Syntax 1:
• class B : virtual public A
• {
• };
• Syntax 2:
• class C : public virtual A
• {
• };
CONT….
• virtual can be written before or after the public. Now only one copy of data/function member will be
copied to class C and class B and class A becomes the virtual base class.
• Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use
multiple inheritances.
• When a base class is specified as a virtual base, it can act as an indirect base more than once without
duplication of its data members.
• A single copy of its data members is shared by all the base classes that use virtual base.
ABSTRACT CLASS
• By definition, a C++ abstract class must include at least one pure virtual function. Alternatively, put a
function without a definition. Because the subclass would otherwise turn into an abstract class in and of
itself, the abstract class's descendants must specify the pure virtual function.
• Broad notions are expressed using abstract classes, which can then be utilized to construct more
specific classes. You cannot make an object of the abstract class type.
• However, pointers and references can be used to abstract class types. When developing an abstract
class, define at least one pure virtual feature. A virtual function is declared using the pure specifier (= 0)
syntax.
CONT…..
• C-lass classname //abstract class
• {
• //data members
• public:
• //pure virtual function
• /* Other members */
• };
THANK YOU 

More Related Content

Similar to OOPS & C++(UNIT 4)

Similar to OOPS & C++(UNIT 4) (20)

Lecture02
Lecture02Lecture02
Lecture02
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
Functions
FunctionsFunctions
Functions
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptx
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 

More from SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

OOPS & C++(UNIT 4)

  • 1. OOPS & C++(UNIT 4) BY:SURBHI SAROHA
  • 2. SYLLABUS • Pointer • Polymorphism & Inheritance • Pointer(Pointer to object ,this pointer, pointer to derive class) • Introduction to polymorphism(Runtime polymorphism, compiletime polymorphism) • Operator overloading • Virtual function • Inheritance(Single inheritance, multiple inheritance,multilevel inheritance,hierarchical inheritance,hybrid inheritance) • Virtual Base class • Abstract class
  • 3. POINTER • When a variable is created in C++, a memory address is assigned to the variable. And when we assign a value to the variable, it is stored in this memory address. • To access it, use the & operator, and the result will represent where the variable is stored: • #include <iostream> • #include <string> • using namespace std; • int main() { • string food = "Pizza"; • cout << &food; • return 0; • }
  • 4. POINTER • A pointer however, is a variable that stores the memory address as its value. • A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator. The address of the variable you're working with is assigned to the pointer: • #include <iostream> • #include <string> • using namespace std; • int main() { • string food = "Pizza"; // A string variable • string* ptr = &food; // A pointer variable that stores the address of food • // Output the value of food • cout << food << "n";
  • 5. CONT…… • // Output the memory address of food • cout << &food << "n"; • // Output the memory address of food with the pointer • cout << ptr << "n"; • return 0; • }
  • 6. POLYMORPHISM & INHERITANCE • Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. • Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
  • 7. POLYMORPHISM & INHERITANCE • // Base class class Animal { public: void animalSound() { cout << "The animal makes a sound n"; } }; // Derived class class Pig : public Animal { public: void animalSound() { cout << "The pig says: wee wee n"; } };
  • 8. CONT…. • // Derived class class Dog : public Animal { public: void animalSound() { cout << "The dog says: bow wow n"; } };
  • 9. POINTER(POINTER TO OBJECT ,THIS POINTER,POINTER TO DERIVE CLASS) • A pointer is a variable that stores the memory address of another variable (or object) as its value. A pointer aims to point to a data type which may be int, character, double, etc. • Pointers to objects aim to make a pointer that can access the object, not the variables. Pointer to object in C++ refers to accessing an object. • There are two approaches by which you can access an object. One is directly and the other is by using a pointer to an object in C++.
  • 10. // EXAMPLE USING AN OBJECT POINTER. • #include <iostream> • using namespace std; • class My_Class { • int num; • public: • void set_number(int value) {num = value;} • void show_number(); • }; • void My_Class::show_number() • { • cout << num << "n"; • }
  • 11. CONT….. • int main() • { • My_Class object, *p; // an object is declared and a pointer to it • object.set_number(1); // object is accessed directly • object.show_number();
  • 12. CONT…. • p = &object; // the address of the object is assigned to p • p->show_number(); // object is accessed using the pointer • return 0; • }
  • 13. THIS POINTER • In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. • It can be used to pass current object as a parameter to another method. • It can be used to refer current class instance variable. • It can be used to declare indexers. •
  • 14. THIS POINTER • #include <iostream> • using namespace std; • class Employee { • public: • int id; //data member (also instance variable) • string name; //data member(also instance variable) • float salary; • Employee(int id, string name, float salary) • { • this->id = id; • this->name = name; • this->salary = salary; • }
  • 15. CONT…. • void display() • { • cout<<id<<" "<<name<<" "<<salary<<endl; • } • }; • int main(void) { • Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee • Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee • e1.display(); • e2.display(); • return 0; • }
  • 16. POINTER TO DERIVE CLASS • A pointer is a data type that stores the address of other data types. Pointers can be used for base objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to the object of the base class are type-compatible (may be used in different ways).
  • 17. CONT….. • A derived class is a class that takes some properties from its base class. • It is true that a pointer of one class can point to another class, but classes must be a base and derived class, then it is possible. • To access the variable of the base class, a base class pointer will be used. • So, a pointer is a type of base class, and it can access all, public function and variables of the base class since the pointer is of the base class, this is known as a binding pointer. • In this pointer base class is owned by the base class but points to the derived class object. • The same works with derived class pointer, values are changed.
  • 18. INTRODUCTION TO POLYMORPHISM(RUNTIME POLYMORPHISM, COMPILETIME POLYMORPHISM) • The word “polymorphism” means having many forms. • In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. • A real-life example of polymorphism is a person who at the same time can have different characteristics. • A man at the same time is a father, a husband, and an employee. • So the same person exhibits different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.
  • 20. FUNCTION OVERLOADING • When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded, hence this is known as Function Overloading. • Functions can be overloaded by changing the number of arguments or/and changing the type of arguments. • In simple terms, it is a feature of object-oriented programming providing many functions that have the same name but distinct parameters when numerous tasks are listed under one function name. • There are certain Rules of Function Overloading that should be followed while overloading a function.
  • 21. OPERATOR OVERLOADING • C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. • For example, we can make use of the addition operator (+) for string class to concatenate two strings. • We know that the task of this operator is to add two operands. • So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.
  • 22. RUNTIME POLYMORPHISM • This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism are other names for runtime polymorphism. The function call is resolved at runtime in runtime polymorphism. In contrast, with compile time polymorphism, the compiler determines which function call to bind to the object after deducing it at runtime. • A. Function Overriding • Function Overriding occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
  • 23. VIRTUAL FUNCTION • A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. • Some Key Points About Virtual Functions: • Virtual functions are Dynamic in nature. • They are defined by inserting the keyword “virtual” inside a base class and are always declared with a base class and overridden in a child class • A virtual function is called during Runtime
  • 24. VIRTUAL FUNCTION • A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a 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 method. • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call. • They are mainly used to achieve Runtime polymorphism. • Functions are declared with a virtual keyword in a base class. • The resolving of a function call is done at runtime.
  • 25. RULES FOR VIRTUAL FUNCTIONS • The rules for the virtual functions in C++ are as follows: • Virtual functions cannot be static. • A virtual function can be a friend function of another class. • Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism. • The prototype of virtual functions should be the same in the base as well as the derived class. • They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. • A class may have a virtual destructor but it cannot have a virtual constructor.
  • 26. // C++ PROGRAM TO ILLUSTRATE // CONCEPT OF VIRTUAL FUNCTIONS • • #include <iostream> • using namespace std; • • class base { • public: • virtual void print() { cout << "print base classn"; } • • void show() { cout << "show base classn"; } • };
  • 27. CONT….. • • class derived : public base { • public: • void print() { cout << "print derived classn"; } • • void show() { cout << "show derived classn"; } • };
  • 28. CONT…. • int main() • { • base* bptr; • derived d; • bptr = &d; • // Virtual function, binded at runtime • bptr->print(); • // Non-virtual function, binded at compile time • bptr->show(); • return 0; • }
  • 29. LIMITATIONS OF VIRTUAL FUNCTIONS • Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult for the compiler to optimize because it does not know exactly which function is going to be called at compile time. • Difficult to Debug: In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from.
  • 30. INHERITANCE(SINGLE INHERITANCE, MULTIPLE INHERITANCE,MULTILEVEL INHERITANCE,HIERARCHICAL INHERITANCE,HYBRID INHERITANCE) • The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming. • Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class. • The derived class is the specialized class for the base class. • Sub Class: The class that inherits properties from another class is called Subclass or Derived Class. • Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
  • 31. VIRTUAL BASE CLASS • Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances. • Need for Virtual Base Classes: Consider the situation where we have one class A . This class A is inherited by two other classes B and C. Both these class are inherited into another in a new class D as shown in figure below. • As we can see from the figure that data members/function of class A are inherited twice to class D. One through class B and second through class C. When any data / function member of class A is accessed by an object of class D, ambiguity arises as to which data/function member would be called? One inherited through B or the other inherited through C. This confuses compiler and it displays error.
  • 32. VIRTUAL BASE CLASS • #include <iostream> • using namespace std; • • class A { • public: • void show() • { • cout << "Hello form A n"; • } • }; •
  • 33. CONT…… • class B : public A { • }; • • class C : public A { • }; • • class D : public B, public C { • }; •
  • 34. CONT….. • int main() • { • D object; • object.show(); • }
  • 35. HOW TO RESOLVE THIS ISSUE? • To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base class by placing a keyword virtual as : • Syntax for Virtual Base Classes: • Syntax 1: • class B : virtual public A • { • }; • Syntax 2: • class C : public virtual A • { • };
  • 36. CONT…. • virtual can be written before or after the public. Now only one copy of data/function member will be copied to class C and class B and class A becomes the virtual base class. • Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple inheritances. • When a base class is specified as a virtual base, it can act as an indirect base more than once without duplication of its data members. • A single copy of its data members is shared by all the base classes that use virtual base.
  • 37. ABSTRACT CLASS • By definition, a C++ abstract class must include at least one pure virtual function. Alternatively, put a function without a definition. Because the subclass would otherwise turn into an abstract class in and of itself, the abstract class's descendants must specify the pure virtual function. • Broad notions are expressed using abstract classes, which can then be utilized to construct more specific classes. You cannot make an object of the abstract class type. • However, pointers and references can be used to abstract class types. When developing an abstract class, define at least one pure virtual feature. A virtual function is declared using the pure specifier (= 0) syntax.
  • 38. CONT….. • C-lass classname //abstract class • { • //data members • public: • //pure virtual function • /* Other members */ • };