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.4 Memberwise Assignment
•Can use = to assign one object to another, or to
initialize a new object with an object’s data
•Examples (assuming class V):
V v1, v2;
… // statements that assign
… // values to members of v1
v2 = v1; // assignment
V v3 = v2; // initialization
11-3
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-4
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-5
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.5 Copy Constructors 1 of 2
A copy constructor
•Is a special constructor used when a newly
created object is initialized with the data of
another object of same class
•The default copy constructor copies field-to-field,
using memberwise assignment
•The default copy constructor works fine in most
cases
11-6
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Copy Constructors 2 of 2
Problems occur when objects contain pointers to
dynamic storage:
class CpClass
{
private:
int *p;
public:
CpClass(int v=0)
{ p = new int; *p = v;}
~CpClass(){delete p;}
};
11-7
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Default Constructor Causes Sharing of
Storage
CpClass c1(5);
if (true)
{
CpClass c2=c1;
}
// c1 is corrupted
// when c2 goes
// out of scope and
// its destructor
// executes
11-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Problems of Sharing Dynamic Storage
•Destructor of one object deletes memory still in
use by other objects
•Modification of memory by one object affects
other objects sharing that memory
11-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Programmer-Defined Copy Constructors
1 of 2
•A copy constructor is one that takes a reference
parameter to another object of the same class
•The copy constructor uses the data in the object
passed as parameter to initialize the object
being created
•The reference parameter should be const to
avoid potential for data corruption
11-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Programmer-Defined Copy Constructors
2 of 2
•The copy constructor avoids problems caused by
memory sharing
•It can allocate separate memory to hold new
object’s dynamic member data
•It can make the new object’s pointer point to the
new object’s own dynamic memory
•It copies the data, not the pointer, from the
original object to the new object
11-11
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Copy Constructor Example
class CpClass
{
int *p;
public:
CpClass(const CpClass &obj)
{ p = new int; *p = *obj.p; }
CpClass(int v=0)
{ p = new int; *p = v; }
~CpClass(){delete p;}
};
11-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-
13
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-
14
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Copy Constructor – When Is It Used?
A copy constructor is called when
•An object is initialized from an object of the same
class
•An object is passed by value to a function
•An object is returned using a return statement
from a function
11-15

More Related Content

PPTX
load balancing in public cloud ppt
PPT
Chapter11.ppt
PPT
Operator overloading
PPTX
C++ Chapter 11 OOP - Part 5
PPTX
C++ Chapter 11 OOP - Part 2
PPT
5 Constructors and Destructors
PPTX
C++ Chapter 11 OOP - Part 4
PPTX
C++ Chapter 11 OOP - Part 7
load balancing in public cloud ppt
Chapter11.ppt
Operator overloading
C++ Chapter 11 OOP - Part 5
C++ Chapter 11 OOP - Part 2
5 Constructors and Destructors
C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 7

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

PPTX
C++ Chapter 11 OOP - Part 6
PDF
Unit 2 Methods and Polymorphism-Object oriented programming
PDF
Assign
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PPTX
Constructors and destructors
PPT
Constructor and destructor in C++
PPTX
2CPP13 - Operator Overloading
PPT
Cast and copy constructor Object Oriented Programming
PPT
Constructors and destructors in C++ part 2
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPT
ConsTRUCTION AND DESTRUCTION
PPTX
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
PDF
CS225_Prelecture_Notes 2nd
PPTX
OOP.pptx
PPT
106da session5 c++
PPTX
CSCI 238 Chapter 07 - Classes
PPT
data Structure Lecture 1
PPT
constructor-.pptcucfkifkficuvguvucufjfugugigig
PPTX
Class and object
PPTX
Object orinted programming lecture| Overlaoded Functions
C++ Chapter 11 OOP - Part 6
Unit 2 Methods and Polymorphism-Object oriented programming
Assign
constructocvbcvbcvbcvbr-Destructor (1).pptx
Constructors and destructors
Constructor and destructor in C++
2CPP13 - Operator Overloading
Cast and copy constructor Object Oriented Programming
Constructors and destructors in C++ part 2
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
ConsTRUCTION AND DESTRUCTION
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
CS225_Prelecture_Notes 2nd
OOP.pptx
106da session5 c++
CSCI 238 Chapter 07 - Classes
data Structure Lecture 1
constructor-.pptcucfkifkficuvguvucufjfugugigig
Class and object
Object orinted programming lecture| Overlaoded Functions
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)

PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
1_English_Language_Set_2.pdf probationary
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
Classroom Observation Tools for Teachers
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
A systematic review of self-coping strategies used by university students to ...
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
Weekly quiz Compilation Jan -July 25.pdf
1_English_Language_Set_2.pdf probationary
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Hazard Identification & Risk Assessment .pdf
History, Philosophy and sociology of education (1).pptx
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
LDMMIA Reiki Yoga Finals Review Spring Summer
Digestion and Absorption of Carbohydrates, Proteina and Fats
Classroom Observation Tools for Teachers
Paper A Mock Exam 9_ Attempt review.pdf.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chinmaya Tiranga quiz Grand Finale.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Unit 4 Skeletal System.ppt.pptxopresentatiom
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
A systematic review of self-coping strategies used by university students to ...

C++ Chapter 11 OOP - Part 3

  • 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.4 Memberwise Assignment •Can use = to assign one object to another, or to initialize a new object with an object’s data •Examples (assuming class V): V v1, v2; … // statements that assign … // values to members of v1 v2 = v1; // assignment V v3 = v2; // initialization 11-3
  • 4. 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-4 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
  • 5. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1-5
  • 6. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.5 Copy Constructors 1 of 2 A copy constructor •Is a special constructor used when a newly created object is initialized with the data of another object of same class •The default copy constructor copies field-to-field, using memberwise assignment •The default copy constructor works fine in most cases 11-6
  • 7. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Copy Constructors 2 of 2 Problems occur when objects contain pointers to dynamic storage: class CpClass { private: int *p; public: CpClass(int v=0) { p = new int; *p = v;} ~CpClass(){delete p;} }; 11-7
  • 8. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Default Constructor Causes Sharing of Storage CpClass c1(5); if (true) { CpClass c2=c1; } // c1 is corrupted // when c2 goes // out of scope and // its destructor // executes 11-8
  • 9. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Problems of Sharing Dynamic Storage •Destructor of one object deletes memory still in use by other objects •Modification of memory by one object affects other objects sharing that memory 11-9
  • 10. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Programmer-Defined Copy Constructors 1 of 2 •A copy constructor is one that takes a reference parameter to another object of the same class •The copy constructor uses the data in the object passed as parameter to initialize the object being created •The reference parameter should be const to avoid potential for data corruption 11-10
  • 11. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Programmer-Defined Copy Constructors 2 of 2 •The copy constructor avoids problems caused by memory sharing •It can allocate separate memory to hold new object’s dynamic member data •It can make the new object’s pointer point to the new object’s own dynamic memory •It copies the data, not the pointer, from the original object to the new object 11-11
  • 12. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Copy Constructor Example class CpClass { int *p; public: CpClass(const CpClass &obj) { p = new int; *p = *obj.p; } CpClass(int v=0) { p = new int; *p = v; } ~CpClass(){delete p;} }; 11-12
  • 13. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1- 13
  • 14. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1- 14
  • 15. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Copy Constructor – When Is It Used? A copy constructor is called when •An object is initialized from an object of the same class •An object is passed by value to a function •An object is returned using a return statement from a function 11-15