SlideShare a Scribd company logo
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.8 Type Conversion Operators
•Conversion Operators are member functions
that tell the compiler how to convert an object
of the class type to a value of another type
•The conversion information provided by the
conversion operators is automatically used by
the compiler in assignments, initializations, and
parameter passing
11-3
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Syntax of Conversion Operators
•Conversion operator must be a member function of
the class you are converting from
•The name of the operator is the name of the type
you are converting to
•The operator does not specify a return type
11-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-5
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Conversion Operator Example
•To convert from a class IntVal to an integer:
class IntVal
{
int x;
public:
IntVal(int a = 0){x = a;}
operator int(){return x;}
};
•Automatic conversion during assignment:
IntVal obj(15); int i;
i = obj; cout << i; // prints 15
11-6
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-7
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-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.9 Convert Constructors
Convert constructors are constructors that take
a single parameter of a type other than the
class in which they are defined
class CCClass
{ int x;
public:
CCClass() //default
CCClass(int a, int b);
CCClass(int a); //convert
CCClass(string s); //convert
};
11-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Example of a Convert Constructor
The C++ string class has a convert
constructor that converts from C-strings:
class string
{
public:
string(char *); //convert
…
};
11-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Uses of Convert Constructors 1 of 2
•They are automatically invoked by the compiler to
create an object from the value passed as
parameter:
string s("hello"); //convert C-string
CCClass obj(24); //convert int
•The compiler allows convert constructors to be
invoked with assignment-like notation:
string s = "hello"; //convert C-string
CCClass obj = 24; //convert int
11-11
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Uses of Convert Constructors 2 of 2
•Convert constructors allow functions that take the
class type as parameter to take parameters of other
types:
void myFun(string s); // needs string
// object
myFun("hello"); // accepts C-string
void myFun(CCClass c);
myFun(34); // accepts int
11-12

More Related Content

PDF
Enterprise java unit-1_chapter-1
PPTX
C++ Chapter 11 OOP - Part 4
PPTX
C++ Chapter 11 OOP - Part 3
PPT
Chapter11.ppt
PPTX
C++ Chapter 11 OOP - Part 7
PPTX
C++ Chapter 11 OOP - Part 2
PPTX
oprators in cpp,types with example and details.pptx
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Enterprise java unit-1_chapter-1
C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 3
Chapter11.ppt
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 2
oprators in cpp,types with example and details.pptx
Basics _of_Operator Overloading_Somesh_Kumar_SSTC

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

PDF
Ch-4-Operator Overloading.pdf
PPT
Operator overloading
PPT
Savitch Ch 11
PPT
Savitch ch 11
PPT
C++ - Constructors,Destructors, Operator overloading and Type conversion
PDF
Operator overloading
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
PDF
C++ L02-Conversion+enum+Operators
PPTX
Operator overloaing
PPTX
Operator overloading
PPTX
9781337102087 ppt ch13
PPTX
C++ Chapter 11 OOP - Part 6
PPT
Type conversions
PPT
OOP in C++
PPT
Unit ii
PPTX
3. Polymorphism.pptx
PPTX
Type conversion
PPTX
13.Data Conversion.pptx
PPTX
C++ Object Oriented Programming
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
Ch-4-Operator Overloading.pdf
Operator overloading
Savitch Ch 11
Savitch ch 11
C++ - Constructors,Destructors, Operator overloading and Type conversion
Operator overloading
Operator_Overloaing_Type_Conversion_OOPC(C++)
C++ L02-Conversion+enum+Operators
Operator overloaing
Operator overloading
9781337102087 ppt ch13
C++ Chapter 11 OOP - Part 6
Type conversions
OOP in C++
Unit ii
3. Polymorphism.pptx
Type conversion
13.Data Conversion.pptx
C++ Object Oriented Programming
21CSC101T best ppt ever OODP UNIT-2.pptx
Ad

More from DanWooster1 (20)

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

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Trump Administration's workforce development strategy
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
human mycosis Human fungal infections are called human mycosis..pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Yogi Goddess Pres Conference Studio Updates
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Trump Administration's workforce development strategy
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Cell Structure & Organelles in detailed.

C++ Chapter 11 OOP - Part 5

  • 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.8 Type Conversion Operators •Conversion Operators are member functions that tell the compiler how to convert an object of the class type to a value of another type •The conversion information provided by the conversion operators is automatically used by the compiler in assignments, initializations, and parameter passing 11-3
  • 4. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Syntax of Conversion Operators •Conversion operator must be a member function of the class you are converting from •The name of the operator is the name of the type you are converting to •The operator does not specify a return type 11-4
  • 5. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1-5
  • 6. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Conversion Operator Example •To convert from a class IntVal to an integer: class IntVal { int x; public: IntVal(int a = 0){x = a;} operator int(){return x;} }; •Automatic conversion during assignment: IntVal obj(15); int i; i = obj; cout << i; // prints 15 11-6
  • 7. 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-7 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
  • 8. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1-8
  • 9. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.9 Convert Constructors Convert constructors are constructors that take a single parameter of a type other than the class in which they are defined class CCClass { int x; public: CCClass() //default CCClass(int a, int b); CCClass(int a); //convert CCClass(string s); //convert }; 11-9
  • 10. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Example of a Convert Constructor The C++ string class has a convert constructor that converts from C-strings: class string { public: string(char *); //convert … }; 11-10
  • 11. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Uses of Convert Constructors 1 of 2 •They are automatically invoked by the compiler to create an object from the value passed as parameter: string s("hello"); //convert C-string CCClass obj(24); //convert int •The compiler allows convert constructors to be invoked with assignment-like notation: string s = "hello"; //convert C-string CCClass obj = 24; //convert int 11-11
  • 12. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Uses of Convert Constructors 2 of 2 •Convert constructors allow functions that take the class type as parameter to take parameters of other types: void myFun(string s); // needs string // object myFun("hello"); // accepts C-string void myFun(CCClass c); myFun(34); // accepts int 11-12