|< C++ Encapsulation 3 | Main | C++ Encapsulation 5 >| Site Index | Download |


 

 

 

 

 

MODULE 13

C++ OBJECTS & CLASSES

ENCAPSULATION PRINCIPLES AND EXAMPLES 4

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

The source code for this tutorial is available in C++ Data Encapsulation source codes.

 

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

 

  • Class and arrays.

  • Pointer within class.

  • Pointer of the objects.

  • static member variable.

  • Pointer of object to another object: list and linked list examples.

  • Class and strings.

  • Nesting the classes.

  • new and delete operators.

  • this pointer.

  • Operators overloading.

  • Functions overloading.

  • Default methods.

 

13.1    An Introduction

  • This Module will illustrate how to use some of the C/C++ features with classes and objects.  Pointers to an object as well as pointers within an object also will be illustrated.

  • Arrays embedded within an object, and an array of objects will also be experimented.

  • Since objects are simply another C++ data type construct, all of these things are possible.  Make sure you have pre-requirement knowledge of the array and pointer to continue on this Module with more fun.

  • Here, method and function terms may be used interchangeably, as simplicity they provide the same meaning.

13.2    Object Array

  • Examine the program named obarray.cpp carefully.  This program is nearly identical to the program named wall1.cpp in the previous Module, until we come to line 47 where an array of 4 wall objects named group are defined (together with 3 normal variable) as shown below:

wall       small, medium, large, group[4];

  • Keep in mind that the proper way to use these constructs is to separate them into three programs: wall.h, wall.cpp (compiled form) and wall2.cpp as in the previous Module.  We do not follow the rule here just to simplify our learning.

1.            // program obarray.cpp

2.            // object and an array

3.            #include  <iostream>

4.            using namespace std;

5.             

6.            // a class declaration

7.            class  wall

8.            {

9.                   int  length;

10.                 int  width;

11.                 static int  extra_data;

12.                 // declaration of the extra_data static type

13.                 public:

14.                     wall(void);

15.                     void set(int new_length, int new_width);

16.                     int get_area(void);

17.                     int get_extra(void) { return  extra_data++;} // an inline function

18.         };

19.          

20.         // class implementation

21.         int wall::extra_data;  //Definition of extra_data

22.          

23.         // a constructor, assigning initial values

24.         wall::wall(void)

25.         {

26.             length = 8;

27.             width = 8;

28.             extra_data = 1;

29.         }

30.          

31.         // this method will set a wall size to the two input parameters

32.         void wall::set(int new_length, int new_width)

33.         {

34.             length = new_length;

35.             width = new_width;

36.         }

37.          

38.         // this method will calculate and return the area of a wall instance

39.         int wall::get_area(void)

40.         {

41.             return (length * width);

42.         }

43.          

44.         // the main program

45.         void  main()

46.         {

47.             wall   small, medium, large, group[4];

48.             // 7 objects are instantiated, including an array

49.             

50.             small.set(5, 7);    // assigning values

51.             large.set(15, 20);

52.             

53.             for(int index=1; index<4; index++)   // the group[0] uses default

54.                 group[index].set(index + 10, 10);

55.             

56.             cout<<"Sending message-->small.get_area()\n";

57.             cout<<"Area of the small wall is "<<small.get_area()<<"\n\n";

58.             cout<<"Sending message-->medium.get_area()\n";

59.             cout<<"Area of the medium wall is "<<medium.get_area()<<"\n\n";

60.             cout<<"Sending message-->large.get_area()\n";

61.             cout<<"Area of the large wall is "<<large.get_area()<<"\n\n";

62.             

63.             cout<<"New length/width group[index].set(index + 10, 10)\n";

64.             for(int index=0; index<4; index++)

65.             {

66.                 cout<<"Sending message using an array

67.                     -->group"<<"["<<index<<"].get_area()\n";

68.                 cout<<"An array of wall area "<<index<<" is

69.                     "<<group[index].get_area()<<"\n\n";

70.             }

71.             

72.             cout<<"extra_data = 1, extra_data++\n";

73.             cout<<"Sending message using-->small.get_extra() or \n";

74.             cout<<"array, group[0].get_extra()\n";

75.            

76.             cout<<"Extra data value is "<<small.get_extra()<<"\n";

77.             cout<<"New Extra data value is "<<medium.get_extra()<<"\n";

78.             cout<<"New Extra data value is "<<large.get_extra()<<"\n";

79.             cout<<"New Extra data value is "<<group[0].get_extra()<<"\n";

80.             cout<<"New Extra data value is "<<group[3].get_extra()<<"\n";

81.             

82.             // system("pause");

83.         }

 

83 lines: Output:

 

C++ object and class output for program examples

for(int index=1; index<4; index++)   // the group[0] uses default value

       group[index].set(index + 10, 10);

                         group[index].set(index + 10, 10)

