February 8, 2002 Reading assignment: Deitel and Deitel Chapter 6.5-6.10, 6.14, 6.16 -or- Wang 3.5, 3.15, 5.1-5.2 Classes - Like a super-struct, because you can store data and functions in one place. - Implementation of class usually goes in a separate file and is compiled separately All information that the client needs is stored in a header file (classname.h) - Example class Ratnum { public: Ratnum(); // no-args constructor Ratnum(int); // integer initialization Ratnum(int, int); // give numberator and denominator unsigned int get_denominator(); int get_numerator(); void set_ratnum(int, int); void display(); double convert_to_double(); private: unsigned int denominator; // Not 0 int numerator; int gcd(int, int); }; - After this declaration, Ratnum is a new type, and variables of this type can be defined. Variables of a class are called objects. - The class has data members and member functions. Usually we make the member functions public and the data members private. Some people call the data members "attributes" and the member functions "behaviors". - Clients of a class have access only to the public members. The implementation itself has access to all members. - Every class must have a member with the same name as the class. This member is called a constructor, and it is called when an object is defined. Very often this function is overloaded, so that the object can be defined and initialized in a variety of ways. - The example above defines an "interface", which separates the clients from the class. This is what would be stored in the header file: ratnum.h - We have to do two things yet: write the programs for implementing the class, and write a client program that uses the class. Implementation of class: ratnum.cpp - This file #includes ratnum.h, and then defines (implements) each of the member functions. Any member function automatically has access to the private data. - Use the binary scope resolution operator :: with the name of the class to indicate that you are implementing a member function of that class - The file ratnum.cpp can be compiled separately, producing the object file ratnum.obj (or ratnum.o on some systems). This file can be distributed along with ratnum.h to clients of the class (people who will write programs that use the class). This way they can use the code without having access to the source file. Clients of class need not have information about the implementation. Testing/using the class: testrat.cpp - Here we also include the header file ratnum.h, but we include it not so we can implement it, but rather so that we can define and access variables that are Ratnum objects. - We can define a Ratnum object with Ratnum r1; // no-args constructor is called Ratnum r2(3); // The version of the constructor that has one argument is called Ratnum r2(0,1); // The version of the constructor that has two arguments is called Explicit type casting (type conversion) - Note that in the function Ratnum::convert_to_double() we forced a conversion of type. This allowed us to avoid integer division and to produce a double result.