SlideShare a Scribd company logo
1 of 10
CSE240 – Introduction to
Programming Languages
Lecture 14:
Programming with C++ | Inheritance
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Inheritance
• Can be used for defining new classes by extending existing classes
• Java: parent (super) class and child (sub) class
• C++: base class and derived class
• New class inherits existing members (variables and functions) and
may add members or redefine members
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Example
#include <iostream>
using namespace std;
class Base {
public:
Base(int n) {
cout << "Base constructor"<<endl;
}
void function() {
cout << "fuction"<<endl;
}
~Base() {
cout << "Base destructor"<<endl;
}
};
class Derived : public Base {
public:
// constructor calls constructor base
Derived(int n) : Base(n) {
cout << "Derived constructor"<<endl;
}
~Derived() {
cout << "Derived destructor"<<endl;
}
};
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Example
#include <iostream>
using namespace std;
class Base {
public:
Base(int n) {
cout << "Base constructor"<<endl;
}
void function() {
cout << "fuction"<<endl;
}
~Base() {
cout << "Base destructor"<<endl;
}
};
class Derived : public Base {
public:
// constructor calls constructor base
Derived(int n) : Base(n) {
cout << "Derived constructor"<<endl;
}
~Derived() {
cout << "Derived destructor"<<endl;
}
};
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Example
int main() {
Derived myPQ1(50);
myPQ1.function(); // inherited
myPQ1.function(); // inherited
Derived *myPQ2;
myPQ2 = new Derived(50);
myPQ2->function(); // inherited
myPQ2->function(); // inherited
delete myPQ2;
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Multiple Inheritance
§ A class can inherit members from more than one class;
§ The semantics of multiple inheritances is complex and error prone. It must be
used with caution.
§ The diamond problem
name and age
needed only once
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Example
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A's constructor called" << endl; }
};
class B {
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A { // Note the order
public:
C() { cout << "C's constructor called" << endl; }
};
int main() {
C c;
return 0;
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Polymorphism
A pointer to a derived class is type-compatible with a pointer to its base class.
// pointers to base class
#include <iostream>
using namespace std;
class Figure {
protected:
int width, height;
public:
void set_values (int a, int b) {width=a; height=b;}
int area () { return 0; }
};
class Rectangle: public Figure {
public:
int area() { return width*height;}
};
class Triangle: public Figure {
public:
int area() { return width*height/2; }
};
int main () {
Rectangle rectangle;
Triangle triangle;
Figure * f1 = &rectangle;
Figure * f2 = &triangle;
f1->set_values (10,20);
f2->set_values (30,40);
cout << rectangle.area() << "n";
cout << triangle.area() << "n";
cout << f1->area() << 'n';
cout << f2->area() << 'n';
return 0;
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Polymorphism
A virtual member is a member function for which dynamic dispatch is facilitated.
// pointers to base class
#include <iostream>
using namespace std;
class Figure {
protected:
int width, height;
public:
void set_values (int a, int b) {width=a; height=b;}
virtual int area () { return 0; }
};
class Rectangle: public Figure {
public:
int area() { return width*height;}
};
class Triangle: public Figure {
public:
int area() { return width*height/2; }
};
int main () {
Rectangle rectangle;
Triangle triangle;
Figure * f1 = &rectangle;
Figure * f2 = &triangle;
f1->set_values (10,20);
f2->set_values (30,40);
cout << rectangle.area() << "n";
cout << triangle.area() << "n";
cout << f1->area() << 'n';
cout << f2->area() << 'n';
return 0;
}
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

C programming pointer
C  programming pointerC  programming pointer
C programming pointerargusacademy
 
Type-level programming
Type-level programmingType-level programming
Type-level programmingDmytro Mitin
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Dr. Loganathan R
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,pptAllNewTeach
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Dr. Loganathan R
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileHarjinder Singh
 
Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Swarup Boro
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Dr. Loganathan R
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILEAnushka Rai
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesSubhajit Sahu
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQLvikram mahendra
 
Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programmingssuser6fc8b1
 
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 

What's hot (20)

Pointers
PointersPointers
Pointers
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Type-level programming
Type-level programmingType-level programming
Type-level programming
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
week-3x
week-3xweek-3x
week-3x
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
.NET/C#_6
.NET/C#_6.NET/C#_6
.NET/C#_6
 
Compiling fµn language
Compiling fµn languageCompiling fµn language
Compiling fµn language
 
Practical no 2
Practical no 2Practical no 2
Practical no 2
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
 
Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programming
 
Practical no 3
Practical no 3Practical no 3
Practical no 3
 
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
 

Similar to 201801 CSE240 Lecture 14

Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptxArjunArora78
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.pptinstaface
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfnisarmca
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxDeepasCSE
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2Aadil Ansari
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdfAneesAbbasi14
 
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
 

Similar to 201801 CSE240 Lecture 14 (20)

OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
 
Basics of objective c
Basics of objective cBasics of objective c
Basics of objective c
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdf
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
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
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
Lecture4
Lecture4Lecture4
Lecture4
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 
201801 CSE240 Lecture 08
201801 CSE240 Lecture 08201801 CSE240 Lecture 08
201801 CSE240 Lecture 08
 

Recently uploaded

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 

Recently uploaded (20)

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 

201801 CSE240 Lecture 14

  • 1. CSE240 – Introduction to Programming Languages Lecture 14: Programming with C++ | Inheritance Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Inheritance • Can be used for defining new classes by extending existing classes • Java: parent (super) class and child (sub) class • C++: base class and derived class • New class inherits existing members (variables and functions) and may add members or redefine members
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Example #include <iostream> using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<<endl; } void function() { cout << "fuction"<<endl; } ~Base() { cout << "Base destructor"<<endl; } }; class Derived : public Base { public: // constructor calls constructor base Derived(int n) : Base(n) { cout << "Derived constructor"<<endl; } ~Derived() { cout << "Derived destructor"<<endl; } };
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Example #include <iostream> using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<<endl; } void function() { cout << "fuction"<<endl; } ~Base() { cout << "Base destructor"<<endl; } }; class Derived : public Base { public: // constructor calls constructor base Derived(int n) : Base(n) { cout << "Derived constructor"<<endl; } ~Derived() { cout << "Derived destructor"<<endl; } };
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Example int main() { Derived myPQ1(50); myPQ1.function(); // inherited myPQ1.function(); // inherited Derived *myPQ2; myPQ2 = new Derived(50); myPQ2->function(); // inherited myPQ2->function(); // inherited delete myPQ2; }
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Multiple Inheritance § A class can inherit members from more than one class; § The semantics of multiple inheritances is complex and error prone. It must be used with caution. § The diamond problem name and age needed only once
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 Example #include <iostream> using namespace std; class A { public: A() { cout << "A's constructor called" << endl; } }; class B { public: B() { cout << "B's constructor called" << endl; } }; class C: public B, public A { // Note the order public: C() { cout << "C's constructor called" << endl; } }; int main() { C c; return 0; }
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 Polymorphism A pointer to a derived class is type-compatible with a pointer to its base class. // pointers to base class #include <iostream> using namespace std; class Figure { protected: int width, height; public: void set_values (int a, int b) {width=a; height=b;} int area () { return 0; } }; class Rectangle: public Figure { public: int area() { return width*height;} }; class Triangle: public Figure { public: int area() { return width*height/2; } }; int main () { Rectangle rectangle; Triangle triangle; Figure * f1 = &rectangle; Figure * f2 = &triangle; f1->set_values (10,20); f2->set_values (30,40); cout << rectangle.area() << "n"; cout << triangle.area() << "n"; cout << f1->area() << 'n'; cout << f2->area() << 'n'; return 0; }
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Polymorphism A virtual member is a member function for which dynamic dispatch is facilitated. // pointers to base class #include <iostream> using namespace std; class Figure { protected: int width, height; public: void set_values (int a, int b) {width=a; height=b;} virtual int area () { return 0; } }; class Rectangle: public Figure { public: int area() { return width*height;} }; class Triangle: public Figure { public: int area() { return width*height/2; } }; int main () { Rectangle rectangle; Triangle triangle; Figure * f1 = &rectangle; Figure * f2 = &triangle; f1->set_values (10,20); f2->set_values (30,40); cout << rectangle.area() << "n"; cout << triangle.area() << "n"; cout << f1->area() << 'n'; cout << f2->area() << 'n'; return 0; }
  • 10. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.