Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Lists: An internal look
Operator overloading redefine the operations of operators
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
Introduction to Programming Lecture 39. Copy Constructor.
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.
Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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.
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.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Arrays, Pointers and Structures CS-240 Dick Steflik.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Kruse/Ryba ch041 Linked Stacks and Queues Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
1 Overload assignment = as a member function class point { public:..... point & operator = (const point & a);..... private: double my_x; double my_y;.....
Yan Shi CS/SE 2630 Lecture Notes
CS Computer Science IB: Object Oriented Programming
Memberwise Assignment / Initialization
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Chapter 16-2 Linked Structures
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Indirection.
Summary: Abstract Data Type
How Dynamic Memory Works with Memory Diagram
CS148 Introduction to Programming II
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.
9-10 Classes: A Deeper Look.
Stacks CS-240 Dick Steflik.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
How Dynamic Memory Works with Memory Diagram
9-10 Classes: A Deeper Look.
C++ support for Object-Oriented Programming
Presentation transcript:

Value Semantics CS-240 & CS-341 Dick Steflik

Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics consist of the assignment operator and the copy constructor these allow assignment of one object to another and the creation of one object from another

for simple/static objects the default assignment operator and copy constructor will do what is needed; i.e. a member by member copy of the elements making up the object. class Date { public: Date(int m,int d, int y) private: int month,day,year } Date today(5,5,2000); Date May5(today); /* create May5 from today, default copy constructor */ Date May5th ; May5th = today; /* create an object called May5th then assign it a value, default assignment operator*/

for dynamic objects where the private data includes dynamically allocated data (ex. dynamic array) we must overload both the default copy constructor and the default assignment operator. class Stack{ public: Stack(); Stack (int size); Stack (const Stack & source); void operator = (const & source); private: int * data; int top; }

implementation Stack::Stack() /* default constructor */ { data = new int[10]; top = 0; } Stack::Stack(int size) /* constructor */ { data = new int[size]; top = 0; }

copy constructor create the object with the correct size, then copy the elements... Stack::Stack(const Stack & source) { int size = sizeof(source.data)/sizeof(int); /* calc the size */ data = new int[size]; /* allocate */ for (int i = 0 ; i < size; i++) /* copy the data */ data[i] = source[i]; top = source.top; /* copy the top pointer */ }

overload assignment operator calculate the size, discard the old data store, make a new one, then copy elements over... void Stack::operator = (const Stack * source) { int size = sizeof(source.data)/sizeof(int); delete data; data = new int[size]; for (int i=0 ; i< size ; i++) data[i] = source.data[i]; top = source.top; }