SlideShare une entreprise Scribd logo
1  sur  40
Inheritance
The mechanism by which one class can
inherit the properties of another.
It allows a hierarchy of classes to be built,
moving from the most general to the most
specific.
Base Class, Derived Class
Base Class
Defines all qualities common to any derived
classes.
Derived Class
Inherits those general properties and adds
new properties that are specific to that class.
Example: Base Class
class base {
int x;
public:
void setx(int n) { x = n; }
void showx() { cout << x << ‘n’ }
};
Example: Derived Class
// Inherit as public
class derived : public base {
int y;
public:
void sety(int n) { y = n; }
void showy() { cout << y << ‘n’;}
};
Access Specifier: public
The keyword public tells the compiler that
base will be inherited such that:
all public members of the base class will
also be public members of derived.
However, all private elements of base will
remain private to it and are not directly
accessible by derived.
Example: main()
int main() {
derived ob;
ob.setx(10);
ob.sety(20);
ob.showx();
ob.showy();
}
An incorrect example
class derived : public base {
int y;
public:
void sety(int n) { y = n; }
/* Error ! Cannot access x, which is
private member of base. */
void show_sum() {cout << x+y; }
};
Access Specifier: private
If the access specifier is private:
public members of base become private
members of derived.
these members are still accessible by
member functions of derived.
Example: Derived Class
// Inherit as private
class derived : private base {
int y;
public:
void sety(int n) { y = n; }
void showy() { cout << y << ‘n’;}
};
Example: main()
int main() {
derived ob;
ob.setx(10); // Error! setx() is private.
ob.sety(20); // OK!
ob.showx(); // Error! showx() is private.
ob.showy(); // OK!
}
Example: Derived Class
class derived : private base {
int y;
public:
// setx is accessible from within derived
void setxy(int n, int m) { setx(n); y = m; }
// showx is also accessible
void showxy() { showx(); cout<<y<< ‘n’;}
};
Protected Members
Sometimes you want to do the following:
keep a member of a base class private
allow a derived class access to it
Use protected members!
If no derived class, protected
members is the same as private
members.
Protected Members
The full general form of a class
declaration:
class class-name {
// private members
protected:
// protected members
public:
// public members
};
3 Types of Access Specifiers
Type 1: inherit as private
Base Derived
private members inaccessible
protected members private members
public members private members
3 Types of Access Specifiers
Type 2: inherit as protected
Base Derived
private members inaccessible
protected members protected members
public members protected members
3 Types of Access Specifiers
Type 3: inherit as public
Base Derived
private members inaccessible
protected members protected members
public members public members
Constructor and Destructor
It is possible for both the base class and
the derived class to have constructor
and/or destructor functions.
The constructor functions are executed in
order of derivation.
i.e.the base class constructor is executed
first.
The destructor functions are executed in
reverse order.
Passing arguments
What if the constructor functions of both
the base class and derived class take
arguments?
1. Pass all necessary arguments to the
derived class’s constructor.
2. Then pass the appropriate arguments
along to the base class.
Example: Constructor of base
class base {
int i;
public:
base(int n) {
cout << “constructing base n”;
i = n; }
~base() { cout << “destructing base n”; }
};
Example: Constructor of derived
class derived : public base {
int j;
public:
derived (int n, int m) : base (m) {
cout << “constructing derivedn”;
j = n; }
~derived() { cout << “destructing derivedn”;}
};
Example: main()
int main() {
derived o(10,20);
return 0;
}
constructing base
constructing derived
destructing derived
destructing base
Multiple Inheritance
Type 1:
base 1
derived 1
derived 2
Multiple Inheritance
• Type 2:
base 1 base 2
derived
Example: Type 2
// Create first base class
class B1 {
int a;
public:
B1(int x) { a = x; }
int geta() { return a; }
};
Example: Type 2
// Create second base class
class B2 {
int b;
public:
B2(int x) { b = x; }
int getb() { return b; }
};
// Directly inherit two base classes.
class D : public B1, public B2 {
int c;
public:
D(int x, int y, int z) : B1(z), B2(y) {
c = x; }
void show() {
cout << geta() << getb() << c;}
} ;
Example: Type 2
Potential Problem
Base is inherited twice by Derived 3!
Base Base
Derived 1 Derived 2
Derived 3
Virtual Base Class
To resolve this problem, virtual base class
can be used.
class base {
public:
int i;
};
Virtual Base Class
// Inherit base as virtual
class D1 : virtual public base {
public:
int j;
};
class D2 : virtual public base {
public:
int k;
};
Virtual Base Class
/* Here, D3 inherits both D1 and D2.
However, only one copy of base is
present */
class D3 : public D1, public D2 {
public:
int product () { return i * j * k; }
};
Pointers to Derived Classes
A pointer declared as a pointer to base
class can also be used to point to any
class derived from that base.
However, only those members of the
derived object that were inherited from the
base can be accessed.
Example
base *p; // base class pointer
base B_obj;
derived D_obj;
p = &B_obj; // p can point to base object
p = &D_obj; // p can also point to derived
// object
Virtual Function
A virtual function is a member function
declared within a base class
redefined by a derived class (i.e. overriding)
It can be used to support run-time
polymorphism.
Example
class base {
public:
int i;
base (int x) { i = x; }
virtual void func() {cout << i; }
};
Example
class derived : public base {
public:
derived (int x) : base (x) {}
// The keyword virtual is not needed.
void func() {cout << i * i; }
};
Example
int main() {
base ob(10), *p;
derived d_ob(10);
p = &ob;
p->func(); // use base’s func()
p = &d_ob;
p->func(); // use derived’s func()
}
Pure Virtual Functions
A pure virtual function has no definition
relative to the base class.
Only the function’s prototype is included.
General form:
virtual type func-name(paremeter-list) = 0
Example: area
class area {
public:
double dim1, dim2;
area(double x, double y)
{dim1 = x; dim2 = y;}
// pure virtual function
virtual double getarea() = 0;
};
Example: rectangle
class rectangle : public area {
public:
// function overriding
double getarea() {
return dim1 * dim2;
}
};
Example: triangle
class triangle : public area {
public:
// function overriding
double getarea() {
return 0.5 * dim1 * dim2;
}
};

