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


 

 

 

 

 

MODULE 12a

  C++ OBJECTS & CLASSES

ENCAPSULATION PROGRAM EXAMPLES 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.

 

The C++ programming skills and knowledge for this session:

 

  • 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,8)

54.          

55.            cout<<"Using new value-->small.set(5, 7)\n";

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

57.            cout<<"Using default/initial value-->medium.set(8, 8)\n";

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

59.            cout<<"Using new value-->big.set(15, 20)\n";

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

61.           

62.            // system("pause");

63.  }

 

63 Lines:Output:

 

C++ object oriented private class program example

 

12.8    Inline Implementation

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

12.9     The Class Header File – A User Defined Header File

1.     // program wall.h, the header file

2.      

3.     // class declaration part

4.     class wall

5.     {

6.       int   length;

7.       int   width;

8.        public:

9.           wall(void);

10.        // constructor method

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

12.        // method

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

14.        // another method

15.        ~wall(void);

16.        // destructor

17.      };

18.    

19.     // this header file cannot be compiled or run

 

19 Lines

 

12.9.1    The Class Implementation Program

1.     // program wall.cpp, this is implementation file

2.      

3.     #include  "wall.h"

4.      

5.     wall::wall(void)

6.     // constructor implementation

7.     {

8.        length = 8;

9.        width = 8;

10.  }

11.   

12.  // this method implementation will set a

13.  // wall size to the two input parameters

14.   

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

16.  {

17.      length = new_length;

18.      width = new_width;

19.  }

20.   

21.  wall::~wall(void)

22.  // destructor implementation

23.  {

24.      length = 0;

25.      width = 0;

26.  }

27.   

28.  // this implementation program should be compiled

29.  // without error, generating object file but can't be

30.  // run because there is no main() entry point.

 

30 Lines

12.9.2   Using The wall Object, main() Program

1.     // program wall2.cpp here are the main program,

2.     // the actual program that programmer create

3.      

4.     #include   <iostream>

5.     using namespace std;

6.     #include   "wall.h"

7.     // a user defined header file containing

8.     // class declaration

9.      

10.  main()

11.  {

12.      wall   small, medium, large;

13.      // three objects instantiated of type class wall

14.   

15.     small.set(5, 7);

16.     large.set(15, 20);

17.     // the medium wall uses the values

18.     // supplied by the constructor

19.   

20.     cout<<"In this part, we have divided our program into\n";

21.     cout<<"three parts.\n"<<"1. Declaration part, wall.h\n";

22.     cout<<"2. Implementation part, wall.cpp\n"<<"3. Main program, wall2.cpp\n";

23.     cout<<"The output just from the 3rd part i.e. the main \n";

24.     cout<<"program is same as the previous program, as follows\n";

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

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

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

28.     cout<<"area of the big wall surface is =  "<<large.get_area()<<"\n";

29.     // system("pause");

30.  }

 

30 Lines:Output:

 

C++ object oriented multiple file inclusion

  1. Create a project.

  2. Create header file wall.h and save it.

  3. Create wall.cpp file, compile it without error, generating object file.

  4. Create the main() program wall2.cpp, compile and run this file.

  5. All three files must be in the same project.

  6. Regarding the use of the "box.h" or <box.h>, please refer to module 4.

C++ object oriented multiple file inclusion

 

C++ object oriented multiple file inclusion

 

tenouk fundamental of C++ object oriented tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ related reading and digging:

 

  1. C++ standards reference: ISO/IEC 14882:1998 on the programming language C++.
  2. MFC library class hierarchy chart.
  3. Check the best selling C / C++ books at Amazon.com.
  4. The source code is available in C++ Encapsulation source code.

 

 

 

 

 

 

 

 

 

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


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