SlideShare a Scribd company logo
1 of 21
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
OUTLINE
11.1 The this Pointer and
Constant Member
Functions
11.2 Static Members
11.3 Friends of Classes
11.4 Memberwise
Assignment
11.5 Copy Constructors
11.6 Operator Overloading
11.7 Rvalue References and
Move Operations
11-1
11.8 Type Conversion
Operators
11.9 Convert Constructors
11.10 Aggregation and
Composition
11.11 Inheritance
11.12 Protected Members and
Class Access
11.13 Constructors,
Destructors, and Inheritance
11.14 Overriding Base Class
Functions
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.12 Protected Members and Class Access
•protected member access specification: A class
member labeled protected is accessible to
member functions of derived classes as well as to
member functions of the same class
•Similar to private, except accessible to
members functions of derived classes
11-3
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Base Class Access Specification
Base class access specification determines how
private, protected, and public members of
base class can be accessed by derived classes
11-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Base Class Access
C++ supports three inheritance modes, also called
base class access modes:
- public inheritance
class Child : public Parent { };
- protected inheritance
class Child : protected Parent{ };
- private inheritance
class Child : private Parent{ };
11-5
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Base Class Access vs. Member Access
Specification
Base class access is not the same as
member access specification:
– Base class access: determine access for
inherited members
– Member access specification: determine
access for members defined in the class
11-6
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Member Access Specification
Specified using the keywords
private, protected, public
class MyClass
{
private: int a;
protected: int b; void fun();
public: void fun2();
};
11-7
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Base Class And Member Access
Specification
class Child : public Parent // base access
{
protected: // member access
int a;
public: // member access
Child();
};
11-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Base Class Access Specifiers
1) public – object of derived class can be
treated as object of base class (not vice-
versa)
2) protected – more restrictive than public,
but allows derived classes to know some of
the details of parents
3) private – prevents objects of derived class
from being treated as objects of base class.
11-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Effect of Base Access
11-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
OUTLINE
11.1 The this Pointer and
Constant Member
Functions
11.2 Static Members
11.3 Friends of Classes
11.4 Memberwise
Assignment
11.5 Copy Constructors
11.6 Operator Overloading
11.7 Rvalue References and
Move Operations
11-11
11.8 Type Conversion
Operators
11.9 Convert Constructors
11.10 Aggregation and
Composition
11.11 Inheritance
11.12 Protected Members and
Class Access
11.13 Constructors,
Destructors, and Inheritance
11.14 Overriding Base Class
Functions
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-
12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.13 Constructors, Destructors and
Inheritance
•By inheriting every member of the base class, a
derived class object contains a base class object
•The derived class constructor can specify which
base class constructor should be used to initialize
the base class object
11-13
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Order of Execution 1 of 2
•When an object of a derived class is created, the
base class’s constructor is executed first,
followed by the derived class’s constructor
•When an object of a derived class is destroyed,
its destructor is called first, then that of the base
class
11-14
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Order of Execution 2 of 2
// Student – base class
// UnderGrad – derived class
// Both have constructors, destructors
int main()
{
UnderGrad u1; // execute Student constructor,
// then execute UnderGrad constructor
...
return 0; // execute UnderGrad destructor, then
// execute Student destructor
}// end main
11-15
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Passing Arguments to Base Class
Constructor 1 of 2
•This allows selection between multiple base class
constructors
•You can specify arguments to a base constructor
on the derived constructor heading
•Can also be done with inline constructors
•This must be done if the base class has no default
constructor
11-16
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Passing Arguments to Base Class
Constructor 2 of 2
class Parent {
int x, y;
public: Parent(int,int);
};
class Child : public Parent {
int z
public:
Child(int a): Parent(a,a*a)
{z = a;}
};
11-17
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
OUTLINE
11.1 The this Pointer and
Constant Member
Functions
11.2 Static Members
11.3 Friends of Classes
11.4 Memberwise
Assignment
11.5 Copy Constructors
11.6 Operator Overloading
11.7 Rvalue References and
Move Operations
11-18
11.8 Type Conversion
Operators
11.9 Convert Constructors
11.10 Aggregation and
Composition
11.11 Inheritance
11.12 Protected Members and
Class Access
11.13 Constructors,
Destructors, and Inheritance
11.14 Overriding Base Class
Functions
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-
19
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.14 Overriding Base Class Functions
•Overriding: a function in a derived class that has
the same name and parameter list as a function in
the base class
•Typically used to replace a function in base class
with different actions in derived class
•This is not the same as overloading. With
overloading, the parameter lists must be different
11-20
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Access to Overridden Function
•When a function is overridden, all objects of a
derived class use the overriding function.
•If it is necessary to access the overridden version
of the function, it can be done using the scope
resolution operator with the name of the base
class and the name of the function:
Student::getName();
11-21

