SlideShare a Scribd company logo
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

DOC
Pads lab manual final
PPTX
Inheritance
PDF
C++ manual Report Full
PPTX
New presentation oop
DOCX
Exam for c
PPTX
#OOP_D_ITS - 3rd - Pointer And References
Pads lab manual final
Inheritance
C++ manual Report Full
New presentation oop
Exam for c
#OOP_D_ITS - 3rd - Pointer And References

What's hot (20)

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

Similar to 201801 CSE240 Lecture 14 (20)

PPTX
OOPS Basics With Example
PPTX
Inheritance_PART2.pptx
PDF
201801 CSE240 Lecture 13
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
Inheritance.pptx
PPT
Inheritance in C++.ppt
PPTX
OOP unit II inheritance.pptx object oriented programming
PDF
Chapter27 polymorphism-virtual-function-abstract-class
PPT
10 inheritance
PPT
Basics of objective c
PDF
c++-language-1208539706757125-9.pdf
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PDF
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
PPTX
05 Object Oriented Concept Presentation.pptx
PPTX
OOPS IN C++
PPSX
Object oriented programming 2
PPTX
[OOP - Lec 20,21] Inheritance
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
Inheritance
OOPS Basics With Example
Inheritance_PART2.pptx
201801 CSE240 Lecture 13
Chapter 6 and inheritance OOP C++ tu ioe
Inheritance.pptx
Inheritance in C++.ppt
OOP unit II inheritance.pptx object oriented programming
Chapter27 polymorphism-virtual-function-abstract-class
10 inheritance
Basics of objective c
c++-language-1208539706757125-9.pdf
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
05 Object Oriented Concept Presentation.pptx
OOPS IN C++
Object oriented programming 2
[OOP - Lec 20,21] Inheritance
lecture-2021inheritance-160705095417.pdf
Inheritance
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
PDF
201801 CSE240 Lecture 10
PDF
201801 CSE240 Lecture 09
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11
201801 CSE240 Lecture 10
201801 CSE240 Lecture 09

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Nekopoi APK 2025 free lastest update
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
ai tools demonstartion for schools and inter college
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms II-SECS-1021-03
System and Network Administraation Chapter 3
Upgrade and Innovation Strategies for SAP ERP Customers
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
How to Migrate SBCGlobal Email to Yahoo Easily
Nekopoi APK 2025 free lastest update
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Essential Infomation Tech presentation.pptx
Reimagine Home Health with the Power of Agentic AI​
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
ai tools demonstartion for schools and inter college
How to Choose the Right IT Partner for Your Business in Malaysia
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

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.