Class 15 Object-Oriented Development from Structs to Classes
The difference between structs and classes A class in C++ is basically the same thing as a struct The following are exactly equivalent struct Time int hour, min; ; The members of a struct are public by default. This means that they can be manipulated outside the struct class Time public: int hour, min; ; The members of a class are private by default. This means that they can t be used outside the class. If you want to use them outside the class, you need to use the public access specifier.
The difference between structs and classes: how they are used Structs are used to encapsulate (bundle) data advantage: fewer arguments to pass to functions Classes tend to have data as well as functions The functions allow the user to manipulate the data They protect the data from inappropriate manipulation
Example: Insecure data in a struct struct Time int hour, min; ; int main() Time t; cin >> t.hour >> t.min; anything could be added resulting in an invalid Time object t.min++; this would take a valid minute like 59, and change it to 60, which is invalid a class can protect the data so that this won t happen
Adding a member function to protect the data class Time public: void increment(); private: int hour, min; ; member function -This function is public and can be accessed outside the class. - Declaration is in the class declaration -It will add one minute to the time member data -The data is kept private so that only the class member functions can change it
Calling a member function When you call a function in a class, first you must create an object from this class: Time t; Then you can access the function using the dot operator: t.increment()
The implicit argument t.increment(); When you call a function like this, t is called the implicit argument of the function It is as though t was passed by reference. i.e. the increment function will be able to change t and its data members (t.hour and t.min)
Defining the function The declaration of the function is found within the class. The definition of the function is elsewhere in the code (possibly below main) It needs to use the scope resolution operator :: to indicate the class it belongs to: void Time::increment()...
The definition void Time::increment() if ( min < 59 ) min++; The definition of the function starts with return_type class_name::function_name( parameters) else min = 0; hour++; if ( hour == 24 ) hour = 0; In this function, min belongs to an unknown Time object. If you called the function with t.increment() then the min in the function is t.min
Initializing Data Members The data members of a class are private by default int main() Time t; t.hour = 1; causes compiler error!
Initializing class members using the constructor The constructor is a function with a few unusual qualities its name is the same as the class name it has no return type it gets called whenever you declare an object of the class
Example class Time public: Time(); // Declaration of default constructor: 0 parameters // Will be called whenever a Time object is created with // no arguments Time( int h, int m ); private: ; int hour, min; // Declaration of a constructor with 2 parameters: // hour will be set to h, min will be set to m
Definitions of constructors Time::Time() hour = 0; min = 0; Time :: Time( int h, int m ) hour = h; min = m; These are located outside of the class, like function delcarations
Calling the constructors int main() Time t; calls default constructor Time t2( 1, 30 ); calls second constructor Time tarray[2]; calls default constructor twice ; What happens in memory when this code is executed?
Example: Point Class Write a class Point with two private data members x and y, and two constructors (one default, and the other has two parameters which set the values of x and y) The class will have one function which calculates the distance of the Point from the origin. The main function will get x and y values for a Point from the user, construct the Point, and display the length of the Point from the origin.