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.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
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?
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
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.
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:
Class Definitions and Writing Methods
"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
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
Anatomy of a Function Part 2
Classes Access Specifiers
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Copy Challenge
How Functions Work Part 1
Classes Short Review of Topics already covered Constructor
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Defining Classes I Part A.
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Functions 2: Stupid Function Tricks
How Dynamic Memory Works with Memory Diagram
How Classes Work with Memory Diagram
Introduction to Programming
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
Lecture 16 Oct 30, 02.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CS148 Introduction to Programming II
Dynamic Memory Copy Challenge
The Stack.
Object-Oriented Programming (OOP) Lecture No. 44
Andy Wang Object Oriented Programming in C++ COP 3330
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
CMSC 202 Constructors Version 9/10.
Day 11 The Last Week!.
Presentation transcript:

How Classes Work with Memory Diagram CSCE 121

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 if (day < 1) { this->day = 1; else if (day > 31) { this->day = 31; 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; } 3/25 /2019 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(); } 3/25 /2019 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