Classes Separating interface from implementation

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

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
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.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
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+
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Learners Support Publications Constructors and Destructors.
Memory Management.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Chapter 7: User-Defined Functions II
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Object Oriented Programming (OOP) Lecture No. 9
Constructors and destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
CS410 – Software Engineering Lecture #5: C++ Basics III
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses the class. This means that several files of the same project may all #include the header file. However, the compiler should only process it once. Conditional compilation allows code to be included or omitted based on certain conditions. We can use the following directives for conditional compilation: #ifdef, #ifndef, #else, #endif, #define

Classes Special member functions: constructors have the same name as the class are used to initialize the data members are invoked automatically when class objects are created do not have a return type destructor has the same name as the class preceded by a tilde (~) is used to perform housekeeping (such as memory deallocation) when a class objects is destroyed is invoked automatically when class objects are destroyed does not have arguments does not have a return type is ESSENTIAL when there are pointer data members

Classes When a data member is a pointer, the constructor must allocate space for it and the destructor must deallocate space. Example: class MemberInfo { char *name; int age; public: MemberInfo(char *initname = ""i, int initage = 0) { name = new char[strlen(initname)+1]; strcpy(name, initname); age = initage; } ~MemberInfo() { if (name != NULL) delete [] name; };

Copy constructor There are certain situations when a copy of an object must be made: when passing the object by value when returning an object Furthermore, we want to be able to initialize an object by another object. e.g. Creating a copy of an object is the job of the copy constructor. A special kind of constructor that takes as argument an object of the same type as the class. MemberInfo president ("John Smith", 50); // Now declare clone, a new MemberInfo whose data members // should have the same values as those for president: MemberInfo clone(president);

Copy constructor If a copy constructor is missing, the compiler creates a default one performs member-by-member copying. A default copy constructor for the MemberInfo class would look like this: As we will see, this is INCORRECT! Consider the following piece of code: MemberInfo (const MemberInfo& init) { name = init.name; age = init.age } MemberInfo *president = new MemberInfo ("Smith", 50); MemberInfo clone(*president); delete president; president = NULL; continue on next slide...

Copy constructor & pointers 1: MemberInfo *president = new MemberInfo ("Smith", 50); president 'S' 'm' 'i' 't' 'h' '\0' 50 2: MemberInfo clone(*president); Let's look inside the copy constructor: name = init.name; age = init.age president 'S' 'm' 'i' 't' 'h' '\0' 50 clone 50 continue on next slide...

Copy constructor & pointers 3: delete president; president = NULL; president freed memory clone 50 president and clone should be independent objects. Modifying one should not affect the other. When pointers are involved, the copy constructor must explicitly allocate space and copy the appropriate value. See the next slide for a correct version of the copy constructor.

Copy constructors & pointers MemberInfo (const MemberInfo& init) { name = new char[strlen(init.name)+1]; strcpy(name, init.name); age = init.age }

operator= A problem similar to that of the copy constructor appears when we use assignments. If me and president are two MemberInfo objects, then by default, me = president; will perform a member-by-member assignment. To avoid the problem, the assignment operator must be overloaded by the programmer. Operator overloading = having more than one operator with same name (in the same scope) Example: integers and MemberInfo objects both have the assignment operator defined for them. The operator has the same name, =, but works in different ways depending on the type of the object.

operator= think of obj1 = obj2; as being equivalent to obj1.operator=(obj2); returning a reference to the current object allows chain assignments: obj1 = obj2 = obj3; pass the right-hand-side as a reference but do not allow it to be modified. MemberInfo& operator= (const MemberInfo& rhs) { if ( this == &rhs ) return *this; if ( name != NULL) delete [] name; name = new char[strlen(rhs.name)+1]; strcpy(name, rhs.name); age = rhs.age } the this pointer provides access to an object's own address. this is a C++ keyword.

operator= if the right-hand side and the left-hand side MemberInfo& operator= (const MemberInfo& rhs) { if ( this == &rhs ) return *this; if ( name != NULL) delete [] name; name = new char[strlen(rhs.name)+1]; strcpy(name, rhs.name); age = rhs.age } if the right-hand side and the left-hand side are identical, (e.g. obj1 = obj1) just return a reference to the current object. (Looking ahead: what will happen if we don't perform this check?)

operator= Since the current object will be assigned a new MemberInfo& operator= (const MemberInfo& rhs) { if ( this == &rhs ) return *this; if ( name != NULL) delete [] name; name = new char[strlen(rhs.name)+1]; strcpy(name, rhs.name); age = rhs.age } Since the current object will be assigned a new value, make certain that any space allocated for the current value is properly deallocated.

operator= Finally, perform the actual assignment and MemberInfo& operator= (const MemberInfo& rhs) { if ( this == &rhs ) return *this; if ( name != NULL) delete [] name; name = new char[strlen(rhs.name)+1]; strcpy(name, rhs.name); age = rhs.age } Finally, perform the actual assignment and return a reference to the current object.

pointer data members Whenever a class has at least one data member that is a pointer, you MUST write a destructor a copy contructor an overloaded assignment operator