The C++ class object and link list program 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: Listing the surface area using pointer to class object which creating a link list in C++ programming

To show: How to create and use the C++ class object as a link list in C++ programming


// C++ class object and link list example

#include <iostream>

using namespace std;


// class declaration part

class wall


int length;

int width;

// pointer variable

wall *another_wall;


// constructor


void set(int new_length, int new_width);

int get_area(void);

void point_at_next(wall *where_to_point);

wall *get_next(void);



// class implementation part

// constructor implementation



length = 8;

width = 8;

another_wall = NULL;



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

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


length = new_length;

width = new_width;



// this method will calculate and return the area of a wall instance

int wall::get_area(void)


return (length * width);



// this method causes the pointer to point to the (next) input parameter

void wall::point_at_next(wall *where_to_point)


another_wall = where_to_point;



// this method returns the wall the current one points to

wall *wall::get_next(void)


return another_wall;



// main program

void main(void)


wall *start = NULL; // always point to the start of the list

wall *temp = NULL; // working pointer, initialize with NULL

wall *wall_pointer; // use for object wall instances


// generate the list

for(int index = 0; index < 8; index++)


// new object instances/memory allocation

wall_pointer = new wall;

wall_pointer->set(index+1, index+3);

if(start == NULL)

// first element in list

start = wall_pointer;


// next element, link the list



temp = wall_pointer;


// print the list

cout<<"Starting with wall_pointer->set("<<(index+1)<<","<<(index+3)<<"):";

cout<<" New Wall's surface area is " <<temp->get_area()<<endl;



// clean up

temp = start;

do {

temp = temp->get_next();

delete start;

start = NULL;

start = temp;

} while (temp != NULL);




Output example:


Starting with wall_pointer->set(1,3): New Wall's surface area is 3

Starting with wall_pointer->set(2,4): New Wall's surface area is 8

Starting with wall_pointer->set(3,5): New Wall's surface area is 15

Starting with wall_pointer->set(4,6): New Wall's surface area is 24

Starting with wall_pointer->set(5,7): New Wall's surface area is 35

Starting with wall_pointer->set(6,8): New Wall's surface area is 48

Starting with wall_pointer->set(7,9): New Wall's surface area is 63

Starting with wall_pointer->set(8,10): New Wall's surface area is 80

Press any key to continue . . .



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