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


 

MODULE 12a

  C++ OBJECTS & CLASSES - MORE ON ENCAPSULATION 2

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from previous Module. Starting from this Module, you have to be careful for the source codes that span 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 as Functions, Arrays, Pointers and Structure that have been discussed in C will not be repeated. The source code is available in C++ Encapsulation source code.

 

C++ abilities:

 

          To understand and use the constructor and destructor.
          To understand and use the inline function.

 

12.5    Objects Data Protection – class Solution

  • Examine the following program named classobj.cpp carefully, as an example of object’s data protection in a very simple program.

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

2.     #include <iostream>

3.     using namespace std;

4.      

5.     // a simple class declaration part

6.     class  rectangle

7.     {

8.        // private by default, member variables

9.        int height;

10.      int width;

11.      public:

12.      // public, with two methods

13.      int area(void);

14.      void initialize(int, int);

15.   };

16.   

17.  // class implementation part

18.  int rectangle::area(void)

19.  {

20.       return (height * width);

21.  }

22.   

23.  void  rectangle::initialize(int initial_height, int initial_width)

24.  {

25.        height = initial_height;

26.        width = initial_width;

27.  }

28.   

29.  // a normal structure - compare it with class

30.  struct  pole

31.  {

32.       int  length;  // public

33.       int  depth;   // public

34.    };

35.   

36.  // main program

37.  void main ( )

38.  {

39.      rectangle wall, square;

40.      pole             lamp_pole;

41.     

42.      // wall.height = 12;

43.      // wall.width  = 10;

44.      // square.height  =  square.width  =  8;

45.      // these 3 lines invalid now, private, access only through methods

46.     

47.      wall.initialize(12,10);    // access data through method

48.      square.initialize(8,8);

49.      lamp_pole.length  =  50;  // a normal struct data access

50.      lamp_pole.depth  =  6;

51.      cout<<"Using class instead of struct\n";

52.      cout<<"access through method area()\n";

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

54.      cout<<"Area of the wall-->wall.area() = "<<wall.area()<< "\n\n";

55.      cout<<"Area of the square-->square.area()= "<<square.area()<<"\n\n";

56.      // cout<<"---->Non related surface area is

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

58.      // cout<<"---->Wrong area is

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

60.      //-----illegal because directly accessing the private data

61.    

62.     // system("pause");

63.   }

 

63 Lines:Output:

 

C++ object oriented private class program example

rectangle  wall, square;

C++ object oriented private class program example

  1. First, you can get all of the data you really need through the interface.

  2. Secondly, you cannot get any protected data that you do not need.

12.6    Constructors And Destructors - Initialization

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

2.     // with constructor and destructor

3.     #include <iostream>

4.     using namespace std;

5.      

6.     // a simple class declaration part

7.     class  rectangle

8.     {

9.          // private by default, member variables

10.        int height;

11.        int width;

12.        // public

13.        public:

14.        rectangle(void);            // constructor

15.        int area(void);

16.        void initialize(int, int);

17.        ~rectangle(void);         // destructor

18.      };

19.   

20.      // Implementation part

21.      rectangle::rectangle(void)

22.      //constructor implementation

23.      {

24.          height = 6;

25.          width = 6;

26.      }

27.      

28.      int rectangle::area(void)

29.      {

30.         return (height * width);

31.      }

32.      

33.      void  rectangle::initialize(int initial_height, int initial_width)

34.      {

35.         height = initial_height;

36.         width = initial_width;

37.      }

38.      

39.      // destructor implementation

40.      rectangle::~rectangle(void)

41.      {

42.         height = 0;

43.         width = 0;

44.      }

45.      

46.      // a normal structure - compare with class usage

47.      struct  pole

48.      {

49.         int  length;

50.         int  depth;

51.      };

52.      

53.      // main program

54.      void main ( )

