Introduction to Classes

Classes are central to object-oriented programming. In the simplest sense, a class is basically a structure with member functions as well as member data. Both structures and classes group related data together, but classes go further by grouping related functions with the data.

A class models a single entity -- one Car, one Person, one Book, one Bank.

Recall the DayOfYear structure.

struct DayOfYear { int month; int day; }; As a (very simple) class, consider this alternative definition. class DayOfYear { public: void Output( ); int m_month; int m_day; }; Ignoring the keyword public consider the code below //-------------------------------- // Savitch text display 6.3 // // Modified for CMSC 202 standards // //Program to demonstrate a very simple example of a class. //A better version of the class DayOfYear will be given in Display 6.4. // // Also note that a better (shorter) version of Output() is recommended //-------------------------------- #include <iostream> using namespace std; class DayOfYear { public: void Output( ); int m_month; int m_day; }; int main( ) { DayOfYear today, birthday; // ask user for today's date cout << "Enter today's date:\n"; cout << "Enter m_month as a number: "; cin >> today.m_month; cout << "Enter the m_day of the m_month: "; cin >> today.m_day; // ask user for their birthday cout << "Enter your birthday:\n"; cout << "Enter m_month as a number: "; cin >> birthday.m_month; cout << "Enter the m_day of the m_month: "; cin >> birthday.m_day; // echo user input cout << "today's date is "; today.Output( ); cout << endl; cout << "Your birthday is "; birthday.Output( ); cout << endl; // output special message if (today.m_month == birthday.m_month && today.m_day == birthday.m_day) cout << "Happy Birthday!\n"; else cout << "Happy Unbirthday!\n"; return 0; }

Some notes and observations

  1. The definition of DayOfYear defines the class.
  2. The variables today and birthday are instances of the class and each is called an object of type DayOfYear. Some folks (incorrectly) use the terms "class" and "object" interchangeably.
  3. The class members m_month and m_day are called data members
  4. The class member Output( ); is called a member function or method.
  5. Data members and methods of a class are specified using the dot operator.
  6. When a member function is defined, the definition must include the class name because two or more classes may have a method with the same name.
  7. Here's the definition of the method Output( ); for the DayOfYear class. //----------------------------------------------- // Output ( ) // // PreCondition: // 1 <= m_month <= 12 // PostConditions: // Month and Day are displayed to standard output //------------------------------------------------ void DayOfYear::Output( ) { switch (m_month) { case 1: cout << "January "; break; case 2: cout << "February "; break; case 3: cout << "March "; break; case 4: cout << "April "; break; case 5: cout << "May "; break; case 6: cout << "June "; break; case 7: cout << "July "; break; case 8: cout << "August "; break; case 9: cout << "September "; break; case 10: cout << "October "; break; case 11: cout << "November "; break; case 12: cout << "December "; break; default: cout << "Error in DayOfYear::output. Contact software vendor."; } cout << m_day; }
  8. Note that the code in the implementation of DayOfYear::Output( );, uses the data members m_month and m_day by themselves without first giving the object and using the dot operator. Member functions can directly reference the data members of a class.
  9. The operator :: is called the scope resolution operator as is used to associate the member function definition with the class.

Classes are full-fledged data types like int, float and double. As we've seen, you can have variables of a class type (an object), pass the them to functions, and a function can return a value of a class type. You can use a class just like any other type.

Very often in this course, you must play three different roles when writing code for a project

  1. The designer and implementer of a class
  2. The programmer who uses a class when writing code
  3. The user of the application who sits at a desk and types on the keyboard and looks at the screen
It's important that you be aware of these roles and which hat you are wearing when writing code.

Last Modified: Monday, 28-Aug-2006 10:15:53 EDT