More Related Content

Similar to C++ Chapter 11 OOP - Part 7

Similar to C++ Chapter 11 OOP - Part 7 (20)

Ch7Structs.pptx
Ch7Structs.pptxCh7Structs.pptx
Ch7Structs.pptx
 
Microservices Primer for Monolithic Devs
Microservices Primer for Monolithic DevsMicroservices Primer for Monolithic Devs
Microservices Primer for Monolithic Devs
 
Lecture 08-inheritance-i
Lecture 08-inheritance-iLecture 08-inheritance-i
Lecture 08-inheritance-i
 
Spring andspringboot training
Spring andspringboot trainingSpring andspringboot training
Spring andspringboot training
 
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
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)
 
An Overview of Entity Framework
An Overview of Entity FrameworkAn Overview of Entity Framework
An Overview of Entity Framework
 
React Interview Questions and Answers | React Tutorial | React Redux Online T...
React Interview Questions and Answers | React Tutorial | React Redux Online T...React Interview Questions and Answers | React Tutorial | React Redux Online T...
React Interview Questions and Answers | React Tutorial | React Redux Online T...
 
Chapter11.ppt
Chapter11.pptChapter11.ppt
Chapter11.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Core JAVA Training.ppt
Core JAVA Training.pptCore JAVA Training.ppt
Core JAVA Training.ppt
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Intro to OOP PHP and Github
Intro to OOP PHP and GithubIntro to OOP PHP and Github
Intro to OOP PHP and Github
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Object-oriented design: Inheritance subtyping (vs. Composition)
Object-oriented design: Inheritance subtyping (vs. Composition)Object-oriented design: Inheritance subtyping (vs. Composition)
Object-oriented design: Inheritance subtyping (vs. Composition)
 
oop Lecture 11
oop Lecture 11oop Lecture 11
oop Lecture 11
 

More from DanWooster1

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
DanWooster1
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
DanWooster1
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
DanWooster1
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
DanWooster1
 

More from DanWooster1 (20)

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1
 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3
 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2
 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1
 
Upstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - ArraysUpstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - Arrays
 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
 

Recently uploaded

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

