 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer 2003 1 Summary of Topics Related to Classes Class definition Defining member.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved Oggetti const e funzioni membro const 2. Composizione: oggetti come membri di classi 3. Funzioni friend.
Advertisements

Operator overloading redefine the operations of operators
C++ Classes & Data Abstraction
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction 17.2 const (Constant) Objects and const Member.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
More on Objects CS 102 More, more, more on Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 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 Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 11.
Chapter 15 C++ Function By C. Shing ITEC Dept Radford University.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 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.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
 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 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Jan 20, 2004.
CompSci 230 S Programming Techniques
Access Functions and Friend Functions
Lecture 5: Classes (continued) Feb 1, 2005
Chapter 16: Classes and Data Abstraction
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
C++ Classes & Object Oriented Programming
Classes: A Deeper Look Outline
Classes and Data Abstraction
C++ Classes and Objects
7.4 friend Functions and friend Classes
#include <iostream.h>
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Chapter 6: Classes and Data Abstraction
Classes Member Qualifiers
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member functions outside Class definition Defining member functions inside Class definition (sometimes used for small functions) Accessing Class members Constant ( const ) objects and functions Initializing const data members friend functions static members The this pointer

2 Basic Class definition example 1 class Time { 2 3 public: 4 Time(); // constructor 5 void setTime( int, int, int ); // set hour, minute, second 6 void printUniversal(); // print universal-time format 7 void printStandard(); // print standard-time format 8 9 private: 10 int hour; // (24-hour clock format) 11 int minute; // int second; // }; // end class Time Definition of class begins with keyword class. Class body starts with left brace. Class body ends with right brace. Definition terminates with semicolon. Function prototypes for public member functions. private data members accessible only to member functions.

3 14 class Time { public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format 20 void printStandard(); // print standard-time format private: 23 int hour; // (24-hour clock format) 24 int minute; // int second; // }; // end class Time Time::Time() 30 { 31 hour = minute = second = 0; } // end Time constructor void Time::setTime( int h, int m, int s ) 36 { 37 hour = ( h >= 0 && h < 24 ) ? h : 0; 38 minute = ( m >= 0 && m < 60 ) ? m : 0; 39 second = ( s >= 0 && s < 60 ) ? s : 0; } // end function setTime... Defining member functions outside class definition ReturnType ClassName::MemberFunctionName( ) { … } Each class must have a constructor function (same name as class): initializes data members of a class object when called

4 14 class Time { public: 17 Time(); // constructor 18 void setTime( int h, int m, int s ){ 19 hour = ( h >= 0 && h < 24 ) ? h : 0; 20 minute = ( m >= 0 && m < 60 ) ? m : 0; 21 second = ( s >= 0 && s < 60 ) ? s : 0; 22 } // end function setTime void printUniversal(); // print universal-time format 25 void printStandard(); // print standard-time format private: 28 int hour; // (24-hour clock format) 29 int minute; // int second; // }; // end class Time... Defining member functions inside Class definition (sometimes used for small functions)

5 66 int main() 67 { 68 Time t; // instantiate object t of class Time t.setTime( 13, 27, 6 ); // change time return 0; } // end main Set data members using public member function. Accessing Class members object.memberfunction; OR objectPtr = &object; objectPtr->memberfunction; same as (*objectPtr ).memberfunction

6 8 class Time { 9 public: 10 Time( int = 0, int = 0, int = 0 ); // default constructor 11 void setTime( int, int, int ); // set time 12 void printUniversal() const; // print universal time private: 15 int hour; // (24-hour clock format) 16 int minute; // int second; // }; // end class Time void Time::setTime( int hour, int minute, int second ) 21 { setHour( hour ); 22 setMinute( minute ); 23 setSecond( second ); } // end function setTime void Time::printUniversal() const 26 { cout << setfill( '0' ) << setw( 2 ) << hour << ":" 27 << setw( 2 ) << minute << ":" 28 << setw( 2 ) << second; } // end function printUniversal int main() 31 { 32 Time wakeUp( 6, 45, 0 ); // non-constant object 33 const Time noon( 12, 0, 0 ); // constant object 34 noon.printUniversal(); // 35 return 0; 36 } // end main Constant ( const ) objects and functions Member functions for const objects must also be const const member functions cannot modify object Specify const in both prototype and definition

7 fig07_04.cpp (1 of 3) 10 class Increment { 11 public: 12 Increment( int c = 0, int i = 1 ); // default constructor 13 void addIncrement() 14 { 15 count += increment; } // end function addIncrement 16 void print() const; // prints count and increment 17 private: 18 int count; 19 const int increment; // const data member 20 }; // end class Increment Increment::Increment( int c, int i ) 23 : count( c ), //initializer for non-const data 24 increment( i )//required initializer for const data 25 { 26 // empty body 27 } // end Increment constructor void Increment::print() const 30 { 31 cout << ", increment = " << increment << endl; } // end function print Initializing const data members (data) Member initializer syntax Can be used for all data members Must be used for const data members Member initializer list separated from parameter list by colon. Member initializer syntax can be used for non- const data member count. Member initializer syntax must be used for const data member increment. Member initializer consists of data member name ( increment ) followed by parentheses containing initial value ( i ). Note that the member initializer list is separated by commas, and the list executes before the body of the constructor.

8 20 class Count { 21 friend void setX( Count &, int ); // friend declaration 22 public: 23 void print() const 24 { 25 cout << x << endl; } // end function print private: 30 int x; // data member 31 }; // end class Count void setX( Count &c, int val ) 34 { 35 c.x = val; // legal: setX is a friend of Count 36 } // end function setX int main() 39 { 40 Count counter; // create Count object counter.print(); setX( counter, 8 ); // set x with a friend counter.print(); 47 return 0; 48 } // end main Use friend function to access and modify private data member x. setX is a function; it is not part of the class Count. Pass Count object since C-style, standalone function. friend functions function defined outside class’s scope but has right to access non-public members Precede function prototype with keyword friend is class definition c is an object with data member x.

9 10 class Employee { 11 public: 12 const char *getLastName() const; 13 static int getCount(); private: 16 char *firstName; 17 char *lastName; 18 static int count; 19 }; int Employee::count = 0; int Employee::getCount() 24 { 25 return count; 26 } int main() 29 { 30 cout << Employee::getCount() << endl; return 0; } // end main static member function can only access static data members and member functions. Initialize static data member exactly once at file scope. static member function accesses static data member count. static members static data is “Class-wide” data; property of class, not specific object of class private static variables Can only be accessed via public static member function class::memberfunction public static variables class::object

10 8 class Test { 9 10 public: 11 void print() const; private: 14 int x; }; // end class Test void Test::print() const 19 { 20 // implicitly use this pointer to access member x 21 cout << " x = " << x; // explicitly use this pointer to access member x 24 cout x = " x; // explicitly use dereferenced this pointer and 27 // the dot operator to access member x 28 cout << "\n(*this).x = " << ( *this ).x << endl; } // end function print int main() 33 { 34 Test testObject( 12 ); 35 testObject.print(); 36 return 0; 37 } The this pointer Allows object to access own address Implicit argument to non-static member function call Implicitly reference member data and functions