|< struct, typedef, enum, union etc. | Main | C++ Encapsulation 2 >|Site Index |Download |


 

 

 

 

 

MODULE 12

  C++ OBJECTS & CLASSES

ENCAPSULATION PRINCIPLES AND CODE SAMPLES 1

 

 

 

 

 

 

My Training Period: xx hours

 

Starting from this Module, you have to be careful for the source codes thatspan more than one line.  When you copy and paste to the text or compiler editor, make it in one line!  This Module is a transition from C to C++ and Topics of C++ such asFunctions,Arrays,Pointers andStructure that have been discussed in C Tutorial,  will not be repeated.  They are reusable! This Module and that follows can be a very good fundamental for Object Oriented programming though it is an old story:o). The source code is available inC++ Encapsulation source code.

 

The C++ programming skills that should be acquired in this session:

 

 

 

 

 

12.1   Introduction

  • This Module is the beginning of the definition of objects oriented programming of C++.  Basically, encapsulation is the process of forming objects.  It is container, which can only be accessed through certain entry points in controlled manner.

  • An encapsulated object is often called an abstract data type (ADT).  Without encapsulation, which involves the use of one or more classes, it is difficult to define the object oriented programming.

  • We need encapsulation because we are human, and humans make errors.  When we properly encapsulate some code, we actually build protection for the contained code from accidental corruption due to the errors that we are all prone to make.

  • We also tend to isolate errors to small portions of code to make them easier to find and fix.  Furthermore, programming becomes more efficient, productive and faster program development cycle by dividing and creating smaller modules or program portions, then combine in a systematic processes.

  • You will find a lot of readily available classes in Java, Visual Basic®, Microsoft Foundation Classes (MFC) of Visual C++ and other visual programming languages.

  • In visual programming languages, you have to learn how to use the classes, which files to be included in your program etc.  For non technical programmer, it is much easier to learn programming by using visual programming languages isn’t it?  You decide!

12.2    Starting With struct

  • As the beginning please refer to program start.cpp.  This program will be the starting point for our discussion of encapsulation.

  • In this program, a very simple structure is defined in lines 5 through 8 which contain a single int type variable within the structure.

// a struct data type

struct   item

{

      int keep_data;

};

  • Three variables are declared in line 12, each of which contains a single int type variable and each of the three variables are available for use anywhere within the main() function.

item   John_cat, Joe_cat, Big_cat;

  • Each variable can be assigned, incremented, read, modified, or have any number of operations performed on it and a few of the operations are illustrated in lines 15 through 17.  Notice the use of the dot operator to access the structure element.

// assigningvalues

John_cat.keep_data = 10;

Joe_cat.keep_data = 11;

Big_cat.keep_data = 12;

  • An isolated normal local variable named garfield is also declared and used in the same section of code for comparison of the normal variable.

  • Study this program example carefully, then compile and run.

1.    // program start.cpp

2.    #include  <iostream>

3.    using namespace std;

4.     

5.     struct   item     // a struct data type

6.       {

7.             int  keep_data;

8.        };

9.        

10.  void main()

11.   {

12.        item   John_cat, Joe_cat, Big_cat;

13.        int   garfield;   // a normal variable

14.       

15.        John_cat.keep_data = 10;      // assigning values

16.        Joe_cat.keep_data = 11;

17.        Big_cat.keep_data = 12;

18.        garfield = 13;

19.     

20.      // displaying data

21.       cout<<"Data value for John_cat is "<<John_cat.keep_data<<"\n";

22.       cout<<"Data value for Joe_cat is  "<<Joe_cat.keep_data <<"\n";

23.       cout<<"Data value for Big_cat is  "<<Big_cat.keep_data<<"\n";

24.       cout<<"Data value for garfield is "<<garfield<<"\n";

25.       cout<<"Press Enter key to quit\n";

26.      // system("pause");

27.   }

 

27 lines:Output:

 

C++ object oriented structure vs class

 

12.3           Changing To class

class   item

1.    // program class.cpp using class instead of struct

2.    #include <iostream>

3.    using namespace std;

4.     

5.    // the class declaration part

6.     

7.    class item

8.     {

9.       int  keep_data;    // private by default, it is public in struct

10.     public:                 // public part

11.     void  set(int enter_value);

12.     int  get_value(void);

13.  };

14.  

15. // class implementation part

16.  

17.  void  item::set(int enter_value)

18.  {

19.      keep_data = enter_value;

20.  }

21.  int  item::get_value(void)

22.  {

23.     return  keep_data;

24.   }

25.  

26. // main program

27. void main()

28.   {

29.        item    John_cat,  Joe_cat,  Big_cat; 

30.        // three objects instantiated

31.        int   garfield;    // a normal variable

32.       

33.         John_cat.set(10);   // assigning values

34.         Joe_cat.set(11);

35.         Big_cat.set(12);

36.         garfield = 13;

37.        // John_cat.keep_data = 100;     

38.        // Joe_cat.keep_data = 110;      

39.        // these are illegal cause keep_data now, is private by default

40.        

41.         cout<<"Accessing data using class\n";

42.         cout<<"-------------------------\n";

43.         cout<<"Data value for John_cat is "<<John_cat.get_value()<<"\n";

44.         cout<<"Data value for Joe_cat is "<<Joe_cat.get_value()<<"\n";

45.         cout<<"Data value for Big_cat is "<<Big_cat.get_value()<<"\n";

46.         cout<<"\nAccessing data normally\n";

47.         cout<<"---------------------------\n";

48.         cout<<"Data value for garfield is "<<garfield<<"\n";

49.         

50.        // system("pause");

51.   }

 

