Now just C++ class, object, constructor and destructor code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Providing object initialization using constructor and destroying object using destructor in C++ programming

To show: The C++ class creation, object instantiation, constructor and destructor execution order in C++ programming

 

// using C++ class instead of struct with constructor and destructor

#include <iostream>

using namespace std;

 

// A simple class declaration

class rectangle

{

// private access by default, access only through the method or interface

int height;

int width;

// public

public:

// constructor, initial object construction, allocating storage, initial value etc.

rectangle(void);

// methods, access something, do something

int area(void);

void initialize(int, int);

// destructor, destroy all the object, return resources such as memory etc. to the system

~rectangle(void);

};

 

// class implementation

// constructor implementation

rectangle::rectangle(void)

{

// give initial values

height = 6;

width = 6;

}

 

int rectangle::area(void)

{

// just return the area

return (height * width);

}

 

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

{

// give initial values

height = initial_height;

width = initial_width;

}

 

// destructor implementation

rectangle::~rectangle(void)

{

// destroy all the object an return the resources to the system

height = 0;

width = 0;

}

 

// normal structure - compare with class usage

struct pole

{

int length;

int depth;

};

 

// comes the main()

void main(void)

{

// class object instantiations

rectangle wall, square;

// normal struct

pole lamp_pole;

 

cout<<"Check the size of rectangle = "<<sizeof(rectangle)<<" bytes"<<endl;

cout<<"Check the size of pole = "<<sizeof(pole)<<" bytes"<<endl<<endl;

cout<<"Using class instead of struct, using DEFAULT VALUE"<<endl;

cout<<"supplied by constructor, access through area() method"<<endl;

cout<<"----------------------------------------------------"<<endl<<endl;

cout<<"Area of the wall, wall.area() = "<<wall.area()<<endl;

cout<<"Area of the square, square.area() = "<<square.area()<<endl<<endl;

 

// we can override the constructor values

wall.initialize(12, 10);

square.initialize(8,8);

 

cout<<"Using class instead of struct, USING ASSIGNED VALUE, access through area() method"<<endl;

cout<<"---------------------------------------------------------------------------------"<<endl;

cout<<"Area of the wall, wall.area() = "<<wall.area()<<endl;

cout<<"Area of the square, square.area()= "<<square.area()<<endl<<endl;

 

lamp_pole.length = 50;

lamp_pole.depth = 6;

 

cout<<"Just a comparison to the class, the following is a struct"<<endl;

cout<<"The length of the lamp pole is = "<<lamp_pole.length*lamp_pole.depth<<endl<<endl;

}

 

Output example:

 

Check the size of rectangle = 8 bytes

Check the size of pole = 8 bytes

Using class instead of struct, using DEFAULT VALUE

supplied by constructor, access through area() method

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

Area of the wall, wall.area() = 36

Area of the square, square.area() = 36

Using class instead of struct, USING ASSIGNED VALUE, access through area() method

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

Area of the wall, wall.area() = 120

Area of the square, square.area()= 64

Just a comparison to the class, the following is a struct

The length of the lamp pole is = 300

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index