- The first principle of Object Oriented programming is called
encapsulation
- An object is defined in terms of data and function members
- In fact, data abstraction principle says that an object should
be defined only in terms of operations. Data members are irrelevant.
That's the topic for COSC2006.
- An object in a C++ program is a variable
- Any variable is an object.
- In COSC 1046 you used objects of simple types such as int,
double, char
- We also looked at an object called array which was collection
of objects
- An Object has to have a type
- We dealt with simple types before, but in many cases these
simple predefined types are not sufficient
- Let us consider representation of time in your program
- Time is just a singular object which happens to be collection
of three different objects hour, minute and second
- We need a mechanism to define an object of the type time
- Since built-in types do not provide such a type, we need to
creat our own types: user-defined types
- Our user defined type is going to be a collection of three
different members/fields
hour
minute
second
- The objects are not static, they can have certain functionality.
For example, a variable/object of the type Time can print relevant
information about itself in military format of standard format
- That's why printMilitary() and printStandard() are its member
functions/operations
- You can send a message to the object of the type time to print
itself in the military format
Time t;
t.printMilitary();
- Figure 6.3 shows that a variable of the type Time has two
other members
- One is called setTime(int, int, int)
- t.setTime(13, 27, 6);
- The above call asks the variable t to set its data members:
hour will be set to 13, minute will be set to 27 and second will
be set to 6
- Quiz:
- Why do we need user-defined types?
- Most of the real world objects are too complex to be represented
by basic types such as int, char, double, etc. It may be possible
to represent them as a collection of objects of previously defined
types. Example is the type Time which is defined as a collection
of three integers. First one is called hour, second is called
minute and the third is called second.
- Why should user defined types have data as well as function
members?
- The objects in real world are not static, they are capable
of taking actions. Member functions allow us to send a message
to the object. The object will take an action based on the message.
For example, t.printMilitary() asks the object t to print information
about itself in the military format.