Quick Review of Structs
In C, a struct is used to group
pieces of data into a single entity.
int month; /* 1 = Jan ... 12 = Dec */
Dot notation is used to access members:
july4th.month = 7;
july4th.day = 4;
Problem: no data hiding.
- A class models a single entity -- one Car, one Person,
- A class type definition:
void Output( );
}; // don't forget the ;
- DayofYear is a class. It is a new data type.
- today and birthday are instances
of the class and each is called an object of type
- People often incorrectly use the terms class and
- The class members m_month and m_day are
- The class member Output( ) is called a member function or method.
Some Notes on Syntax
- Several classes may have the same member name.
- Data members and methods of an object are specified using the dot
operator: today.m_month, today.Output().
- Code in a member function can directly reference the data
members of the same class without using a dot.
- The scope resolution operator ::
is used to associate a member with a class.
- When a member function is defined, the definition must include
the class name: DayOfYear::Output().
Objects are "First-Class"
- Classes have same standing as built-in types like
int, float and double.
- You can declare objects (variables) of a class type.
- You can pass objects to functions.
- The return value of a function can be an object.
- You can implement type-converters to automatically
convert objects from one class to another.
- Rules for resolving overloaded functions apply
to function calls with objects as arguments.
Notes on File Organization
- Use separate files for class definitions and implementations.
Name the files after the class name:
- Code that need a class must include its header file:
- The header file must be guarded, to prevent it being read twice.