An object consists of:
Class -- a blueprint for objects. A class is a user-defined type that describes what a certain type of object will look like. A class description consists of a declaration and a definition. Usually these pieces are split into separate files.
An object is a single instance of a class. You can create many objects from the same class type.
Define - A definition usually consists of the implementation details. This part doesn't need to be seen by the user of the interface. A function definition is the code that makes a function work (the function body). A class definition consists definitions of its members.
Use - The use of an item, through its interface. The user of an executable program uses the graphic interface, keyboard, and mouse. The user of a function is a programmer, who makes calls to the function (without needing to know the implementation details). The user of a class is also a programmer, who uses the class by creating objects and calling the available functions for those objects.
Note -- The concept of interface is a very important one in object-oriented
programming. The interface is what the user sees. We
often leave the implementation details hidden. We want to strive
to create a clear interface for the user (not necessarily the end user
of a program -- could be a programmer, i.e. the user of a class).
The public section of a class is essentially the interface of the object. The user of an object is some other portion of code (other classes, functions, main program). So, objects are used by programmers, and we want the interface to be as simple as possible. This usually means providing functions in the public area that handle all of the necessary actions on the object.
Although there is no set rule on what is made public and what is made private, the standard practice is to protect member data of a class by making it private. If there are functions that do not need to be part of the interface, then these can be private, as well.
Reasons for data hiding:
class <className> { public: (public member data and functions go here) private: (private member data and functions go here) };
A constructor is easy to recognize because:
Example: Circle class with constructor
A constructor is a function, and you can define it to do anything you want. However, you do not explicitly call the constructor function. It is automatically called when you declare an object. Example: Look at the following declaraion.
Circles circ1;
This declaration creates an object named circ1. This line
of code also causes the Circles contructor function to be run for the circ1
object.
Note the syntax in the frac.cpp file for defining functions.
void Fraction::Show() { cout << numerator << '/' << denominator; }
The declaration for this function in the class header was:
void Show();
When we write the function definition outside of the class declaration block (as we are doing here, in the file frac.cpp), we must refer back to the class that the function was declared in. This is the reason for the class name Fraction in the function definition header:
void Fraction::Show()
The double colon :: is called the scope resolution operator, and it is used for specifying which class a member belongs to when we define it separately from its declaration inside the class. The format for it's use is:
className::memberName
Also note the syntax used in calling member functions of an object from the main program file. We have declared fraction objects:
Fraction f1, f2;
To call a member function of an existing object, we use the dot-operator. The syntax format is:
objectName.memberName
memberName can be a member function call or a member data item
Examples:
f1.Show(); // f1 is the object, Show() is the member function cout << f2.Evaluate(); // f2 is the object, Evaluate() is the member function
Fraction(); // default constructor Fraction(int n, int d = 1); // constructor with parameters
The term default constructor will always refer to a constructor with no parameters. When we declared objects in the simple fraction example, its default constructor was used because no parameters were passed. Example:
Fraction f1, f2; // f1 and f2 are now Fraction objects
To utilize a constructor with parameters, we can pass arguments in when the object is declared. Examples:
Fraction f1(2,3); // passes 2 and 3 as the parameters (2/3) int x = 4, y = 8; Fraction f2(x,y); // passes in the values stored in x and y
Notice, also, that this constructor has a default value on the second parameter, which makes it an optional parameter. If only one argument is passed in, the second parameter takes the default value, 1.
Fraction f3(6); // initializes a fraction to 6/1