• Articles
  • Xistenial's TUTS Classes and Objects Le
Published by
Aug 18, 2013 (last update: Aug 19, 2013)

Xistenial's TUTS Classes and Objects Lesson 1

Score: 3.5/5 (14 votes)
*****

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:

1
2
3
4
class classIdentifier
{
classMembersList
};


**A missing semicolon will result in a syntax error!

The members of a class are classified into 3 categories: private,public,protected and the fancy name for these 3 are member access specifiers.

Some facts about private & public 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:

1
2
3
int hr;
int min;
int sec;


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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class clockType
{
public:
void setTime(int,int,int);
void getTime(int&,int&,int&) const;
void printTime() const;
void IncrementSeconds();
void IncrementMinutes();
void IncrementHours();
bool equalTime(const clockType&) const;

private:
int hr;
int min;
int sec;
};
 


(*)--The word const specifies that these functions cannot modify the member variables of a variable of type class clockType.


Object Declaration


Until now we have been using the word class variable, what this is in , technical c++ terms, is an object.

syntax for declaring a class object

clockType myClock; //Declaring an object, myClock

Accessing Class Members


syntax 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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Declaring our objects
clockType myClock; 
clockType yourClock; 

//Accessing

myClock.setTime(5,2,30)// values 5,2,30 passed as parameters to function setTime
myClock.printTime();
yourClock.setTime(x,y,z); //x,y,z are variables of type int.

if(myClock.equalTime(yourClock))
.
.
//Some code in here
.
.

//Illegal Statements
myClock.hr =10;
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.
</.X.>