Contenu connexe

Tendances

Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Javascript foundations: variables and types
Javascript foundations: variables and typesJavascript foundations: variables and types
Javascript foundations: variables and typesJohn Hunter
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Jakub Zalas
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
Design of OO language
Design of OO languageDesign of OO language
Design of OO languageGeorgiana T.
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructorsAkshaya Parida
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 

Tendances (20)

Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Javascript foundations: variables and types
Javascript foundations: variables and typesJavascript foundations: variables and types
Javascript foundations: variables and types
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
Inheritance
InheritanceInheritance
Inheritance
 
Design of OO language
Design of OO languageDesign of OO language
Design of OO language
 
Php Enums
Php EnumsPhp Enums
Php Enums
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Constructor
ConstructorConstructor
Constructor
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocol
 
Java2
Java2Java2
Java2
 

Similaire à Lab3

lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdfAneesAbbasi14
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Abu Saleh
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Abu Saleh
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceAnanda Kumar HN
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Abu Saleh
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)Ananda Kumar HN
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.pptinstaface
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Abu Saleh
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 

Similaire à Lab3 (20)

lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Lecture4
Lecture4Lecture4
Lecture4
 

Plus de karan saini

Thestoryofmylife 140221061604-phpapp01
Thestoryofmylife 140221061604-phpapp01Thestoryofmylife 140221061604-phpapp01
Thestoryofmylife 140221061604-phpapp01karan saini
 
Thestoryofmylife 140221061604-phpapp01 (1)
Thestoryofmylife 140221061604-phpapp01 (1)Thestoryofmylife 140221061604-phpapp01 (1)
Thestoryofmylife 140221061604-phpapp01 (1)karan saini
 
Snrg2011 6.15.2.sta canney_suranofsky
Snrg2011 6.15.2.sta canney_suranofskySnrg2011 6.15.2.sta canney_suranofsky
Snrg2011 6.15.2.sta canney_suranofskykaran saini
 
Risc and cisc eugene clewlow
Risc and cisc   eugene clewlowRisc and cisc   eugene clewlow
Risc and cisc eugene clewlowkaran saini
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterationskaran saini
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)karan saini
 
