C++ Lecture 5 Monday, 18 July 2005. Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.

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

Introduction to Programming Lecture 39. Copy Constructor.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction 17.2 const (Constant) Objects and const Member.
Class and Objects.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Classes: A Deeper Look Systems Programming.
Classes and Objects. const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects permissions they need, no more.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 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.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
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 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.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
Programming Techniques Classes II Important Class Features Spring 2009.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 7.1Introduction 7.2const (Constant) Objects and const Member Functions 7.3Composition: Objects.
 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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Lecture 5: Classes (continued) Feb 1, 2005
CSC241: Object Oriented Programming
Computer Science Department University of California, Davis
CISC181 Introduction to Computer Science Dr
Chapter 17 - C++ Classes: Part II
Chapter 17 - C++ Classes: Part II
Chapter 7: Classes Part II
Chapter 7: Classes Part II
Classes: Part 2.
Lecture 4: Classes (continued) June 14, 2004
Chapter 7: Classes Part II
7.4 friend Functions and friend Classes
Chapter 7: Classes Part II
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Chapter 7: Classes Part II
Presentation transcript:

C++ Lecture 5 Monday, 18 July 2005

Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes l friend functions and friend classes l “this” pointer l Dynamic memory allocation

Const Objects Time wakeup(6, 45, 0); // non-const const Time noon(12, 0, 0) // const l A const object can not change its value; a const object is initialized once (by its constructor). l A const object can not call a member function, unless the member function is also declared a const.

Const Member Function void Time::getHour( ) const { return hour; } l A const function cannot modify the object. C.f

Time Class Example, class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void setTime( int, int, int ); // set time void setHour( int ); // set hour … // get functions (normally declared const) int getHour() const; // return hour … // print functions (normally declared const) void printUniversal() const; // print universal time void printStandard(); // print standard time private: int hour; // (24-hour clock format) … }; // end class Time

Software Engineering Principles l Principle of least privilege: If objects or variables do not change, or functions that do not modify objects, declare them as const.

Composition: Objects as Members of Classes class Employee { public: … private: char firstName[25]; char lastName[25]; const Date birthDate; const Date hireDate; };

Constructor for composition Employee(const char *, const char *, const Date &, const Date &); How to initialize the objects inside a class like birthDate and hireDate?

Composition l Example Fig l Date class and implementation l Employee class and implementation l main( ) function

Friend Functions and Classes l A friend function of a class is defined outside that class's scope, yet has the right to access private members of the class. l Friendship is neither symmetric nor transitive.

Declare a class as Friend class ClassA { friend class ClassB; … }

Declare a function as friend class Count { friend void setX(Count &, int); public: Count() {x = 0;} void print( ); private: int x; }; C.f. Fig

"this" Pointer l Every object has access to its own address through a pointer called “this”. I.e., “this” is a pointer pointing to the object itself. C.f. Fig.7.13

Cascading member function calls using "this" Class Time { public: Time &setTime( …) } // SetTime() returns a reference t.setHour(18).setMinute(30). … C.f. Fig

Dynamic Memory Allocation l The "new" operator allocate memory; the "delete" operator free memory TypeName *typeNamePtr; typeNamePtr = new TypeName; delete typeNamePtr;

Allocate an Array int *arrayPtr = new int [10]; delete [ ] arrayptr; l new automatically invokes the constructor and delete automatically invokes the class destructor

Static Class Members l If a data member is declared as static, only one copy exists and is shared by all the instances of the class. class Employee { … static int count; }

Public and Private Static Class Members l Public static: accessible through any object of that class. l Private static: accessible only through public member functions. l Static member function cannot access nonstatic data/function. C.f. Fig

Problem 1 l Why the following code segment is a programming error? #include int main( ) { char *p, s[ ] = “source”; strcat(p,s);

Problem 2 l What are printed by cout? char s1[50] = “jack”; char s3[50], s2[50] = “jill”; cout << strcpy(s3,s2) << endl; cout <<strcat(strcat(strcpy(s3,s1), “and ” ), s2) << endl; cout << strlen(s1) + strlen(s2) << endl; cout << strlen(s3) << endl;

Problem 3, Find error class Example { public: Example(int y = 10) : data(y) { } int getIncData() const { return ++data; } static int getCount( ) { cout << “Data is ” << data << endl; return count; } private: int data; static int count; }