51 Lines:Output:

 

C++ object oriented class program example

 

12.3.1    Private Section

 // John_cat.keep_data = 100;

  // Joe_cat.keep_data = 110;

C++ object oriented class program example

 

12.3.2    Public Section

  • A new keyword public, introduced in line 10 which states that anything following this keyword can be accessed from outside of this class as shown below:

    public:  // public part

  • Because the two functions are declared following the keyword public, they are both public and available for use by any calling program that is within the scope of this object.

  • This essentially opens two small peepholes in the solid wall of protection that we built around the class and the privatekeep_data variable is not available to the calling program.

  • Thus, we can only use the variable by calling one of the two functions defined within the public part of the class.  These are called member functions because they are members of the class.

  • Since we have two functions, we need to define them by saying what each function will actually do.  This is done in lines 17 through 24 where they are each define in the normal way, except that the class name is prepended onto the function name and separated from it by a double colon ( :: ), called scope operator as shown below:

void item::set(int enter_value)

{

                keep_data = enter_value;

}

 

int item::get_value(void)

{

               return keep_data;

}

C++ object oriented private class program example

12.3.3    Some Terminologies

Term

Description

class

Is a group of data and methods (functions).  A class is very much like a structure type as used in ANSI-C, it is just a type used to create a variable which can be manipulated through method in a program.

object

Is an instance of a class, which is similar to a variable, defined as an instance of a type.  An object is what you actually use in a program since it contains values and can be changed.

method

Is a function contained within the class.  You will find the functions used within a class often referred to as methods in programming literature.

message

Is similar to function call.  In object oriented programming, we send messages instead of calling functions.  For the time being, you can think of them as identical.  Later you will see that they are in fact slightly different.  In programming terms, event or action of the object normally used to describe a consequence of sending message.

 

Table 12.1:  Some terms definition.

void  set(int enter_value);

int   get_value(void);

12.3.4    Sending A Message Or Function Call?

John_cat.set(10);  // assigning values

 

12.4    Real Object And The Problem

1.    // program robject.cpp

2.    #include <iostream>

3.    using namespace std;

4.     

5.    // a function prototype

6.    int  area(int rectangle_height, int rectangle_width);

7.     

8.    struct  rectangle

9.    {

10.        int  height;  // public

11.        int  width;   // public

12.   };

13.  

14. struct  pole

15. {

16.        int  length;   // public

17.        int  depth;    // public

18.  };

19.  

20. // rectangle area

21. int surface_area(int rectangle_height, int rectangle_width)

22. {

23.    return   (rectangle_height * rectangle_width);

24. }

25.  

26. // main program

27. void main ( )

28. {

29.     rectangle        wall,  square;

30.     pole       lamp_pole;

31.  

32.    wall.height = 12;     // assigning values

33.    wall.width  = 10;

34.    square.height  =  square.width  =  8;

35.  

36.    lamp_pole.length  =  50;

37.    lamp_pole.depth  =  6;

38.  

39.   cout<<"Area of wall = height x width, OK!"<< "\n";

40.   cout<<"-------------------------------------"<< "\n";

41.   cout<<"----> Area of the wall is "<<surface_area(wall.height,

42.                                                    wall.width)<< "\n\n";

43.   cout<<"Area of square = height x width, OK!"<< "\n";

44.   cout<<"-------------------------------------"<< "\n";

45.   cout<<"----> Area of square is

46.                        "<<surface_area(square.height,square.width)<<"\n\n";

47.   cout<<"Non related area?"<<"\n = height of square x width of the wall?"<<

48.                                                                       "\n";

49.   cout<<"-------------------------------------"<< "\n";

50.   cout<<"----> Non related surface area is

51.                          "<<surface_area(square.height,wall.width)<<"\n\n";

52.   cout<<"Wrong surface area =  height of square"<<"\nx depth of lamp

53.                                                               pole?"<<"\n";

54.   cout<<"-------------------------------------"<< "\n";

55.   cout<<"---->Wrong surface area is

56.                       "<<surface_area(square.height,lamp_pole.depth)<<"\n";

57.  

58.  // system("pause");

59. }

 

59 Lines:Output:

 

 

 

 

 

 

 

 

---------------------------------------------------------------------------------------

 

C++ object oriented private class program example

C++ object oriented structure program example

Tenouk C++ encapsulation principles, concepts and implementation

 

 

 

 

 

 

 

Further C++ class related reading and digging:

 

  1. The source code is available in C++ Encapsulation source code.
  2. Check the best selling C / C++ books at Amazon.com.
  3. C++ standards reference:ISO/IEC 14882:1998 on the programming language C++.
  4. ISO C++ standards reference:ISO C++.
  5. MFC library class hierarchy chart.
  6. What C++ has done to this world? (http://www.stroustrup.com/applications.html) - Bjarne Stroustrup (http://www.stroustrup.com/).

 

 

 

 

|< struct, typedef, enum, union etc. | Main | C++ Encapsulation 2 >|Site Index |Download |


C++ Object, Class and Encapsulation: Part 1 | Part 2 | Part 3