Helen keller-1226880485154369-8
Helen keller-1226880485154369-8Helen keller-1226880485154369-8
Helen keller-1226880485154369-8karan saini
 
Final 121114041321-phpapp01
Final 121114041321-phpapp01Final 121114041321-phpapp01
Final 121114041321-phpapp01karan saini
 
Engh 140118084844-phpapp01
Engh 140118084844-phpapp01Engh 140118084844-phpapp01
Engh 140118084844-phpapp01karan saini
 

Plus de karan saini (20)

Topology ppt
Topology pptTopology ppt
Topology ppt
 
Tibor
TiborTibor
Tibor
 
Thestoryofmylife 140221061604-phpapp01
Thestoryofmylife 140221061604-phpapp01Thestoryofmylife 140221061604-phpapp01
Thestoryofmylife 140221061604-phpapp01
 
Thestoryofmylife 140221061604-phpapp01 (1)
Thestoryofmylife 140221061604-phpapp01 (1)Thestoryofmylife 140221061604-phpapp01 (1)
Thestoryofmylife 140221061604-phpapp01 (1)
 
Snrg2011 6.15.2.sta canney_suranofsky
Snrg2011 6.15.2.sta canney_suranofskySnrg2011 6.15.2.sta canney_suranofsky
Snrg2011 6.15.2.sta canney_suranofsky
 
Science
ScienceScience
Science
 
Risc and cisc eugene clewlow
Risc and cisc   eugene clewlowRisc and cisc   eugene clewlow
Risc and cisc eugene clewlow
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Periodic table1
Periodic table1Periodic table1
Periodic table1
 
Maths project
Maths projectMaths project
Maths project
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lcd monitors
Lcd monitorsLcd monitors
Lcd monitors
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Helen keller-1226880485154369-8
Helen keller-1226880485154369-8Helen keller-1226880485154369-8
Helen keller-1226880485154369-8
 
Hardware
HardwareHardware
Hardware
 
Gsm cdma1
Gsm cdma1Gsm cdma1
Gsm cdma1
 
Final 121114041321-phpapp01
Final 121114041321-phpapp01Final 121114041321-phpapp01
Final 121114041321-phpapp01
 
Engh 140118084844-phpapp01
Engh 140118084844-phpapp01Engh 140118084844-phpapp01
Engh 140118084844-phpapp01
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 

