1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
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.
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
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.
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
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.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
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+
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSC241: Object Oriented Programming Lecture No 05.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
Programming Techniques Classes II Important Class Features Spring 2009.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Lecture 5: Classes (continued) Feb 1, 2005
Inheritance CMSC 202, Version 4/02.
CISC181 Introduction to Computer Science Dr
Object-Oriented Programming
Constructor & Destructor
Pointers Revisited What is variable address, name, value?
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Oriented Programming (OOP) Lecture No. 9
Constructors and Destructors
#include <iostream.h>
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Presentation transcript:

1 CMSC 202 More C++ Classes

2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March 5 th

3 const Use “const” (constant) whenever possible Supports the concept of “least privilege” Objects may be const Member functions may be const Parameters may be const

4 const Objects We’ve already seen const float PI = 3.14; that creates a float that cannot be changed We can also create const objects const Time noon(12, 0, 0); –noon is not modifiable

5 const member functions Not all member functions modify the object –Accessors –Others later And so should be declared ‘const’ int GetHour ( void ) const; The compiler doesn’t “know” that GetHour( ) doesn’t modify the Time object

6 (non-)const objects and (non-)const member functions Rules of engagement: –const member functions may be invoked for const and non-const objects –non-const member functions can only be invoked for non-const objects –you cannot invoke a non-const member function on a const object Constructors and Destructors may not be const

7 Modified Time class Time { public: Time (int h = 0, int m = 0, int s = 0 ); void InitTime (int h, int m, int s); void PrintMilitary ( void ) const; void PrintStd ( void ) const; void Increment ( void ); …..

8 Modified Time (cont’d) // mutators void SetHour (int h); void SetMinute (int m); void SetSecond (int s); // accessors – should be const int GetHour (void) const; int GetMinute (void) const; int GetSecond (void) const;

9 Modified Time (cont’d) private: int hour; int minute; int second; };

10 int main ( ) { Time t1 (2, 4, 7); const Time noon (12); cout << “Initial Military Time for t1 is :” ; t1.PrintMilitary ( );// okay cout << endl; cout << “Noon in Military Time:” noon.PrintMilitary ( ); // okay cout << endl; cout << “Increment Noon:” ; noon.Increment ( ); // compiler error cout << endl; }

11 Non-member functions should have const reference parameters wherever possible Rules of engagement: –Non-const objects may be passed to const and non-const arguments –Const objects may only be passed to const arguments Bottom line – you can’t pass a const object to a non-const argument

12 Non-member PrintTime void PrintTime (Time& t) { cout << t.GetHour( ) << “:”; cout << t.GetMinute( ) << “:”; cout << t.GetSecond( ); }

13 Calling PrintTime void PrintTime (Time& t); Callable using t1 – PrintTime ( t1 ); Not callable with noon – PrintTime (noon); Why the difference?

14 A better PrintTime Since PrintTime( ) doesn’t modify the Time object passed to it, the argument should be const void PrintTime (const Time& t) Doing so doesn’t change the code, but now does allow noon to use the function PrintTime (noon);

15 A subtlety What happens if ‘noon’ is passed to the new PrintTime ( ) function, but the accessors are not const?

16 Composition Sometimes objects are used as members of other objects. For example, an AlarmClock class may include a Time object. This is known as “composition” or “aggregation”. Member objects are constructed automatically A common form of code reuse.

17 AlarmClock class AlarmClock { public: AlarmClock ( ); void SetAlarm (int h, int m, int s);... private:... void ring( ) const; Time currentTime; const Time alarmTime; }

18 “this” and that Every object has access to it’s own address through a pointer called this. The this is implicitly used to reference both data members and member functions of an object… it can also be used explicitly. This is passed implicitly as the first argument of every (non-static) member function

19 A “this” example // should have error checking void Time::SetMinute (int m) { this->minute = m; } More valuable later

20 Dynamic Memory Allocation In C we used malloc int *intp; intp = (int *)malloc (sizeof(int) ); In C++ we use new int *intp; intp = new int; or intp = new int (7);

21 Allocating Dynamic Arrays Again, in C we used malloc: int *ip2; int *ip2 = malloc (50 * sizeof(int)); In C++ we use new and the size int *ip2 = new int[10];

22 Freeing Dynamic Memory In C we used free free (intp);// for a single int free (ip2);// or an array In C++ we use delete delete intp;// for a single int delete [ ] ip2;// for an array

23 A simple class that uses dynamic memory Class Array { public: Array (int size = 100); void putElement (int x); int getElement (void);.... private: int *theArray; }

24 Array Constructor Allocate the memory in the constructor Array::Array (int size) { theArray = new int [size]; }

25 The Array Destructor Free the memory in the destructor Array::~Array ( ) { delete [ ] theArray; }

26 Good Programming Practices Use const wherever possible Declare member functions that do not change the object as const Avoid friends Use member initialization lists Allocate dynamic memory only in the constructor Delete dynamic memory only in the destructor