SlideShare une entreprise Scribd logo
1  sur  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.1 The this Pointer and Constant
Member Functions
•this pointer:
- Implicit parameter passed to every member
function
- it points to the object calling the function
•const member function:
- does not modify its calling object
11-3
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Using the this Pointer
Can be used to access members that may be
hidden by parameters with the same name:
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
};
11-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Constant Member Functions
•Declared with keyword const
•When const appears in the parameter list,
int setNum (const int num)
the function is prevented from modifying the
parameter. The parameter is read-only.
•When const follows the parameter list,
int getX()const
the function is prevented from modifying the
object.
11-5
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-6
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-7
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.2 Static Members
•Static member variable:
–There is one instance of the variable for the
entire class
–It is shared by all objects of the class
•Static member function:
–Can be used to access static member
variables
–Can be called before any class objects are
created
11-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Static Member Variables 1 of 3
1) Must be declared in the class with the keyword
static:
class IntVal
{
public:
intVal(int val = 0)
{ value = val; valCount++ }
int getVal();
void setVal(int);
private:
int value;
static int valCount;
};
11-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Static Member Variables 2 of 3
2) Must be defined outside of the class:
class IntVal
{
//In-class declaration
static int valCount;
//Other members not shown
};
//Definition outside of class
int IntVal::valCount = 0;
11-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Static Member Variables 3 of 3
3) Can be accessed or modified by any object of
the class: Modifications by one object are
visible to all objects of the class:
IntVal val1, val2;
11-11
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Static Member Functions 1 of 2
1)Declared with static before the return type:
class IntVal
{ public:
static int getValCount()
{ return valCount; }
private:
int value;
static int valCount;
};
11-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Static Member Functions 2 of 2
2) Can be called independently of any class
objects, through the class name:
cout << IntVal::getValCount();
3) Because of item 2 above, the this pointer
cannot be used
4) Can be called before any objects of the
class have been created
5) Used primarily to manipulate static
member variables of the class
11-13
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-
14
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-15
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-
16
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.3 Friends of Classes
•Friend function: a function that is not a member
of a class, but has access to private members of
the class
•A friend function can be a stand-alone function or
a member function of another class
•It is declared a friend of a class with the friend
keyword in the function prototype
11-17
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Friend Function Declarations 1 of 2
1) Friend function may be a stand-alone function:
class aClass
{
private:
int x;
friend void fSet(aClass &c, int a);
};
void fSet(aClass &c, int a)
{
c.x = a;
}
11-18
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Friend Function Declarations 2 of 2
2) A friend function may be a member of another
class:
class aClass
{ private:
int x;
friend void OtherClass::fSet
(aClass &c, int a);
};
class OtherClass
{ public:
void fSet(aClass &c, int a)
{ c.x = a; }
};
11-19
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Friend Class Declaration 1 of 2
3) An entire class can be declared a friend of a
class:
class aClass
{private:
int x;
friend class frClass;
};
class frClass
{public:
void fSet(aClass &c,int a){c.x = a;}
int fGet(aClass c){return c.x;}
};
11-20
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Friend Class Declaration 2 of 2
•If frClass is a friend of aClass, then all
member functions of frClass have unrestricted
access to all members of aClass, including the
private members.
•In general, you should restrict the property of
Friendship to only those functions that must have
access to the private members of a class.
11-21

Contenu connexe

Tendances

Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2
DanWooster1
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
Srinivas Reddy
 

Tendances (20)

Java interface
Java interfaceJava interface
Java interface
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object oriented programming 3 object oriented concepts
Object oriented programming 3 object oriented conceptsObject oriented programming 3 object oriented concepts
Object oriented programming 3 object oriented concepts
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
 
Interface
InterfaceInterface
Interface
 
Data classes in kotlin by Naveed
Data classes in kotlin by NaveedData classes in kotlin by Naveed
Data classes in kotlin by Naveed
 
Packages and Interfaces
Packages and InterfacesPackages and Interfaces
Packages and Interfaces
 
Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Mi0041 java and web design
Mi0041  java and web designMi0041  java and web design
Mi0041 java and web design
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 

Similaire à C++ Chapter 11 OOP - Part 2

C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7
DanWooster1
 
C++ Chapter 11 OOP - Part 5
C++ Chapter 11 OOP - Part 5C++ Chapter 11 OOP - Part 5
C++ Chapter 11 OOP - Part 5
DanWooster1
 
C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6
DanWooster1
 