Dernier (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Lab3

  • 1. Inheritance The mechanism by which one class can inherit the properties of another. It allows a hierarchy of classes to be built, moving from the most general to the most specific.
  • 2. Base Class, Derived Class Base Class Defines all qualities common to any derived classes. Derived Class Inherits those general properties and adds new properties that are specific to that class.
  • 3. Example: Base Class class base { int x; public: void setx(int n) { x = n; } void showx() { cout << x << ‘n’ } };
  • 4. Example: Derived Class // Inherit as public class derived : public base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘n’;} };
  • 5. Access Specifier: public The keyword public tells the compiler that base will be inherited such that: all public members of the base class will also be public members of derived. However, all private elements of base will remain private to it and are not directly accessible by derived.
  • 6. Example: main() int main() { derived ob; ob.setx(10); ob.sety(20); ob.showx(); ob.showy(); }
  • 7. An incorrect example class derived : public base { int y; public: void sety(int n) { y = n; } /* Error ! Cannot access x, which is private member of base. */ void show_sum() {cout << x+y; } };
  • 8. Access Specifier: private If the access specifier is private: public members of base become private members of derived. these members are still accessible by member functions of derived.
  • 9. Example: Derived Class // Inherit as private class derived : private base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘n’;} };
  • 10. Example: main() int main() { derived ob; ob.setx(10); // Error! setx() is private. ob.sety(20); // OK! ob.showx(); // Error! showx() is private. ob.showy(); // OK! }
  • 11. Example: Derived Class class derived : private base { int y; public: // setx is accessible from within derived void setxy(int n, int m) { setx(n); y = m; } // showx is also accessible void showxy() { showx(); cout<<y<< ‘n’;} };
  • 12. Protected Members Sometimes you want to do the following: keep a member of a base class private allow a derived class access to it Use protected members! If no derived class, protected members is the same as private members.
  • 13. Protected Members The full general form of a class declaration: class class-name { // private members protected: // protected members public: // public members };
  • 14. 3 Types of Access Specifiers Type 1: inherit as private Base Derived private members inaccessible protected members private members public members private members
  • 15. 3 Types of Access Specifiers Type 2: inherit as protected Base Derived private members inaccessible protected members protected members public members protected members
  • 16. 3 Types of Access Specifiers Type 3: inherit as public Base Derived private members inaccessible protected members protected members public members public members
  • 17. Constructor and Destructor It is possible for both the base class and the derived class to have constructor and/or destructor functions. The constructor functions are executed in order of derivation. i.e.the base class constructor is executed first. The destructor functions are executed in reverse order.
  • 18. Passing arguments What if the constructor functions of both the base class and derived class take arguments? 1. Pass all necessary arguments to the derived class’s constructor. 2. Then pass the appropriate arguments along to the base class.
  • 19. Example: Constructor of base class base { int i; public: base(int n) { cout << “constructing base n”; i = n; } ~base() { cout << “destructing base n”; } };
  • 20. Example: Constructor of derived class derived : public base { int j; public: derived (int n, int m) : base (m) { cout << “constructing derivedn”; j = n; } ~derived() { cout << “destructing derivedn”;} };
  • 21. Example: main() int main() { derived o(10,20); return 0; } constructing base constructing derived destructing derived destructing base
  • 22. Multiple Inheritance Type 1: base 1 derived 1 derived 2
  • 23. Multiple Inheritance • Type 2: base 1 base 2 derived
  • 24. Example: Type 2 // Create first base class class B1 { int a; public: B1(int x) { a = x; } int geta() { return a; } };
  • 25. Example: Type 2 // Create second base class class B2 { int b; public: B2(int x) { b = x; } int getb() { return b; } };
  • 26. // Directly inherit two base classes. class D : public B1, public B2 { int c; public: D(int x, int y, int z) : B1(z), B2(y) { c = x; } void show() { cout << geta() << getb() << c;} } ; Example: Type 2
  • 27. Potential Problem Base is inherited twice by Derived 3! Base Base Derived 1 Derived 2 Derived 3
  • 28. Virtual Base Class To resolve this problem, virtual base class can be used. class base { public: int i; };
  • 29. Virtual Base Class // Inherit base as virtual class D1 : virtual public base { public: int j; }; class D2 : virtual public base { public: int k; };
  • 30. Virtual Base Class /* Here, D3 inherits both D1 and D2. However, only one copy of base is present */ class D3 : public D1, public D2 { public: int product () { return i * j * k; } };
  • 31. Pointers to Derived Classes A pointer declared as a pointer to base class can also be used to point to any class derived from that base. However, only those members of the derived object that were inherited from the base can be accessed.
  • 32. Example base *p; // base class pointer base B_obj; derived D_obj; p = &B_obj; // p can point to base object p = &D_obj; // p can also point to derived // object
  • 33. Virtual Function A virtual function is a member function declared within a base class redefined by a derived class (i.e. overriding) It can be used to support run-time polymorphism.
  • 34. Example class base { public: int i; base (int x) { i = x; } virtual void func() {cout << i; } };
  • 35. Example class derived : public base { public: derived (int x) : base (x) {} // The keyword virtual is not needed. void func() {cout << i * i; } };
  • 36. Example int main() { base ob(10), *p; derived d_ob(10); p = &ob; p->func(); // use base’s func() p = &d_ob; p->func(); // use derived’s func() }
  • 37. Pure Virtual Functions A pure virtual function has no definition relative to the base class. Only the function’s prototype is included. General form: virtual type func-name(paremeter-list) = 0
  • 38. Example: area class area { public: double dim1, dim2; area(double x, double y) {dim1 = x; dim2 = y;} // pure virtual function virtual double getarea() = 0; };
  • 39. Example: rectangle class rectangle : public area { public: // function overriding double getarea() { return dim1 * dim2; } };
  • 40. Example: triangle class triangle : public area { public: // function overriding double getarea() { return 0.5 * dim1 * dim2; } };