1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
Road Map Introduction to object oriented programming. Classes
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
C++ data types. Structs vs. Classes C++ Classes.
Classes Separating interface from implementation
Computer Science II Exam I Review Monday, February 6, 2006.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
ECE 264 Object-Oriented Software Development
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
This pointer, Dynamic memory allocation, Constructors and Destructor
Basic C++ What’s a declaration? What’s a definition?
Andy Wang Object Oriented Programming in C++ COP 3330
CMSC 202 Templates.
COMS 261 Computer Science I
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
Object oriented programming (OOP) Lecture No. 6
Separating Interface from Implementation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Templates CMSC 202, Version 4/02.
CS148 Introduction to Programming II
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
C++ data types.
Destructors, Copy Constructors & Copy Assignment Operators
Operator overloading.
SPL – PS3 C++ Classes.
Presentation transcript:

1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28

2 Announcements

3 Review Classes –User defined data types –Constructors Default –Constructors With parameters –Constructors Parameters with default values

4 Outline Classes –Copy constructor

Classes Copy constructor –VEC v1(1.2f, 3.4f); –VEC v2(v1); Uses the copy constructor Creates v2, an independent object that contains a copy of the data members of v1 v1.x = 1.2; v1.y = 3.4; v2.x = 1.2; v2.y = 3.4; Same values as v1, but a different object

Copy Constructor –Syntax error when compiling VEC::VEC(VEC v) { … } –Since, call by value requires we make a copy of the vector v when calling the copy constructor –The copy constructor would be called over and over int main () { VEC v1; VEC v2(v1); Call the copy constructor Call by value make a copy of v1

Copy Constructor Copy constructor cannot us call by value –Only other choice is call by reference VEC::VEC(VEC& v) { … } –Should the copy constructor ever change the data member values of the reference parameter? No, it should only read the values but not change them Make the parameter a const reference

Copy Constructor vec.h: definition file vec.cpp: implementation file Caution –If you don’t provide an implementation of the copy constructor, the compiler will supply one It may not do what you think it will Run CodeWarrior vec01

Assignment Operator It would be nice to assign one VEC object to another –v1 = v2; To do this we must overload the assignment operator (=) to define a function when a VEC object is on both the lhs and the rhs

Assignment Operator Should there be any arguments? –Yes, the VEC we wish to assign –Avoid making a copy, use call by reference Should the assignment operator change the RHS? –No, make it a const reference –VEC& operator=(const VEC& v); Run CodeWarrior vec03

Assignment Operator The assignment operator for this class does not behave the way the primitive data types do –They allow chaining A = B = C; –What happens if we try to chain the VEC assignment operator? –How do we get around this?