55.      {

56.            rectangle   wall, square;

57.            pole       lamp_pole;

58.      

59.      

60.       cout<<"Using class instead of struct, using DEFAULT VALUE\n";

61.       cout<<"supplied by constructor, access through method area()\n";

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

63.       cout<<"Area of the wall-->wall.area() = "<<wall.area()<< "\n\n";

64.       cout<<"Area of the square-->square.area() = "<<square.area()<<"\n\n";

65.       //    wall.height = 12;

66.       //    wall.width  = 10;

67.       //    square.height  =  square.width  =  8;

68.       // these 3 lines, invalid now, private access only through methods

69.      

70.        wall.initialize(12,10);     // override the constructor values

71.        square.initialize(8,8);

72.        lamp_pole.length  =  50;

73.        lamp_pole.depth  =  6;

74.      

75.       cout<<"Using class instead of struct, USING ASSIGNED VALUE\n";

76.       cout<<"access through method area()\n";

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

78.       cout<<"Area of the wall-->wall.area() = "<<wall.area()<<"\n\n";

79.       cout<<"Area of the square-->square.area()= "<<square.area()<<"\n\n";

80.       // cout<<"----> Non related surface area is

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

82.       // cout<<"---->Wrong area is

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

84.      

85.       // system("pause");

86.  }

 

86 Lines:Output:

 

C++ object oriented constructor and destructor program example

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

 

rectangle(void)   // constructor declaration

...

// constructor implementation

rectangle::rectangle(void)

{

    height = 6;

    width = 6;

}

cout<<"Area of the wall-->wall.area() = "<<wall.area()<< "\n\n";

cout<<"Area of the square-->square.area() = "<<square.area()<<"\n\n";

wall.initialize(12, 10);  // override the constructor values

square.initialize(8, 8);

  ~rectangle(void);  // destructor declaration

...

  // destructor implementation

  rectangle::~rectangle(void)

  {

       height = 0;

       width  = 0;

  }

 

  • In this case, the destructor only assigns zeros to the variables prior being de-allocated.

  • The destructor is only included for illustration of how it is used.  If some blocks of memory were dynamically allocated within an object, the destructor should contain code to de-allocate them prior to losing the pointers to them.  This would release the memory back to system for later or other use.

  • Most compilers implement the destructor calling by default.

12.7   Object  Packaging

  • Examine the program named wall1.cpp carefully.  This is an example of how do not to package an object for universal use.

  • This packaging is actually fine not just for a very small program, but is meant to illustrate to you how to split your program up into smaller, more manageable programs when you are developing a large program as individual or a team of programmers.

  • This program is very similar to the last one, with the pole structure dropped and the class named wall.  The class is declared in lines 6 through 20:

 

// a simple class, declaration part

class wall

{

   // private by default

   int length;

   int width;

  

   public:

       // constructor declaration

       wall(void);

       // methods

       void   set(int new_length, int new_width);

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

       // destructor declaration

       ~wall(void);

  };

// implementation part, a constructor

wall::wall(void)

{

    length = 8;

    width = 8;

}

 

// this method will set a wall size to the two inputs

// parameters by default or the initial values

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

{

    length = new_length;

    width = new_width;

}

 

// destructor implementation

wall::~wall(void)

{

    length = 0;

    width = 0;

}

1.     // program wall1.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a simple class, declaration part

6.     class  wall

7.     {

8.         int  length;

9.         int  width;

10.       // private by default

11.       public:

12.         wall(void);

13.         // constructor declaration

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

15.        // method

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

17.        // destructor method

18.        ~wall(void);

19.        // destructor declaration

20.    };

21.   

22.      // implementation part

23.      wall::wall(void)

24.      {    length = 8;

25.            width = 8;

26.      }

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

28.      // parameters by default or initial value,

29.     

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

31.      {

32.            length = new_length;

33.            width = new_width;

34.      }

35.     

36.      wall::~wall(void)

37.      // destructor implementation

38.      {    length = 0;

39.          width = 0;

40.      }

41.     

42.      // main program

43.      void  main()

44.      {

45.            wall   small, medium, big;

46.            // three objects instantiated of type class wall

47.           

48.            small.set(5, 7);

49.            // new length and width for small wall

50.            big.set(15, 20);

51.            // new length and width for big wall

52.            // the medium wall uses the default

53.            // values supplied by constructor (8