Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Informática II Prof. Dr. Gustavo Patiño MJ
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Memory Management Issues, Solutions, and Examples.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Workin’ with Pointas An exercise in destroying your computer.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Memory Management.
Learning Objectives Pointers as dada members
Computer Science 210 Computer Organization
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
CISC181 Introduction to Computer Science Dr
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Checking Memory Management
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Classes with Dynamically Allocated Data
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
Computer Science 210 Computer Organization
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Indirection.
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Stacks CS-240 Dick Steflik.
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Data Structures and Algorithms Memory allocation and Dynamic Array
Class: Special Topics 2 For classes using memory allocation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and References
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct in C but with the addition of functions An Object is an instance of a class Objects share the same functions with other objects of the same class, but each object or instance has its own copy of the data structure I.e, the member data

Member Functions These are functions declared within a class definition They are referred to as methods of the class Member Functions may be public or private (as can member data)

Public and Private A public member (data or function) is one that can be read or written (data) or called (functions) by anybody A private member can only be read, written, or called by a member function of the class

Class Example 1 class Stack { public: void Push(int value); int top; int stack[10]; }; void Stack::Push(int value) { ASSERT(top < 10); stack[top++] = value; }

Class Example 1 The Stack class has two data members and one member function All of these are public If we have a pointer “s” to a Stack object we can access top by doing like so s->top = 5; If top were private we'd still be able to modify it through the member function Push as it increments the top variable and is public

Class Example 2 class Stack { public: void Push(int value); bool Full(); int top; int stack[10]; }; bool Stack::Full() { return (top == 10); } void Stack::Push(int value) { ASSERT(!(this->Full())); stack[top++] = value;

Class Example 2 You can get a pointer to the current object that has been called by using the this keyword Note that the this is not necessary as it is implicit

Class Example 3 class Stack { public: void Push(int value); bool Full(); private: int top; int stack[10]; };

Class Example 3 In this example, all data members are private Private Members are only visible to member functions of the class all data members are only accessible by the member functions Full() and Push(int) It is suggested that data members remain private if possible

Constructors and the "new" operator In C one uses the malloc() function to allocate memory on the heap In C++ we use the new Operator C might look like this struct Stack * s = (struct Stack *)malloc(sizeof(struct Stack)); InitStack(s, 17); C++ would look like this Stack * s = new Stack(17);

Constructors and the “new” operator – Example 4 class Stack { public: Stack(int sz); void Push(int value); bool Full(); private: int size; int top; int * stack; };

Example 4 continued. // constructor Stack::Stack(int sz) { size = sz; top = 0; stack = new int[size]; }

Example 4 – The Constructor To specify how an object is initialized we write a constructor for it as a member function. This member function has the same name as the class name i.e, Stack(). Stack(int) is the constructor in this example

Constructors and new Operator The new operator automatically allocates space for the object and calls the constructor for the new object If you declare an automatic variable then the object space is allocated on the stack and the constructor is called

Final words on new and constructors Notice the constructor does not have a return value Always define a constructor and always initialize all data members If you do not create a constructor one is automatically defined (not recommended)

Destructors and delete operator delete is to new as free() is to malloc() TO destroy an object allocated with the new operator do: delete s; This deallocates the object but first calls the destructor. The destructor, like the constructor as it is a member function of the Stack class and is called ~Stack()

Example 5 class Stack { public: Stack(int sz); ~Stack(); void Push(int value); bool Full(); private: int size; int top; int * stack; };

Example 5 Continued Stack::~Stack() { delete [] stack; } Notice: to delete the whole allocated array the [] comes after delete and before the array variable name. The Destructor is used to deallocate data allocated by the constructor. It is not always necessary to define one.

Pointers! Pointers hold or store an address in memory Pointers may point to any type Assigning one pointer to another causes both pointers to point to the same location in memory, hence the same data or object

Pointers! - Example void main() { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) x = malloc(sizeof(int)); // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee *y = 13; // CRASH -- y does not have a pointee yet y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee }

Pointers – Example (with “new”) void main() { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) x = new int; // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee *y = 13; // CRASH -- y does not have a pointee yet y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee }

Question At the end of the above code, y is set to have a pointee and then dereferenced it store the number 13 into its pointee. After this happens, what is the value of x's pointee?

Answer The value of x's pointee is 13 because it is also y's pointee. This is what sharing is all about -- multiple pointers pointing to one pointee.

Question 2 Write some code that creates the above pointer structure in C++ using the new operator.

Answer int* x; int* y; x = new int; y = new int; *x = 1; *y = 2; x = y;