INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS

Home                  ABOUT US                    CONTACT US                  TUTORIAL

CHAPTER 8

OBJECT AND CLASS:  EVERYTHING AS AN OBJECT OF A CLASS

 

The world around us is made of objects and each object has its own characteristics as to what it does and what can be done to it. A group of similar objects can be generated from a model (blueprint) that encompasses essential characteristics such as its data and its functionality, which is known as class.  A class bundles the data, its functions, and how these two members interact with each other as well as the outside world. Introduction of class and its objects will shift the focus from Procedural Programming (with function in center) to Object Oriented Programming where security (access), extendibility (expansion), and reusability (cost) are the major concerns.  As a matter of fact, you have already dealt with class and object.  The simple cin and cout statements are objects of the iostream class.

 

 

HOW TO MAKE A CLASS 

 


There are two ways to make a class, one that starts with the keyword struct and one that starts with the keyword class itself.  The following examples of employee class describe the similarities and differences between when the keyword class or the keyword struct is used. Note that the only difference between struct and class in C++ is the default accessibility of the members. By default members in struct are public, whereas members in class are private. However, you may argue that to define a class it would be easier to simply use the keyword class instead of using struct as the alternative, thus eliminating much confusion. C++ has demonstrated its loyalty to C language by extending the existing struct. A C++ programmer prefers class versus struct since the default accessibility of the members of a class is private.

 

   Figure 8.1a – A class with keyword struct.                Figure 8.1b – A class equivalent to                  

   Members are public by default.                                struct in figure 8.1a

                                                                         

 

 

 

 

 

Text Box: class employee {
char name[20];
int age;
double salary;  
void setdata(char *, int, double);
void displaydata(char*, int, double);
};
 
 

Text Box: struct employee {                 
private: char name[20];
int age;
double salary;
void setdata(char *, int, double);
void displaydata(char*, int, double);
};
 
 

Figure 8.2a – A struct. Members are declared                 Figure 8.2b – A class equivalent to

private with the key word private.                                   struct in figure 8.2

 

 

 

GENERAL SYNTAX OF CLASS AND ITS OBJECT

The following illustrates the general syntax for a class declaration and how to create an object from the class.

 

Text Box: Figure 8.3 – The general syntax for a class declaration.

Text Box: class classname {
 
accessspecifier:  memberdata;
accessspecifier:  functionmembers();
}; 
 
classname  objectname;


 
  

 

 

 

 


 

The following program will create an object called center, which is initialized and then is displayed. Let’s note that an object encompasses its data and its functions.

 

 

 

 

 

 

 

 

 

 

Text Box: #include<iostream>
using namespace std;
class point {
public: int x;
int  y;
public: void displayxy(){cout <<"X IS = "<<x<<" Y IS = "<<y<<endl;}
};
void main(){    
point center;
center.x=5;
center.y=6;
center.displayxy();    }//MAIN
 
 

 

 


 

 

                                                                                                                     

 

 

 

DATA MEMBERS AND MEMBER FUNCTIONS

A class consists of data (e.g. variables) known as data members that represent the attribute of class. In addition a class consists of functions (methods) known as member functions that represent the action or operation of the class. 

Text Box: #include<iostream>
using namespace std;
class point {
public: int x;
int  y;
public: void displayxy(){cout <<"X  IS = "<<x<<" Y  IS  = "<<y<<endl; }
            void movexy(int mx,int my){ x=x+mx; y=y + my;}
};
void main(){
            point corner;
corner.x=2;
corner.y=4;
corner.displayxy();
corner.movexy(10,20);
corner.displayxy();    }//MAIN
 
Text Box: Figure 8.5a – A program showing a class with its data members and member functions
Text Box: X IS = 2 Y IS = 4
X IS = 12 Y IS = 24

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Text Box: Figure 8.5b – Output of the program in figure 8.5a

  

 


 

OBJECTS DO NOT MIX

For every object that is created from class it will have its own data member values and its own functions.  In the following example, data member x and y of object center are different from data members x and y of object corner

Text Box: #include<iostream>
using namespace std;
class point {
public: int x;
int  y;
public: void displayxy(){cout <<"X  IS ="<<x<<" Y  IS = "<<y<<endl; }
            void movexy(int mx,int my){ x=x+mx; y=y + my;}
};
void main(){
             point center,corner;
center.x=5;
center.y=6;
corner.x=2;
corner.y=4;
center.displayxy();
corner.displayxy();
corner.movexy(10,20);
center.movexy(100,200);
corner.displayxy();
center.displayxy();        }//MAIN
 
 
Text Box: X IS = 5 Y IS = 6
X IS = 2 Y IS = 4
X IS = 12 Y = 24
X IS = 105 Y = 206
Text Box: Figure 8.6b – Output of the program in figure 8.6a
Text Box: Figure 8.6a - A program showing more than one object created from the same class.

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

