Using the C++ pointer to class object with new and delete keywords C++ 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: Displaying some stored class object data, default and overridden using pointer to an object in C++ programming

To show: How to use pointer to class object, new and delete keywords to print the default and overridden values of the class object in C++ programming

 

// C++ class object and pointer example

#include <iostream>

using namespace std;

 

// class declaration

class wall

{

int length;

int width;

int *point;

public:

// constructor

wall(void);

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

// inline function

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

// inline function

int get_value(void) { return *point; }

// destructor

~wall();

};

 

// class implementation

// constructor

wall::wall(void)

{

length = 8;

width = 8;

// new keyword

point = new int;

*point = 112;

}

 

// this method will set the wall size to the input parameters

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

{

length = new_length;

width = new_width;

*point = stored_value;

}

 

// destructor

wall::~wall(void)

{

length = 0;

width = 0;

//delete keyword

delete point;

point = NULL;

}

 

// main program

void main(void)

{

// objects instantiation

wall small, medium, large;

 

// overriding the constructor/default values

small.set(5, 7, 177);

large.set(15, 20, 999);

 

// printing the default and overridden values

cout<<" Using normal variable of type object,"<<endl;

cout<<"     default and overridden"<<endl;

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

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

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

cout<<"Area of the large wall surface is "<<large.get_area()<<endl<<endl;

 

// using the pointer variable to an object

cout<<" Using *point pointer variable which point to an object,"<<endl;

cout<<"                default and overridden"<<endl;

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

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

cout<<"Stored value of the medium wall surface is "<<medium.get_value()<<endl;

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

return;

}

 

Output example:

 

Using normal variable of type object,

         default and overridden

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

Area of the small wall surface is 35

Area of the medium wall surface is 64

Area of the large wall surface is 300

 

Using *point pointer variable which point to an object,

                     default and overridden

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

Stored value of the small wall surface is 177

Stored value of the medium wall surface is 112

Stored value of the large wall surface is 999

Press any key to continue . . .

 

 

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