The_object.the_method

cout<<"An array of wall area "<<index<<" is "<<group[index].get_area()<<"\n\n";

      cout<<"Extra data value is "<<small.get_extra()<<"\n";

      cout<<"New Extra data value is "<<medium.get_extra()<<"\n";

      cout<<"New Extra data value is "<<large.get_extra()<<"\n";

      cout<<"New Extra data value is "<<group[0].get_extra()<<"\n";

      cout<<"New Extra data value is "<<group[3].get_extra()<<"\n";

 

13.3    A static Variable

static int extra_data;

int wall::extra_data;  // an extra_data definition

 

 

 

 

 

 

 

 

 

 

 

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

 

C++ object and class output for program examples

extra_data = 1;

13.4    A String Within An Object

1.            // program obstring.cpp

2.            #include <iostream>

3.            using namespace std;

4.             

5.            // class declaration part

6.            class wall

7.            {

8.                int    length;

9.                int    width;

10.              char   *line_of_text;    // a pointer variable

11.              public:

12.                 wall(char *input_line);  // a constructor declaration

13.                 void  set(int new_length, int new_width);

14.                 int  get_area(void);

15.         };

16.          

17.         // class implementation part

18.         wall::wall(char *input_line)    // a constructor implementation

19.         {

20.             length = 8;

21.             width = 8;

22.             line_of_text = input_line;

23.         }

24.          

25.         // this method will set a wall size to the two input parameters

26.         void wall::set(int new_length, int new_width)

27.         {

28.             length = new_length;

29.             width = new_width;

30.         }

31.          

32.         // this method will calculate and return

33.         // the area of a wall instance

34.         int wall::get_area(void)

35.         {

36.             cout<<line_of_text<< "= ";

37.             return (length * width);

38.         }

39.          

40.         // the main program

41.         void main()

42.         {

43.             // objects are instantiated with a string

44.             // constant as an actual parameters

45.             wall  small("of small size "),

46.             medium("of medium size "),

47.             large("of large size ");

48.             

49.             small.set(5, 7);

50.             large.set(15, 20);

51.            

52.             cout<<"    Embedded string used as an object\n";

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

54.             cout<<"Area of wall surface ";

55.             cout<<small.get_area()<<"\n";

56.             cout<<"Area of wall surface ";

57.             cout<<medium.get_area()<<"\n";

58.             // use default value of constructor

59.             cout<<"Area of wall surface ";

60.             cout<<large.get_area()<<"\n";

61.             

62.             // system("pause");

63.         }

 

63 lines: Output:

 

C++ object and class output for program examples

 

  • You will notice that line 10 contains a pointer to a char named line_of_text.  The constructor contains an input parameter which is a pointer to a string which will be copied to the string named line_of_text within the constructor.

char   *line_of_text;    // a pointer variable

  • We could have defined the variable line_of_text as an actual array in the class, then use strcpy() to copy the string into the object.

  • It should be pointed out that we are not limited to passing a single parameter to a constructor.  Any number of parameters can be passed, as will be illustrated later.

  • You will notice that the three walls are defined this time, we supply a string constant as an actual parameter with each declaration which is used by the constructor to assign the string pointer some data to point to.

  • When we call get_area() in lines 55, 57 and 60 as shown below, we get the message displayed and the area returned.  It would be prudent to put this operation in separate methods since there is no apparent connection between printing the message and calculating the area, but it was written this way to illustrate that it can be done.

 

   cout<<small.get_area()<<"\n";

   ...

   cout<<medium.get_area()<<"\n";

    // use a default value of constructor

   ...

   cout<<large.get_area()<<"\n";

13.5    An Object With An Internal Pointer

1.     // program opinptr.cpp

2.     #include <iostream>

3.     using namespace std;

4.      

5.     // class declaration part

6.     class wall

7.     {

8.           int   length;

9.           int   width;

10.         int   *point;   

11.         // a declaration of the pointer variable

12.        public:

13.             wall(void);

14.             // a constructor declaration

15.             void   set(int new_length, int new_width, int stored_value);

16.             int    get_area(void) { return (length * width); }

17.             // an inline function

18.             int    get_value(void) { return *point; }

19.             // an inline function

20.             ~wall();

21.             // a destructor

22.      };

23.      

24.      // class implementation part

25.      wall::wall(void)

26.      // a constructor implementation

27.      {

28.         length = 8;

29.         width = 8;

30.         point = new int;      // notice a new keyword

31.         *point = 112;

32.      }

33.      

34.      // this method will set a wall size to the input parameters

35.      void wall::set(int new_length, int new_width, int stored_value)

36.      {

37.            length = new_length;

38.            width = new_width;

39.            *point = stored_value;

40.      }

41.      