CLASS ACCESSIBILITY: INFORMATION HIDING

One idea behind the class is the access ability of its members (data and function members). A class may want to hide its information detail (implementation detail) from others by allowing only certain members (interface) to access it. There are three keywords that define the accessibility of a class: public, private and protected.

 

Text Box: class employee {
private: char name[20];
 int age;
 double salary;
public:   void setdata(char *, int, double);
  void displaydata(char*, int, double);
};

  

 

 

 

 

 

 

 

Text Box: Figure 8.7a – A simple class with the use of key word private

  

 

 


 

In the above class example since default accessibility is private there would be no need to place the keyword private before the data members. This is shown in below example.

 

Text Box: class employee {
char name[20];
int age;
double salary;
public:  void setdata(char *, int, double);
 void displaydata(char*, int, double);
};
 

  

 

 

 

 


 

                                                                                                

 

 

 

Text Box: Figure 8.7b – A simple class without the use of keyword private

  

 

 

 


 

WHAT SHOULD BE PRIVATE AND WHAT SHOULD BE PUBLIC?

In a class the data is usually set to private and the functions are set to public. This way data cannot be accessed directly from outside of class preventing unintentional change of data. However, from the outside class (e.g. main program) data can be accessed or changed by the member functions that are set to public.

Text Box: #include<iostream>
using namespace std;
class point {
private: int x;
 int  y;
public: void setxy(int sx, int sy){x=sx; y=sy; } 
void displayxy(){cout <<"X is "<<x<<" Y is "<<y<<endl; }
};
void main(){ 
point center;
center.setxy(5,6);
center.displayxy();     }//MAIN
 
Text Box: Figure 8.8a – A class showing private and public access to its data and function members.
 
 

 

 

 

 

 

 

 

 

Text Box: X is 5 Y is 6
Text Box: Figure 8.8b – Output of Figure 8.8a
 

 

 

 

 

 


 

Note that by having x and y as private, the main program cannot access the x and y directly therefore access has to be through a member function.

 

 

WHEN DO YOU MAKE A FUNCTION PRIVATE?

 

The functions of a class are known as interface, meaning user of the class (outsider) through these functions can access the class. Therefore these functions are mainly set to public. However there are situations when there is no need for an outsider to access a class function and the task of the function is to provide an inside job, therefore it is set to private.


 

 

Text Box: #include<iostream>
using namespace std;
class point {
private: int x;
int  y;
private: void resetxy(int x,int y){ x=0, y=0;}
public: void setxy(int sx, int sy){x=sx; y=sy; } 
             void displayxy(){cout << "X IS = "<<x<< " Y IS = "<<y<<endl; 
                     resetxy(x,y);}
};
void main(){    
            point center;
            center.setxy(5,6);
            center.displayxy();     }//MAIN
 

                                                                                                                                    

 

 

 

 

 

 

 

Text Box: Figure 8.9a – A program showing the use of a private member function 
 

  

 

Text Box: X IS = 5 Y IS = 6
Text Box: Figure 8.9b – Output of Figure 8.9a

 

 

 

 

 

 


 

In this example the function resetxy( ) is set to private and it is called from function displayxy(). Note that function resetxy( ) can not be invoked from the main program.

 

 

FROM C STRUCTURE TO C++ CLASS

 

In C language, the structure bundles only data, however, in C++ structure (class) bundles data and functions, which is the major difference between C structure and C++ structure (class).  Interestingly, the original name of C++ was “C with classes”.  One can see that a prime reason for creation of the C++ was the frustration of simulating classes in C by using structure and the use of pointer to the functions instead of inclusion of functions.

 

 

CLASS AS A USER DEFINED TYPE (DATA ABSTRACTION)

 

C/C++ has its own type such as built-in data type (int, float, char, boolean).  However, a user can make its own type as needed (class) and make the object (variable) from it. The class is the blueprint and object is the real thing that you work with.  In other words, an object is the instance of a class. For example, an analogous example for class would be a built-in type such as int x; Where int is a class and x is an object of the class int. In the following example, point is the class and center is the object of the class:

 

Text Box: #include<iostream>
using namespace std;
class point{
            int x;
            int y;
public: void setxy(int sx, int sy){ x=sx; y=sy;}
            void displayxy(){ cout <<"X IS "<<x<<" Y IS "<<y <<endl; }
            void movexy(int mx,int my){ x= x+mx ;  y=y + my; }          
};
void main(){
            point center;
            center.setxy(10,20);
            center.displayxy();
            center.movexy(5, 3);
            center.displayxy();    }//MAIN
 

  

 

 

 

 

 

 

 

Text Box: Figure 8.10a – A program showing the use of a class object.
 

 

 

 

Text Box: X IS 10 Y IS 20
X IS 15 Y IS 23

 

 

 

 

Hosted by www.Geocities.ws

1