this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Similar documents
Classes: A Deeper Look. Systems Programming

CS 1337 Computer Science II Page 1

Computer Programming with C++ (21)

A Deeper Look at Classes

Constant Member Functions

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

Lecture 5. Function Pointers

Computer Programming Class Members 9 th Lecture

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Chapter 9 Classes : A Deeper Look, Part 1

C How to Program, 6/e, 7/e

Chapter 17 - C++ Classes: Part II

OBJECT ORIENTED PROGRAMMING USING C++

IS 0020 Program Design and Software Tools

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

IS 0020 Program Design and Software Tools

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

Introduction to Programming session 24

Classes and Data Abstraction. Topic 5

A <Basic> C++ Course

A <Basic> C++ Course

Tutorial letter 202/2/2018

Inheritance and Polymorphism

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

CSCE 110 PROGRAMMING FUNDAMENTALS

Object Oriented Programming with C++ (24)

CSI33 Data Structures

Implementing an ADT with a Class

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

CS 247: Software Engineering Principles. ADT Design

การทดลองท 8_2 Editor Buffer Array Implementation

Practice Problems CS2620 Advanced Programming, Spring 2003

Programming II (CS300)

EE 152 Advanced Programming LAB 7

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Constants, References

Sheet 3. void SetPayRate(float rate); float CalcGrossPay(); void Validation(ifstream & in); //employee.h. //operator overloading. #include <fstream.

Classes: A Deeper Look

Chapter 11: More About Classes and Object-Oriented Programming

Intermediate Programming, Spring 2017*

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

C++ For Science and Engineering Lecture 15

Software Development With Java CSCI

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Programming C++ Lecture 2. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Object Oriented Design

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Chapter 6: Classes and Data Abstraction

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Object Oriented Programming COP3330 / CGS5409

Abstract Data Types. Development and Implementation. Well-defined representations that allow objects to be created and used in an intuitive manner

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

ADTs & Classes. An introduction

Lecture 7. Log into Linux New documents posted to course webpage

Extending Classes (contd.) (Chapter 15) Questions:

Inheritance Examples

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Classes and Objects: A Deeper Look

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Operator Overloading

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

C++ Polymorphism. Systems Programming

Polymorphism Part 1 1

Classes and Data Abstraction. Topic 5

IS 0020 Program Design and Software Tools

Classes: A Deeper Look, Part 2

Circle all of the following which would make sense as the function prototype.

Structures. Lecture 15 COP 3014 Spring April 16, 2018

Object Oriented Modeling

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

2. It is possible for a structure variable to be a member of another structure variable.

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Lab 2: ADT Design & Implementation

Come and join us at WebLyceum

Object-Based Programming (Deitel chapter 8)

Fundamentals of Programming Session 24

More C++ Classes. Systems Programming

lecture04: Constructors and Destructors

Inheritance, and Polymorphism.

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

CS 225. Data Structures

TypeScript. Types. CS144: Web Applications

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

C++ Classes, Constructor & Object Oriented Programming

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

What is an algorithm?

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

CSCE 110 PROGRAMMING FUNDAMENTALS

Unit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Transcription:

this Pointer, Constant Functions, Static Data Members, and Static Member Functions 3/2/07 CS250 Introduction to Computer Science II 1 this Pointer (11.1) functions - only one copy of each function exists in memory independent of the number of objects instantiated using the class declaration data members - each unique object of a particular class has space allocated for the data members of the class this - is a pointer that can be used to access an objects data members. No space associated with the class is allocated for the pointer this 3/2/07 CS250 Introduction to Computer Science II 2 Example of this pointer #ifndef RATIONAL_H #define RATIONAL_H using namespace std; class Rational Rational(int, int); print(); int numerator; int denominator; ; #endif 3/2/07 CS250 Introduction to Computer Science II 3 1

Example #include "Rational.h" Rational::Rational(int numerator, int denominator) (*this).numerator = numerator; (*this).denominator = denominator; Rational::print() cout << numerator << '/' << denominator; 3/2/07 CS250 Introduction to Computer Science II 4 Pointers Accessing data members and pointers using pointers (*this).numerator can be replaced with this->numerator 3/2/07 CS250 Introduction to Computer Science II 5 Write the definition for settime class Time int hour; int minute; int second; Time(); Time(int hour = 0, int minute = 0, int second = 0); int gethour(); int getminute(); int getsecond(); void settime(int hour, int minute, int second); void printuniversal(); void printstandard(); ; // end class Time 3/2/07 CS250 Introduction to Computer Science II 6 2

const Many things can be specified as const in C++ Examples: o Objects o Member Functions o Data members o Function arguments 3/2/07 CS250 Introduction to Computer Science II 7 const Objects Principle of least privilege What happens when we declare any object to be a const? Example: o const int SIZE = 50; What do you think it means if I have o const Time dinnertime(18, 30, 0); What member functions of class Time do you think dinnertime can call? 3/2/07 CS250 Introduction to Computer Science II 8 const Member Functions A const object can only call const functions How do we declare member functions to be const? o Use the const keyword in both the function prototype and the function definition o Appears after the parameter list const member functions CANNOT modify data members (i.e. the current instantiation of the class) 3/2/07 CS250 Introduction to Computer Science II 9 3

Time Example class Time int hour; int minute; int second; Time(); Time(int = 0, int = 0, int = 0); int gethour() const; int getminute() const; int getsecond() const; void settime(int, int, int); void printuniversal() const; void printstandard() const; ; // end class Time 3/2/07 CS250 Introduction to Computer Science II 10 Object Details What does memory look like after creating multiple objects of a class? For example: o Time t(3, 45, 00); o Time t2(5, 29); o Time t3(14); o Time t4; o Time *ptime = new Time(); 3/2/07 CS250 Introduction to Computer Science II 11 static Class Members Each object gets it s own copy of the data members What if we wanted a data member to be shared between all objects o Each object sees the same value for the data member o Each object can modify that data member, and the other objects will see the change Data members of this type are called static 3/2/07 CS250 Introduction to Computer Science II 12 4

static Class Member (11.2) static members represent class-wide information and are not specific to one object There is only one copy of the member and it is shared between all objects Why would we ever need or want a static class member? Can you think of an example. 3/2/07 CS250 Introduction to Computer Science II 13 static Class Members They are not global variables The static data member could be declared public, private, or protected static data members must be initialized once 3/2/07 CS250 Introduction to Computer Science II 14 Example #ifndef EMPLOYEE_H #define EMPLOYEE_H class Employee char *firstname; char *lastname; static int count; Employee(const char *,const char *); ~Employee(); char *getfirstname() const; char *getlastname() const; static int getcount(); ; #endif 3/2/07 CS250 Introduction to Computer Science II 15 5

Constructor Definition Employee::Employee(const char *, const char *) firstname = new char[strlen(first) + 1]; strcpy(firstname, first); lastname = new char[strlen(last) + 1]; strcpy(lastname, last); count++ 3/2/07 CS250 Introduction to Computer Science II 16 What is the value of count? int Employee::count = 0; int main() Employee emp1( john, doe ); Employee emp2( jane, doe ); Employee emp3( bob, doe ); 3/2/07 CS250 Introduction to Computer Science II 17 static Member Functions class IntVal int value; static int valcount; static int getvalcount() return valcount; ; 3/2/07 CS250 Introduction to Computer Science II 18 6

Calling Static Functions Can be called independently of class objects, through the class name: cout << IntVal::getValCount(); Can be called before any objects of the class have been created Used mostly to manipulate static member variables of the class 3/2/07 CS250 Introduction to Computer Science II 19 7