42.      wall::~wall(void)

43.      // a destructor

44.      {

45.          length = 0;

46.          width = 0;

47.          delete  point;   // notice a delete keyword

48.      }

49.      

50.      // the main program

51.      void main()

52.      {

53.         wall  small, medium, large;  // objects instance

54.        

55.         small.set(5, 7, 177);

56.         large.set(15, 20, 999);

57.        

58.         cout<<"Area of the small wall surface is "<<small.get_area()<<"\n";

59.         cout<<"Area of the medium wall surface is "<<medium.get_area()<<"\n";

60.         cout<<"Area of the large wall surface is "<<large.get_area()<<"\n\n";

61.         cout<<"Third variable in class wall, pointer *point\n";

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

63.         cout<<"Stored value of the small wall surface is "<<small.get_value()<<"\n";

64.         cout<<"Stored value of the medium wall surface is

65.                                                   "<<medium.get_value()<<"\n";

66.         cout<<"Stored value of the large wall surface is "<<large.get_value()<<"\n";

67.        

68.         // system("pause");

69.  }

 

69 lines: Output:

 

C++ object and class output for program examples

int   *point;   

...

point = new int; // notice a new keyword

wall    small, medium, large;  // objects instance

*point = 112;

if (*point = NULL)

    do something...

else

    do something...

delete    point;   // delete operator

length = 0;

width = 0;

13.6    Dynamically Allocated Object

1.            // program obdyna.cpp

2.            // dynamically allocated object

3.             

4.            #include <iostream>

5.            using namespace std;

6.             

7.            // class declaration part

8.            class wall

9.            {

10.             int length;

11.             int width;

12.             // two member variables

13.             public:

14.                 wall(void);

15.                 // constructor declaration

16.                 void set(int new_length, int new_width);

17.                 int get_area(void);

18.                 // two methods

19.             };

20.            

21.             // class implementation part

22.             wall::wall(void)

23.             // constructor implementation

24.             {

25.                 length = 8;

26.                 width = 8;

27.             }

28.            

29.             // this method will set a wall size to the input parameters

30.             void wall::set(int new_length, int new_width)

31.             {

32.                 length = new_length;

33.                 width = new_width;

34.               }

35.            

36.             // this method will calculate and return the area of a wall instance

37.             int wall::get_area(void)

38.             {

39.                 return    (length * width);

40.             }

41.            

42.             // main program

43.             void main()

44.             {

45.                 wall   small, medium, large;

46.                 // objects are instantiated of type class wall

47.                 wall   *point;

48.                 // a pointer to a class wall

49.                

50.                 small.set(5, 7);

51.                 large.set(15, 20);

52.                

53.                 point = new wall;     // new operator

54.                 // use the defaults value supplied by the constructor

55.                

56.                 cout<<"Use small.set(5, 7)\n";

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

58.                 cout<<"Area of the small wall surface is "<<small.get_area()<<"\n\n";

59.                 cout<<"Use default/constructor value medium.set(8, 8)\n";

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

61.                 cout<<"Area of the medium wall surface is "<<medium.get_area()<<"\n\n";

62.                 cout<<"Use large.set(15, 20)\n";

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

64.                 cout<<"Area of the large wall surface is "<<large.get_area()<<"\n\n";

65.                 cout<<"Use default/constructor value, point->get_area()\n";

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

67.                 cout<<"New surface area of wall "<<point->get_area()<<"\n\n";

68.                 cout<<"Use new value, point->set(12, 12)\n";

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

70.                 point->set(12, 12);

71.                 cout<<"New surface area of wall "<<point->get_area()<<"\n";

72.                 delete   point;    // delete operator

73.                 

74.                 // system("pause");

75.         }

 

75 lines: Output:

 

C++ object and class output program examples

wall    *point;

 point = new wall;     // new keyword, assigns point a wall object

                              cout<<"New surface area of wall "<<point->get_area()<<"\n\n";

       ...

       point->set(12, 12);

       cout<<"New surface area of wall "<<point->get_area()<<"\n";

(*point).set(12, 12);

point->set(12, 12)

delete   point;    // delete keyword

Tenouk C++ encapsulation object oriented tutorial: principles and implementations

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ related reading and digging:

 

  1. The source code for this tutorial is available in C++ Data Encapsulation source codes.
  2. Check the best selling C / C++ and object oriented books at Amazon.com.
  3. MFC library class hierarchy chart.
  4. Any reference to Data Structures Using C.
  5. But may be it is better to use the container, iterator and algorithm of Standard Template Library (STL).

 

 

 

 

 

 

|< C++ Encapsulation 3 | Main | C++ Encapsulation 5 >| Site Index | Download |


C++ Class and Object - The Encapsulation and Data Abstraction:  Part 1 | Part 2 | Part 3 | Part 4