How Classes Work with Memory Diagram

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Definition Class In C++, the class is the construct primarily used to create objects.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 8: Classes 1 Based on slides created by Bjarne Stroustrup.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
Chapter 10 Introduction to Classes
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
ITP © Ron Poet Lecture 15 1 Helper Objects Again.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
CMSC202 Computer Science II for Majors Lecture 09 – Overloaded Operators and More Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Written by: Dr. JJ Shepherd
CMPE Data Structures and Algorithms in C++ September 21 Class Meeting
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 9 Type Conversions
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
"A class is where we teach an object how to behave." --Rich Pattis
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CS Computer Science IB: Object Oriented Programming
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Anatomy of a class Part I
Dynamic Memory CSCE 121 J. Michael Moore.
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Classes and Objects: Encapsulation
Variables with Memory Diagram
Anatomy of a Function Part 2
Classes Access Specifiers
Exceptions with Functions
Dynamic Memory Copy Challenge
Return by Reference CSCE 121 J. Michael Moore.
Anatomy of a Function Part 1
How Functions Work Part 1
Classes Short Review of Topics already covered Constructor
Exceptions CSCE 121 J. Michael Moore
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Functions 2: Stupid Function Tricks
Static in Classes CSCE 121 J. Michael Moore.
How Dynamic Memory Works with Memory Diagram
How Functions Work Part 2
Dynamic Memory A whole heap of fun….
Object Oriented Programming (OOP) Lecture No. 13
Classes.
Today’s Topic Const Ref:
Introduction of Programming
Destructor CSCE 121 J. Michael Moore.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Dynamic Memory Copy Challenge
The Stack.
Object-Oriented Programming (OOP) Lecture No. 44
Anatomy of a Function Part 1
CS 201(Introduction To Programming)
How Dynamic Memory Works with Memory Diagram
CMSC 202 Encapsulation Version 9/10.
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
How Classes Work with Memory Diagram
CMSC 202 Constructors Version 9/10.
Presentation transcript:

How Classes Work with Memory Diagram CSCE 121 J. Michael Moore

output identifier stack int main() { Date w(7, 7, 2015); // constructor w.setDay(4); // mutator w.printDate(); } identifier stack

class Date { int month; int day; int year; public: // constructors Date(); Date(int month, int day, int year); // accessors and mutators int getMonth(); void setMonth(int month); int getDay(); void setDay(int Day); int getYear(); void setYear(int year); // methods void printDate(); };

output identifier stack int main() { Date w(7, 7, 2015); // constructor w.setDay(4); // mutator w.printDate(); } month main day w year identifier stack

output identifier stack implicit parameter ‘this’ Date::Date(int month, int day, int year) : month(month), day(day), year(year) {} implicit parameter ‘this’ year 2015 day 7 Identifier resolution First look for local version from parameter or local variable Second, prepend with ‘this->’ to associate with object and try again Date month 7 this month 7 main day 7 w year 2015 identifier stack

output identifier stack int main() { Date w(7, 7, 2015); // constructor w.setDay(4); // mutator w.printDate(); } year 2015 day 7 Date month 7 this month 7 main day 7 w year 2015 identifier stack

output identifier stack void Date::setDay(int day) { if (day>=1 && day<=31) { this->day = day; } else throw runtime_error( "Invalid day"); day 4 setDay this year 2015 day 7 Date month 7 this month 7 main day 4 w 7 year 2015 identifier stack

output identifier stack int main() { Date w(7, 7, 2015); // constructor w.setDay(4); // mutator w.printDate(); } day 4 setDay this year 2015 day 7 Date month 7 this month 7 main day 4 w 7 year 2015 identifier stack

output identifier stack Recall: Identifier resolution void Date::printDate() { cout << month << "/" << day; cout << "/" << year << endl; } 7/4 /2015 Recall: Identifier resolution First look for local version from parameter or local variable Second, prepend with this-> to associate with object and try again printDate this day 4 setDay this year 2015 day 7 Date month 7 this So body essentially becomes: cout << this->month << "/" << this->day; cout << "/" << this->year << endl; month 7 main day 7 4 w year 2015 identifier stack

output identifier stack int main() { Date w(7, 7, 2015); // constructor w.setDay(4); // mutator w.printDate(); } 7/4 /2015 printDate this day 4 setDay this year 2015 day 7 Date month 7 this month 7 main day 4 w 7 year 2015 identifier stack