CSI 201 - Introduction to Computer Science
Chapter 6 Defining classes Brian R. King Instructor
3/8/2006 CSI 201 -- Chapter 06 2
Review
- An object is a special variable that has data as well as member functions
that act on that data.
- A class is a type definition that gives us a blueprint for creating objects of
that type.
- Example classes we've seen:
- stream
- fstream
- istream
- ifstream
- An object is a variable whose type is a class.
- We say an object is an instance of a class.
- Example objects we've seen:
- cin
- cout
- Goal of Chapter 6 - Learn how to define our own class
- The complete class definition will define:
- What kind of data the object can hold
- What functions should exist to operate on that data.
3/8/2006 CSI 201 -- Chapter 06 3
Structures
A structure (or struct) is a simplified class, and
makes a good introduction to exploring classes.
Structs allow us to define new data types that are
am amalgam of other data types.
A good analogy of a struct is a record of a database.
A student record may include fields such as:
- Name
- Student ID
- GPA
- Status (1=freshman, … , 4 = senior, 5 graduate)
Structs are sometimes referred to as records in
some texts.
3/8/2006 CSI 201 -- Chapter 06 4
Struct definition syntax
struct Identifier
{ variable_declaration_1; variable_declaration_2; … variable_declaration_n; };
Identifier names the structure type. We call this the structure
tag.
It is an industry standard to have the first letter of a class or struct
name be a CAPITAL letter (This is not a C++ syntax requirement.)
Each one of the variable declarations between the braces are
referred to as the members of the struct, also called member names or member fields.
The structure definition ends with a semi-colon. The structure tag becomes a new data type for your program.