The C++ class object: using the default (constructor) and overridden values C++ code example

 

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: Using the default (constructor) and overridden values for C++ class object in C++ programming

To show: The C++ class object initialization, creation/instantiation with default and overridden values in C++ programming

 

// the C++ class object, object of the class

#include <iostream>

using namespace std;

 

// a simple class, declaration

class wall

{

// private by default, access only through methods

int length;

int width;

public:

// object constructor, prepare the wall objects initialization

wall(void);

// methods, what the object can do or will do?

void set(int new_length, int new_width);

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

// destructor, destroy all the created object and return the resources to system

~wall(void);

};

 

// class implementation

// This method will set a wall size to the two input parameters by default or initial value

wall::wall(void)

{

// Just set the wall object initial values

cout<<"I'm in wall() constructor, constructing the object..."<<endl;

length = 8;

width = 8;

}

 

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

{

// what the wall object can do? Set the length and width

cout<<"I'm in set() method, 'modify' what the object can do or provide..."<<endl;

length = new_length;

width = new_width;

}

// destructor implementation

wall::~wall(void)

{

// explicitly, what to destroy?

cout<<"I'm in wall() destructor, seek and destroy the object!"<<endl;

length = 0;

width = 0;

}

 

// the main()

void main(void)

{

// three objects instantiated of type wall class

wall small, medium, big;

 

cout<<endl<<"Using default/initial value, small.set(8, 8): ";

cout<<"Area of the small wall is = "<<small.get_area()<<endl<<endl;

 

// new length and width for small wall

small.set(5, 7);

cout<<endl<<"Using new, overridden value, small.set(5, 7): ";

cout<<"Area of the small wall is = "<<small.get_area()<<endl<<endl;

cout<<"Using default/initial value, medium.set(8, 8): ";

cout<<"Area of the medium wall is = "<<medium.get_area()<<endl<<endl;

cout<<"Using default/initial value, big.set(8, 8): ";

cout<<"Area of the big wall is = "<<big.get_area()<<endl<<endl;

 

// new length and width for big wall, while the medium wall uses the default values supplied by constructor, that is (8,8)

big.set(15, 20);

cout<<endl<<"Using new, overridden value, big.set(15, 20): ";

cout<<"Area of the big wall is = "<<big.get_area()<<endl<<endl;

 

return;

}

 

Output example:

 

I'm in wall() constructor, constructing the object...

I'm in wall() constructor, constructing the object...

I'm in wall() constructor, constructing the object...

 

Using default/initial value, small.set(8, 8): Area of the small wall is = 64

 

I'm in set() method, 'modify' what the object can do or provide...

 

Using new, overridden value, small.set(5, 7): Area of the small wall is = 35

 

Using default/initial value, medium.set(8, 8): Area of the medium wall is = 64

 

Using default/initial value, big.set(8, 8): Area of the big wall is = 64

 

I'm in set() method, 'modify' what the object can do or provide...

 

Using new, overridden value, big.set(15, 20): Area of the big wall is = 300

 

I'm in wall() destructor, seek and destroy the object!

I'm in wall() destructor, seek and destroy the object!

I'm in wall() destructor, seek and destroy the object!

Press any key to continue . . .

 

 

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