C++ Chapter 11 OOP - Part 7

  • 1. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved OUTLINE 11.1 The this Pointer and Constant Member Functions 11.2 Static Members 11.3 Friends of Classes 11.4 Memberwise Assignment 11.5 Copy Constructors 11.6 Operator Overloading 11.7 Rvalue References and Move Operations 11-1 11.8 Type Conversion Operators 11.9 Convert Constructors 11.10 Aggregation and Composition 11.11 Inheritance 11.12 Protected Members and Class Access 11.13 Constructors, Destructors, and Inheritance 11.14 Overriding Base Class Functions
  • 2. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1-2
  • 3. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.12 Protected Members and Class Access •protected member access specification: A class member labeled protected is accessible to member functions of derived classes as well as to member functions of the same class •Similar to private, except accessible to members functions of derived classes 11-3
  • 4. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Base Class Access Specification Base class access specification determines how private, protected, and public members of base class can be accessed by derived classes 11-4
  • 5. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Base Class Access C++ supports three inheritance modes, also called base class access modes: - public inheritance class Child : public Parent { }; - protected inheritance class Child : protected Parent{ }; - private inheritance class Child : private Parent{ }; 11-5
  • 6. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Base Class Access vs. Member Access Specification Base class access is not the same as member access specification: – Base class access: determine access for inherited members – Member access specification: determine access for members defined in the class 11-6
  • 7. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Member Access Specification Specified using the keywords private, protected, public class MyClass { private: int a; protected: int b; void fun(); public: void fun2(); }; 11-7
  • 8. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Base Class And Member Access Specification class Child : public Parent // base access { protected: // member access int a; public: // member access Child(); }; 11-8
  • 9. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Base Class Access Specifiers 1) public – object of derived class can be treated as object of base class (not vice- versa) 2) protected – more restrictive than public, but allows derived classes to know some of the details of parents 3) private – prevents objects of derived class from being treated as objects of base class. 11-9
  • 10. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Effect of Base Access 11-10
  • 11. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved OUTLINE 11.1 The this Pointer and Constant Member Functions 11.2 Static Members 11.3 Friends of Classes 11.4 Memberwise Assignment 11.5 Copy Constructors 11.6 Operator Overloading 11.7 Rvalue References and Move Operations 11-11 11.8 Type Conversion Operators 11.9 Convert Constructors 11.10 Aggregation and Composition 11.11 Inheritance 11.12 Protected Members and Class Access 11.13 Constructors, Destructors, and Inheritance 11.14 Overriding Base Class Functions
  • 12. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1- 12
  • 13. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.13 Constructors, Destructors and Inheritance •By inheriting every member of the base class, a derived class object contains a base class object •The derived class constructor can specify which base class constructor should be used to initialize the base class object 11-13
  • 14. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Order of Execution 1 of 2 •When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor •When an object of a derived class is destroyed, its destructor is called first, then that of the base class 11-14
  • 15. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Order of Execution 2 of 2 // Student – base class // UnderGrad – derived class // Both have constructors, destructors int main() { UnderGrad u1; // execute Student constructor, // then execute UnderGrad constructor ... return 0; // execute UnderGrad destructor, then // execute Student destructor }// end main 11-15
  • 16. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Passing Arguments to Base Class Constructor 1 of 2 •This allows selection between multiple base class constructors •You can specify arguments to a base constructor on the derived constructor heading •Can also be done with inline constructors •This must be done if the base class has no default constructor 11-16
  • 17. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Passing Arguments to Base Class Constructor 2 of 2 class Parent { int x, y; public: Parent(int,int); }; class Child : public Parent { int z public: Child(int a): Parent(a,a*a) {z = a;} }; 11-17
  • 18. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved OUTLINE 11.1 The this Pointer and Constant Member Functions 11.2 Static Members 11.3 Friends of Classes 11.4 Memberwise Assignment 11.5 Copy Constructors 11.6 Operator Overloading 11.7 Rvalue References and Move Operations 11-18 11.8 Type Conversion Operators 11.9 Convert Constructors 11.10 Aggregation and Composition 11.11 Inheritance 11.12 Protected Members and Class Access 11.13 Constructors, Destructors, and Inheritance 11.14 Overriding Base Class Functions
  • 19. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1- 19
  • 20. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.14 Overriding Base Class Functions •Overriding: a function in a derived class that has the same name and parameter list as a function in the base class •Typically used to replace a function in base class with different actions in derived class •This is not the same as overloading. With overloading, the parameter lists must be different 11-20
  • 21. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Access to Overridden Function •When a function is overridden, all objects of a derived class use the overriding function. •If it is necessary to access the overridden version of the function, it can be done using the scope resolution operator with the name of the base class and the name of the function: Student::getName(); 11-21