Classes and Objects
Picture a kitchen...Now in this kitchen you will have various appliances (Stove, Fridge,Microwave, kettle etc..). These appliances are what makes a kitchen, a kitchen, without them it would just be another room. C++ works in pretty much the same way. The Kitchen being your class and all the other Appliances as your members of the class.
The general syntax
for defining a class is:
**A missing semicolon will result in a syntax error!
The members of a class are classified into 3 categories: private
and the fancy name for these 3 are member access specifiers
Some facts about private
members of a class:
(*)-- By default all members are declared private.
(*)-- If a member is private
, you cannot access it outside of the class(Example 1 will illustrate this)
(*)-- A public
member is accessible outside of a class (Example 1 will illustrate this)
How to know when to choose public and private?
Suppose we wish to define a class to implement the time of day in a program. Lets call the this class clockType
. Further we use 3 variables as follows:
We also wish to perform these functions:
1. set time
2. retrieve time
3. print time //well we need to read it yea?
4. increment time by 1 sec
5. increment time by 1 min
6.increment time by 1 hr
7. Compare two times for equality
From above we can see class clockType
has 10 member variables.
Ok you have come this far but now what? How does this help with the private and public dilemma.. Deciding which members to make public or private depends on the nature of the member. The rule of thumb is,
*If a member needs to be accessed outside a class, declare it public.
*Any member that should not be accessed directly by the user should be declared private.
For example the user should be able to set the time and print the time, So they should be public. Also members to increment time and compare time should be declared public.To prevent direct manipulation of member variables hr,min,sec we will make them private.
**Keep in mind that if the user does have direct access to these, why would we need function setTime?
Your code should now look like this:
void getTime(int&,int&,int&) const;
void printTime() const;
bool equalTime(const clockType&) const;
(*)--The word const
specifies that these functions cannot modify the member variables of a variable of type class clockType.
Until now we have been using the word class variable, what this is in , technical c++ terms, is an object
for declaring a class object
clockType myClock; //Declaring an object, myClock
Accessing Class Members
for an object to access a member of a class is
classObjectName.memberName // the .(period) is known as a dot operator
Ok all these words are pretty much boring,I know, you want code not read through an essay (thats why you choose c++ not latin) but its important to keep up with the jargon used because thats where most people hit the wall. Ok so enough talk for now lets display some code.
Example 1 (Code segment)
This following piece of code will show you how accessing works..
//Declaring our objects
myClock.setTime(5,2,30)// values 5,2,30 passed as parameters to function setTime
yourClock.setTime(x,y,z); //x,y,z are variables of type int.
//Some code in here
myClock.min = yourClock.min;
/*Illegal because hr and min are private variables and cannot be accessed by the objects*/
Thats it for this tutorial let all that stuff sink in. Next we tackle implementing Member Functions and a full example of the clock source code with an explanation for each step I took.