C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3
DanWooster1
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
DanWooster1
 
C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 4C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 4
DanWooster1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
farhan amjad
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptxInheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lines
lokeshG38
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 

Similaire à C++ Chapter 11 OOP - Part 2 (20)

C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7
 
C++ Chapter 11 OOP - Part 5
C++ Chapter 11 OOP - Part 5C++ Chapter 11 OOP - Part 5
C++ Chapter 11 OOP - Part 5
 
C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6
 
C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
 
C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 4C++ Chapter 11 OOP - Part 4
C++ Chapter 11 OOP - Part 4
 
Chapter11.ppt
Chapter11.pptChapter11.ppt
Chapter11.ppt
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Ch7Structs.pptx
Ch7Structs.pptxCh7Structs.pptx
Ch7Structs.pptx
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
 
Lecture 08-inheritance-i
Lecture 08-inheritance-iLecture 08-inheritance-i
Lecture 08-inheritance-i
 
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptxInheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptx
 
Class and object
Class and objectClass and object
Class and object
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lines
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 

Plus de 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
 

Plus de 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
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

C++ Chapter 11 OOP - Part 2

  • 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.1 The this Pointer and Constant Member Functions •this pointer: - Implicit parameter passed to every member function - it points to the object calling the function •const member function: - does not modify its calling object 11-3
  • 4. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Using the this Pointer Can be used to access members that may be hidden by parameters with the same name: class SomeClass { private: int num; public: void setNum(int num) { this->num = num; } }; 11-4
  • 5. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Constant Member Functions •Declared with keyword const •When const appears in the parameter list, int setNum (const int num) the function is prevented from modifying the parameter. The parameter is read-only. •When const follows the parameter list, int getX()const the function is prevented from modifying the object. 11-5
  • 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-6 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
  • 7. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1-7
  • 8. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.2 Static Members •Static member variable: –There is one instance of the variable for the entire class –It is shared by all objects of the class •Static member function: –Can be used to access static member variables –Can be called before any class objects are created 11-8
  • 9. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Static Member Variables 1 of 3 1) Must be declared in the class with the keyword static: class IntVal { public: intVal(int val = 0) { value = val; valCount++ } int getVal(); void setVal(int); private: int value; static int valCount; }; 11-9
  • 10. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Static Member Variables 2 of 3 2) Must be defined outside of the class: class IntVal { //In-class declaration static int valCount; //Other members not shown }; //Definition outside of class int IntVal::valCount = 0; 11-10
  • 11. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Static Member Variables 3 of 3 3) Can be accessed or modified by any object of the class: Modifications by one object are visible to all objects of the class: IntVal val1, val2; 11-11
  • 12. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Static Member Functions 1 of 2 1)Declared with static before the return type: class IntVal { public: static int getValCount() { return valCount; } private: int value; static int valCount; }; 11-12
  • 13. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Static Member Functions 2 of 2 2) Can be called independently of any class objects, through the class name: cout << IntVal::getValCount(); 3) Because of item 2 above, the this pointer cannot be used 4) Can be called before any objects of the class have been created 5) Used primarily to manipulate static member variables of the class 11-13
  • 14. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1- 14
  • 15. 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-15 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
  • 16. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1- 16
  • 17. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.3 Friends of Classes •Friend function: a function that is not a member of a class, but has access to private members of the class •A friend function can be a stand-alone function or a member function of another class •It is declared a friend of a class with the friend keyword in the function prototype 11-17
  • 18. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Friend Function Declarations 1 of 2 1) Friend function may be a stand-alone function: class aClass { private: int x; friend void fSet(aClass &c, int a); }; void fSet(aClass &c, int a) { c.x = a; } 11-18
  • 19. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Friend Function Declarations 2 of 2 2) A friend function may be a member of another class: class aClass { private: int x; friend void OtherClass::fSet (aClass &c, int a); }; class OtherClass { public: void fSet(aClass &c, int a) { c.x = a; } }; 11-19
  • 20. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Friend Class Declaration 1 of 2 3) An entire class can be declared a friend of a class: class aClass {private: int x; friend class frClass; }; class frClass {public: void fSet(aClass &c,int a){c.x = a;} int fGet(aClass c){return c.x;} }; 11-20
  • 21. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Friend Class Declaration 2 of 2 •If frClass is a friend of aClass, then all member functions of frClass have unrestricted access to all members of aClass, including the private members. •In general, you should restrict the property of Friendship to only those functions that must have access to the private members of a class. 11-21