Classnote02-1x2

Classnote02-1x2 - C+ Primer CSE 250 Lecture Note 02 C+...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Primer CSE 250 Lecture Note 02 C++ Primer (2) Adapted from the Koffman-Wolfgang C++ Primer, from the Instructor Materials slides by Elliot Koffman and Paul Wolfgang. This material is used by permission of John Wiley & Sons, Inc For private use only by students in CSE250 Re-distribution or public posting in any form is expressly forbidden. c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 1 / 193 Defining C++ Classes A C++ program is a collection of functions and classes . A class represents a set of objects that have common properties. A class is a template for creating objects. A class represents a type . Type determines the set of values an object may have. Type determines the operations that can be performed on those values. In C++ there are two kinds of types: Primitive or build-in types User Defined or class types c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 4 / 193
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class Definition A class consists of members Data members also called data fields or attributes Member functions also called operators, functions or methods Data members are also sometimes called instance variables because each object (instance of a class) contains them. Data members may be either primitive or class types. c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 6 / 193 Public versus Private Class members that are declared in the public section of a class definition are accessible to all functions (inside or outside) the class. Class members that are declared in the private section of a class definition are accessible only to functions that are members of the class. Generally we want the operators (member functions) visible to the users of the class. Thus they are declared public. Generally we want to keep the implementation details (data members) hidden from the users of the class Thus they are declared private. c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 8 / 193
Background image of page 2
Example: Clock.h #ifndef CLOCK_H_ #define CLOCK_H_ / ** The class Clock represents the time of day. * / class Clock { public: // The interface is defined here / ** Set the clock. * / void set_clock(int hr, int min, int sec); / ** Get the current hour. * / int get_hours() const; / ** Get the current minute. * / int get_minutes() const; / ** Get the current second. * / int get_seconds() const; / ** Advance the clock by one second. * / void tick(); private: // The implementation details are defined here }; #endif c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 10 / 193 Conditional Compilation Forms: #ifdef macro-name code to be compiled if macro-name is defined #else code to be compiled if macro-name is not defined #endif or #ifndef macro-name code to be compiled if macro-name is not defined #else code to be compiled if macro-name is defined #endif c ± Xin He (University at Buffalo) CSE 250 Data Structures Classnote 02 12 / 193
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Preventing Multiple Includes A header file may be included by another header file.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 48

Classnote02-1x2 - C+ Primer CSE 250 Lecture Note 02 C+...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online