Copy Constructor, Assignment, Equality
|
|
- Aubrey Scott
- 6 years ago
- Views:
Transcription
1 CS 246: Software Abstraction and Specification Lecture 4 Copy Constructor, Assignment, Equality Readings: Eckel, Vol. 1 Ch. 11 References and the Copy-Constructor Ch. 12 Operator Overloading (operator=) U Waterloo CS246se (Spring 2011) p.1/21
2 Copying Objects How many times is an object copied in the following code? class Money { ; Money operator+ (Money m, Money n); int main() { Money m(10, 30); Money n(m); Money p = m; p = p + n; U Waterloo CS246se (Spring 2011) p.2/21
3 Copying Objects with Pointers Shallow copy copies the object and its pointers' addresses, so that the original and copied pointers refer to the same object. day_ = 17 month_ year_ = 2011 day_ = 17 month_ year_ = 2011 "Jan" "Feb" "Mar" "Apr" "May" Deep copy copies the object and what its pointers point to, so that the pointer data members refer to distinct objects. p1 code_ "N2L 3G1" p2 code_ "N2L 3G1" U Waterloo CS246se (Spring 2011) p.3/21
4 Copy Constructor A copy constructor constructs a new object whose value is equal to an existing object. We can define our own, which the compiler uses whenever it needs to copy an object. class Password { Password(const Password&); // copy constructor private: string *passwd_; Password::Password (const Password &p) { if (p.passwd_ == 0) { passwd_ = 0; else { passwd_ = new string( *(p.passwd_) ); U Waterloo CS246se (Spring 2011) p.4/21
5 Copy Constructor class MyClass { MyClass(const MyClass&); // copy constructor private: A a_; // object type B *b_; // pointer type int c_; // simple data type ; MyClass::MyClass (const MyClass &m) : a_(m.a_), c_(m.c_) { if (m.b_ == 0) { b_ = 0; else { b_ = new B(*m.b_); Best Practice: copy simple data members and object members via their copy constructors, invoked in the constructor's initialization list. U Waterloo CS246se (Spring 2011) p.5/21
6 Copy Constructor class MyClass { MyClass(const MyClass&); // copy constructor private: A a_; // object type B *b_; // pointer type int c_; // simple data type ; Call to Class A's copy constructor MyClass::MyClass (const MyClass &m) : a_(m.a_), c_(m.c_) { if (m.b_ == 0) { b_ = 0; else { b_ = new B(*m.b_); Best Practice: copy simple data members and object members via their copy constructors, invoked in the constructor's initialization list. Uninitialized members are constructed (via default constructor) and assigned (via the member's assignment operator) inside the method. U Waterloo CS246se (Spring 2011) p.6/21
7 HowMany Example class HowMany { string name_; // Object identifier static int objectcount_; HowMany(const string& id = "") : name_(id) { ++objectcount_; printcount("howmany()"); ~HowMany() { --objectcount_; printcount("~howmany()"); // The copy-constructor: HowMany(const HowMany& h) : name_(h.name_) { name_ += " copy"; ++objectcount_; printcount("howmany(const HowMany&)"); void printcount(const string& Location = "") const { if(location.size()!= 0) { cout << Location << endl; cout << '\t' << name_ << ": "objectcount = " << objectcount_ << endl; ; int HowMany::objectCount = 0; Bruck U Waterloo Eckel, CS246se Thinking (Spring C ) Vol. p.7/21 1
8 HowMany Example // Pass and return BY VALUE: 1) HowMany f(howmany x) { 2) x.printcount("x argument inside f()"); 3) cout << "Returning from f()" << endl; 4) return x; 5) 6) int main() { 7) HowMany h("h"); 8) cout << "Entering f()" << endl; 9) HowMany h2 = f(h); 10) h2.printcount("h2 after call to f()"); 11) cout << "After call to f()" << endl; 12) Bruck Eckel, Thinking in C++ Vol. 1 U Waterloo CS246se (Spring 2011) p.8/21
9 Compiler-Generated Copy Constructor If we do not declare a copy constructor for our class, the compiler will generate one for us: based on memberwise initialization simple data members: bitwise copy pointer members: bitwise copy member objects: "recursively" calls members' copy constructors class MyClass { // NO copy constructor - compiler will generate one private: A a_; // member object B *b_; // pointer member int c_; // simple data member ; Best Practice: Define your own copy constructor. U Waterloo CS246se (Spring 2011) p.9/21
10 Preventing Pass-by-Value We can prohibit copy construction (and all pass-by-value) by declaring this method as a private method and never defining it. class Password { private: string *passwd_; Password(const Password&); // copy constructor // declared but never defined U Waterloo CS246se (Spring 2011) p.10/21
11 Singleton Design Pattern Can ensure that exactly one object instance of our class exists (singleton design pattern) via static member objects and a private copy constructor. class Egg { static Egg e; // singleton instance int i; // data member Egg(int ii) : i(ii) { // unimplemented constructor Egg(const Egg&); // unimplemented copy-constructor static Egg* instance() { return &e; int val() const { return i; ; Egg Egg::e(47); // initialization of singleton Bruck Eckel, Thinking in C++ Vol. 1 U Waterloo CS246se (Spring 2011) p.11/21
12 Assignment Operator Similar to the copy constructor, except that the destination of the copy already exists. MyType a; MyType b(a); MyType c = a; b = c; // default construction // copy construction // copy construction // assignment U Waterloo CS246se (Spring 2011) p.12/21
13 Overloading Assignment class Rational { Rational(int num=0, int denom=1); Rational& operator=(const Rational&); // must be member func private: int num_; int denom_; ; Rational& operator= (const Rational &r) { num_ = r.num_; // copy member values denom_ = r.denom_; return *this; // return reference to self U Waterloo CS246se (Spring 2011) p.13/21
14 Standard Format of operator= class MyClass { MyClass& operator= (const MyClass&); // overloading assignment private: A a_; // object type B *b_; // pointer type int c_; // simple data type ; MyClass& MyClass::operator= (const MyClass &mc) { if (&mc!= this) { // 1. Check self assignment a_ = mc.a_; c_ = mc.c_; // 2. (Re)use member's assignment operations delete b_; // 3. Assign to pointer member if (mc.b_ == 0) // design decision: shallow or deep copy b_ = 0; // (don't forget to manage memory!) else { b_ = new B(*mc.b_); return *this; // 4. Return reference to self U Waterloo CS246se (Spring 2011) p.14/21
15 Password Example class Password { Password& operator= (const Password&); // assignment private: string *passwd_; Password& operator= (const Password &p) { if (&p!= this) { delete passwd_; if (p.passwd_ == 0) { passwd_ = 0; else { passwd_ = new string( *p.passwd_ ); return *this; U Waterloo CS246se (Spring 2011) p.15/21
16 Compiler-Generated Assignment Operator If we do not overload the assignment operator for our class, the compiler will create an operator= member function for us: based on memberwise assignment: simple data members: bitwise copy pointer members: bitwise copy (may not be what we want) member objects: "recursively" calls members' assignment ops class MyClass { // NO overloaded operator= - compiler will generate one private: A a_; // member object B *b_; // pointer member int c_; // simple data member ; Best Practice: Define your own assignment operator. U Waterloo CS246se (Spring 2011) p.16/21
17 Equality The compiler will NOT generate a comparison operator for us. We have to define our own. bool operator== (const Password &p) const { return *passwd_ == p.passwd_; U Waterloo CS246se (Spring 2011) p.17/21
18 Copy, Assignment, Equality MyType a; MyType b(a); MyType c = a; b = c; // default construction // copy construction // copy construction // assignment The copy constructor, equality, and assignment methods should be compatible with one another copying an object should be the same (==) as assigning the values of an existing object to the initial values of a new object a copied/assigned object should be equal (==) to the original U Waterloo CS246se (Spring 2011) p.18/21
19 class Dog { string nm_; Summary of Essential Methods Dog(const string& name) : nm_(name) { cout << "Creating Dog: " << *this << endl; ~Dog() { cout << "Deleting Dog: " << *this << endl; // Compiler-generated copy-constructor & operator= are OK // Create a Dog from a Dog pointer: Dog(const Dog* dp, const string& msg) : nm_(dp->nm_ + msg) { cout << "Copied dog " << *this << " from " << *dp << endl; friend ostream& operator<<(ostream& os, const Dog& d) { return os << "[" << d.nm_ << "]"; ; Bruck Eckel, Thinking in C++ Vol. 1 U Waterloo CS246se (Spring 2011) p.19/21
20 class DogHouse { Dog* d_; string house_; Summary of Essential Methods DogHouse(Dog* dog, const string& hname) : d_(dog), house_(hname){ DogHouse(const DogHouse& dh) : d_(new Dog(dh.d_, " copy-constructed")), house_(dh.house_ + " copy-constructed") { ~DogHouse() { delete d_; DogHouse& operator=(const DogHouse& dh) { // Check for self-assignment: if (&dh!= this) { delete d_; d_ = new Dog(dh.d_, " assigned"); house_ = dh.house_ + " assigned"; return *this; Dog* getdog() const { return d_; friend ostream& operator<<(ostream& os, const DogHouse& dh) { return os << "[" << dh.house_ << "] contains " << *dh.d_; Bruck ; Eckel, Thinking in C++ Vol. 1 U Waterloo CS246se (Spring 2011) p.20/21
21 Summary of Essential Methods int main() { DogHouse fidos(new Dog("Fido"), "FidoHouse"); cout << fidos << endl; DogHouse fidos2 = fidos; // Copy construction cout << fidos2 << endl; DogHouse fidos3(new Dog("Spot"), "SpotHouse"); cout << fidos3 << endl; fidos3 = fidos2; cout << fidos3 << endl; // Assignment Bruck Eckel, Thinking in C++ Vol. 1 U Waterloo CS246se (Spring 2011) p.21/21
Special Member Functions
CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) U Waterloo CS247 (Spring 2017)
More informationSpecial Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions
Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) C++
More informationConstants, References
CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const
More informationValue vs. Entity Objects, Information Hiding
CS 247: Software Engineering Principles Value vs. Entity Objects, Information Hiding Readings: Eckel, Vol. 1 Ch. 5 Hiding the Implementation U Waterloo CS247 (Spring 2017) p.1/12 Entity vs. Value Objects
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien Deantoni adapted from Jean-Paul Rigault courses This Week A little reminder Constructor / destructor Operator overloading Programmation
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien DeAntoni adapted from Jean-Paul Rigault courses 1 2 This Week A little reminder Constructor / destructor Operator overloading
More informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More informationReview: C++ Basic Concepts. Dr. Yingwu Zhu
Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to
More informationIntermediate Programming & Design (C++) Classes in C++
Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called
More information14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members
Chapter 14: More About Classes 14.1 Instance and Static Members Instance and Static Members instance variable: a member variable in a class. Each object has its own copy. static variable: one variable
More informationCS11 Intro C++ Spring 2018 Lecture 5
CS11 Intro C++ Spring 2018 Lecture 5 C++ Abstractions C++ provides rich capabilities for creating abstractions class Complex { double re, im; public: Complex(double re, double im);... ; Would be nice if
More informationComp151. Generic Programming: Container Classes
Comp151 Generic Programming: Container Classes Container Classes Container classes are a typical use for class templates, since we need container classes for objects of many different types, and the types
More informationC++ 8. Constructors and Destructors
8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of
More informationObject-Oriented Programming, Iouliia Skliarova
Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout
More informationModern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi
Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance
More informationEINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science
EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science Written examination Homologation C++ and Computer Organization (2DMW00) Part I: C++ - on Tuesday, November 1st 2016, 9:00h-12:00h.
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationSoftware Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example
Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell
More informationCircle all of the following which would make sense as the function prototype.
Student ID: Lab Section: This test is closed book, closed notes. Points for each question are shown inside [ ] brackets at the beginning of each question. You should assume that, for all quoted program
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 9, 2016 Outline Outline 1 Chapter 9: C++ Classes Outline Chapter 9: C++ Classes 1 Chapter 9: C++ Classes Class Syntax
More informationstatic CS106L Spring 2009 Handout #21 May 12, 2009 Introduction
CS106L Spring 2009 Handout #21 May 12, 2009 static Introduction Most of the time, you'll design classes so that any two instances of that class are independent. That is, if you have two objects one and
More informationSFU CMPT Topic: Has-a Relationship
SFU CMPT-212 2008-1 1 Topic: Has-a Relationship SFU CMPT-212 2008-1 Topic: Has-a Relationship Ján Maňuch E-mail: jmanuch@sfu.ca Friday 22 nd February, 2008 SFU CMPT-212 2008-1 2 Topic: Has-a Relationship
More informationIS0020 Program Design and Software Tools Midterm, Fall, 2004
IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the
More informationTHE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.
Constructor and Destructor Member Functions Constructor: - Constructor function gets invoked automatically when an object of a class is constructed (declared). Destructor:- A destructor is a automatically
More informationCISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:
CISC 2200 Data Structure Fall, 2016 C++ Review:3/3 1 From last lecture: pointer type and pointer variable (stores memory addresses of a variable (of any type, local or global, automatic/static/dynamic)
More informationOverloading Operators in C++
Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what
More informationDetermine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array
Recursion review Thinking recursion: if I know the solution of the problem of size N-, can I use that to solve the problem of size N. Writing recursive routines: Decide the prototype Can formulate both
More informationpointers & references
pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using
More informationOperator overloading: extra examples
Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading
More informationAssertions and Exceptions
CS 247: Software Engineering Principles Assertions and Exceptions Reading: Eckel, Vol. 2 Ch. 1 Exception Handling U Waterloo CS247 (Spring 2017) p.1/32 Defensive Programming The question isn t whether
More information/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/
/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/ #1: Give the output for the following program. #include
More informationOutline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor
Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened
More informationChapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.
Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example
More informationConcepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.
CSC 143 Classes with Dynamically Allocated Data Concepts (this lecture) Constructors and destructors for dynamic data The this pointer Deep versus shallow copy Defining assignment (operator=) Copy constructor
More informationMore About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil
More About Classes Gaddis Ch. 14, 13.3 CS 2308 :: Fall 2015 Molly O'Neil Pointers to Objects Just like pointers to structures, we can define pointers to objects Time t1(12, 20, true); Time *tptr; tptr
More informationChapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo
Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this
More informationCAAM 420 Fall 2012 Lecture 29. Duncan Eddy
CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................
More informationCh 8. Operator Overloading, Friends, and References
2014-1 Ch 8. Operator Overloading, Friends, and References May 28, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel
More informationWritten by John Bell for CS 342, Spring 2018
Advanced OO Concepts Written by John Bell for CS 342, Spring 2018 Based on chapter 3 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from other sources. Constructors Constructors
More informationDue Date: See Blackboard
Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.
More informationMidterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?
Midterm 2 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer? A handle class is a pointer vith no visible type. What s wrong with
More informationCS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1
CS32 - Week 1 Umut Oztok June 24, 2016 Administration Email:umut@ucla.edu Office hours: R 09:30am-12:30pm (BH 4663) Constructor Special member function to initialize an instance of a class. Called whenever
More informationUse the dot operator to access a member of a specific object.
Lab 16 Class A class is a data type that can contain several parts, which are called members. There are two types of members, data member and functions. An object is an instance of a class, and each object
More informationProgramming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor
Programming C++ Lecture 6 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Friends 2 Friends of Objects S Classes sometimes need friends. S Friends are defined outside
More informationG52CPP C++ Programming Lecture 13
G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and
More informationReusability. Relationships among Objects. Relationships among Objects
Reusability Reusability means that a class that has been designed, created and debugged once can be distributed to other programmers for use in their own programs. Similar to the idea of a library of functions
More informationModule Operator Overloading and Type Conversion. Table of Contents
1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading
More informationImplementing Abstract Data Types (ADT) using Classes
Implementing Abstract Data Types (ADT) using Classes Class Definition class classname { public: //public member functions private: //private data members and member functions }; // Note the semicolon!
More informationObject-Oriented Programming
- oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne
More informationCSC1322 Object-Oriented Programming Concepts
CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.
More informationLecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli
Lecture-5 Miscellaneous topics Templates W3101: Programming Languages C++ Miscellaneous topics Miscellaneous topics const member functions, const arguments Function overloading and function overriding
More informationCh. 11: References & the Copy-Constructor. - continued -
Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed
More informationCS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.
CS 247: Software Engineering Principles C++ Templates Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth U Waterloo CS247 (Spring 2017) p.1/16 Overloaded Functions Problem: Code in overloaded functions whose
More informationClasses Ch
Classes Ch 10.1-10.3 Highlights - public/private - constructor - friend functions - operator overloading class vs array Arrays group together similar data types (any amount you want) Classes (and structs)
More informationAssignment of Structs
Deep Copy 1 Assignment of Structs 2 Slides 1. Table of Contents 2. Assignment of Structs 3. Dynamic Content 4. Shallow Copying 5. Assignment Operator 6. Deep Assignment Copy 7. Assignment Problems 8. Assignment
More informationCome and join us at WebLyceum
Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad
More informationLab 2: ADT Design & Implementation
Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted
More informationChapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved
Chapter 8 Operator Overloading, Friends, and References Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Basic Operator Overloading Unary operators As member functions Friends
More informationShahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods
Overloading Operators friend functions static data and methods Shahram Rahatlou Computing Methods in Physics http://www.roma1.infn.it/people/rahatlou/cmp/ Anno Accademico 2018/19 Today s Lecture Overloading
More informationMore Advanced Class Concepts
More Advanced Class Concepts Operator overloading Inheritance Templates PH (RH) (Roger.Henriksson@cs.lth.se) C++ Programming 2016/17 146 / 281 Operator Overloading In most programming languages some operators
More informationDo not write in this area TOTAL. Maximum possible points: 75
Name: Student ID: Instructor: Borja Sotomayor Do not write in this area 1 2 3 4 5 6 7 TOTAL Maximum possible points: 75 This homework assignment is divided into two parts: one related to the fundamental
More informationLecture 2. Binary Trees & Implementations. Yusuf Pisan
CSS 343 Data Structures, Algorithms, and Discrete Math II Lecture 2 Binary Trees & Implementations Yusuf Pisan Overview 1. Huffman Coding and Arithmetic Expressions 2. 342 Topics a. Pointers & References
More informationWentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.
Lecture 14 1 Exceptions Iterators Random numbers Casting Enumerations Pairs The Big Three Outline 2 Error Handling It is often easier to write a program by first assuming that nothing incorrect will happen
More informationConstructors.
Constructors Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New
More informationCMPT 135: Midterm Answer Key
CMPT 135, Spring 2017, Surrey Midterm Answer Key Page 1 of 6 CMPT 135: Midterm Answer Key Last name exactly as it appears on your student card First name exactly as it appears on your student card Student
More informationIntroduction & Review
CPSC 250 Data Structures Introduction & Review Dr. Yingwu Zhu What to learn? ADT design & implementation using C++ class Algorithm efficiency analysis Big-O ADTs: Binary search trees, AVL trees, Heaps,
More informationInheritance and Polymorphism
Inheritance and Polymorphism 1 Inheritance extending a clock to an alarm clock deriving a class 2 Polymorphism virtual functions and polymorphism abstract classes MCS 360 Lecture 8 Introduction to Data
More informationDue Date: See Blackboard
Source File: ~/2315/11/lab11.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 1 The purpose of this assignment is to become more familiar with
More informationThe University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value
The University Of Michigan Lecture 15 Andrew M. Morgan Savitch Ch. 8 Operator Overloading Returning By Constant Value Consider This Program class ChangePocketClass public: ChangePocketClass():quarters(0),dimes(0)
More informationSFU CMPT Topic: Class Templates
SFU CMPT-212 2008-1 1 Topic: Class Templates SFU CMPT-212 2008-1 Topic: Class Templates Ján Maňuch E-mail: jmanuch@sfu.ca Monday 3 rd March, 2008 SFU CMPT-212 2008-1 2 Topic: Class Templates Class templates
More informationCSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan
CSS 343 Data Structures, Algorithms, and Discrete Math II Polymorphism Yusuf Pisan Polymorphism Hierarchy of classes that are related by inheritance static linkage / early binding Decide on function to
More informationA class is a user-defined type. It is composed of built-in types, other user-defined types and
Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.
More informationReview. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?
Review Describe pass-by-value and pass-by-reference Why do we use pass-by-reference? What does the term calling object refer to? What is a const member function? What is a const object? How do we initialize
More informationCS 246: Software Abstraction and Specification. Lecture 8. Object Composition
CS 246: Software Abstraction and Specification Lecture 8 Object Composition Agenda: Object Composition Implementing Composite Objects "Law" of Demeter Reading: Eckel, Vol. 1 Ch. 14 Inheritance and Composition
More information(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.
CS 7A Final Exam - Fall 206 - Final Exam Solutions 2/3/6. Write the number of the definition on the right next to the term it defines. () Defining two functions or operators with the same name but different
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded
More informationCS105 C++ Lecture 7. More on Classes, Inheritance
CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.
More informationAssignment of Objects
Copying Objects 1 Assignment of Objects 2 Slides 1. Table of Contents 2. Assignment of Objects 3. Dynamic Content 4. Shallow Copying 5. Deep Copying 6. this Pointer 7. Improved Deep Copy 8. Passing an
More informationMore class design with C++ Starting Savitch Chap. 11
More class design with C++ Starting Savitch Chap. 11 Member or non-member function? l Class operations are typically implemented as member functions Declared inside class definition Can directly access
More informationReference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that
Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes
More informationCSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms
CSC 330 Object Oriented Programming Operator Overloading Friend Functions & Forms 1 Restrictions on Operator Overloading Most of C++ s operators can be overloaded. Operators that can be overloaded + -
More informationProgramming, numerics and optimization
Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428
More informationW3101: Programming Languages C++ Ramana Isukapalli
Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationChapter 9 Technicalities: Classes, etc.
Chapter 9 Technicalities: Classes, etc. Dr. Hyunyoung Lee Based on slides by Dr. Bjarne Stroustrup www.stroustrup.com/programming Abstract This lecture presents language technicalities, mostly related
More informationCS304 Object Oriented Programming Final Term
1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing
More informationCS113: Lecture 4. Topics: Functions. Function Activation Records
CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make
More informationIntroduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.
Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and
More informationCSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 2 Yusuf Pisan Compiled helloworld yet? Overview C++ fundamentals Assignment-1 CSS Linux Cluster - submitting assignment Call by Value,
More informationOverloaded Operators, Functions, and Students
, Functions, and Students Division of Mathematics and Computer Science Maryville College Outline Overloading Symbols 1 Overloading Symbols 2 3 Symbol Overloading Overloading Symbols A symbol is overloaded
More informationCS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.
CS 31 Discussion 1A, Week 8 Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. Today s focus Pointer Structure Clarifications Pointer A pointer is the memory address of a variable.
More informationCh. 12: Operator Overloading
Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42
More informationCS 61B, Spring 1996 Midterm #1 Professor M. Clancy
CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem
More informationInitializing and Finalizing Objects
4 Initializing and Finalizing Objects 147 Content Initializing and Finalizing Objects 4 Constructors Default Constructor Copy Constructor Destructor 148 Initializing Objects: Constructors Initializing
More informationStarting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,
Classes Starting Savitch Chapter 10 l l A class is a data type whose variables are objects Some pre-defined classes in C++ include int, char, ifstream Of course, you can define your own classes too A class
More informationCS 162, Lecture 25: Exam II Review. 30 May 2018
CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you
More informationAssignment operator string class c++ Assignment operator string class c++.zip
Assignment operator string class c++ Assignment operator string class c++.zip Outside class definitions; Addition assignment: a += b: The binding of operators in C and C++ is specified (character string)
More informationAnd Even More and More C++ Fundamentals of Computer Science
And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members
More information