The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Writing a Good Program 6. Pointers and Arrays
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Recursion Fall 2008 Dr. David A. Gaitros
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
C++ Review (3) Structs, Classes, Data Abstraction.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming (OOP) Lecture No. 11.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Overloading C++ supports the concept of overloading Two main types
Function Overloading Can enables several function Of same name
Access Functions and Friend Functions
Chapter 6 CS 3370 – C++ Functions.
Lecture 5: Classes (continued) Feb 1, 2005
Command Line Arguments
Pointers and Pointer-Based Strings
group work #hifiTeam
More on Classes Programming in C++ Fall 2008
Chapter 5 Function Basics
Contents Introduction to Constructor Characteristics of Constructor
Pointers & Functions.
Today’s Topic Const Ref:
9-10 Classes: A Deeper Look.
Passing Arguments and The Big 5
Pointers and Pointer-Based Strings
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Pointers & Functions.
Class: Special Topics Overloading (methods) Copy Constructors
9-10 Classes: A Deeper Look.
Presentation transcript:

The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros

The This Pointer The Problem: “Yes, inside of a class you have access to all of the private data, but how do you access the object itself like a client would?” Each object maintains a pointer to itself which is called the “this” pointer. Each object can determine it’s own address by using the “this” keyword. Many member functions of a class in C++ require no arguments because of the use of the implicit pointer “this”. Very handy to use this which avoids the overhead of passing parameters but still enforces the rules of good sound software engineering by using the appropriate class functions.

The This Pointer // Example of using the this pointer #include using namespace std; class Test{ public: Test (int=0); void print () const; private: int x; };

The This Poiner Test::Test (int a ) // Constructor { x = a;} void Test::print() const { cout << " x is equal to " << x ; cout is equal to " x; cout << "\n (*this).x is equal to "<< (*this).x <<endl; } int main (void) { Test testobject(12); testobject.print(); return 0; }

The This Pointer It may seem redundant but the “this” pointer does have some uses: – Prevents an object from being assigned to itself. – Enables cascading member function calls.