The C++ class object and some 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: Displaying some data using the pointer to an object class and using link list in C++ programming
To show: How to create and use the pointer to the object class and link list in C++ programming
// C++ class object and some link list example
#include <iostream>
using namespace std;
// class declaration part
class wall
{
int length;
int width;
// pointer variable, point to an object
wall *another_wall;
public:
wall(void);
// 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
wall::wall(void)
{
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 the wall instance
int wall::get_area(void)
{
return (length * width);
}
// this method causes the pointer to point to the 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)
{
// objects are instantiated, of type wall
wall small, medium, large;
// a pointer to a wall object type
wall *wall_pointer;
// overriding the default/constructor values for small and large, the others use default
small.set(5, 7);
large.set(15, 20);
cout<<"Using small.set(5, 7): ";
cout<<"Area of the small wall surface is "<<small.get_area()<<endl<<endl;
cout<<"Using constructor value (8,8): ";
cout<<"Area of the medium wall surface is "<<medium.get_area()<<endl<<endl;
cout<<"Using large.set(15, 20): ";
cout<<"Area of the large wall surface is "<<large.get_area()<<endl<<endl;
// small pointing to the next object, medium
small.point_at_next(&medium);
// medium pointing to the next object, large
medium.point_at_next(&large);
// just to check lor...
cout<<"Just to check those values..."<<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;
// assigning the small object address to wall_pointer pointer
wall_pointer = &small;
wall_pointer = wall_pointer->get_next();
// check the small area value, it should be the constructor value instead of the overridden one
cout<<"After assigning the small object address to the wall_pointer..."<<endl;
cout<<"The pointer pointed to the small wall has an area of "<<wall_pointer->get_area()<<endl;
return;
}
Output example:
Using small.set(5, 7): Area of the small wall surface is 35
Using constructor value (8,8): Area of the medium wall surface is 64
Using large.set(15, 20): Area of the large wall surface is 300
Just to check those values...
Area of the small wall surface is 35
Area of the medium wall surface is 64
Area of the large wall surface is 300
After assigning the small object address to the wall_pointer...
The pointer pointed to the small wall has an area of 64
Press any key to continue . . .