Understanding Object and Type/Class - C++ (Part 1)
Quote:
What is an `object` in an `object oriented program`?
A: An `object` is a variable which stores information. We can also
use this variable to retrieve previous information and add new
information from/to its own store, i.e. it can be asked to perform
operations on itself.
Interaction in an object oriented program is achieved by an object
sending requests to call the functions of other objects for retrieving
or adding/editing information from the other object's store.
Quote:
What is an object's `type`/`class`?
A: Every object has a type associated with it. Type represents what
kind of information/properties can that object hold. Type is also
known as a `class`.
Example:
Type/Class of `dogs`.
If our object is `dog`, then its type/class may contain the
information about his voice, eating habits, habitat, breed etc.
The requests that can be made to an object are tied to its type/class.
That is we can't query a `dog` object about its `flying` speed. The
`dog` object does not contain the property of `flying`.
Quote:
What is the purpose of `hiding` some contents of a type/class?
A: A type/class is created by the type/class programmers for the
client programmers. The intention is to provide the client
programmers a toolbox full of interfaces (classes/types) which they
can use for their own application development.
The goal of the type/class creator is to build a type/class that
exposes only what’s necessary to the client programmer and keeps
everything else hidden.
The reason is that if the information/function is hidden, the client
programmer can’t access or edit it, which means that the type/class
creator can change the hidden portion at will without worrying about
the impact to anyone else. Also, hiding the implementation reduces
program bugs.
Example:
Let there be a function of a type/class which outputs the numbers
given in a sorted order. The client programmer is supposed to input
the numbers and receive the results. What algorithm goes behind
the number sorting function should not be any of his business.
This kind of background processing/information should be hidden so
that it doesn't get corrupted by a careless or uninformed client
programmer anyhow.
Secondly, if tomorrow the type/class programmer decides that the
bubble sorting algorithm be changed to quick sorting algorithm, the
client programmer won't get affected by this change at all because
the algorithm wasn't visible to him in the first place itself.
C++ uses three keywords to set the boundaries in a type/class:
- public
- private
- protected
These access specifiers determine who can use the data/functions
that follow.
- `public` means the following data/functions are available to everyone.
- `private` means that no one can access those definitions except you,
the creator of the type/class, and other member functions of that
type/class. - `protected` acts just like private, with the exception that an
inheriting class has the access to protected members, but not the
private members.
Quote:
How to reuse a type/class?
need to reinvent the wheel by writing the same type/class, again, for
some different purpose. The existing class should be reused.
Ways to reuse a type/class:
- Use an object of that type/class directly.
- Place an object of that type/class inside a new type/class.
type(s)/class(es), this concept is called `composition`.
`Composition` is often referred to as a `has-a` relationship, as in `a
car has an engine`.
Reference:
Thinking in C++ (Vol 1) - Bruce Eckel
Total Comments 1
Comments
-
Updated the blog with some sensible (IMO) information.Posted 11-29-2012 at 03:15 AM by Anisha Kaul




