The C++ class object and array type 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 data from the normal class object and an arraywith default and overridden values of constructors in C++ programming
To show: How to use an array to store the class objects with default and overridden values of constructor in C++ programming
// C++ object and array example
#include <iostream>
using namespace std;
// class declaration
class wall
{
// the following members are private by default
int length;
int width;
// declaration of the extra_data static type
static int extra_data;
public:
wall(void);
void set(int new_length,int new_width);
int get_area(void);
// inline function
int get_extra(void) { return extra_data++;}
};
// class implementation
// definition of extra_data
int wall::extra_data;
// constructor, assigning initial values
wall::wall(void)
{
length = 8;
width = 8;
extra_data = 1;
}
// this method will set the wall size to the two 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);
}
// the main program
void main(void)
{
// 7 objects are instantiated, including an array
wall small, medium, large, group[4];
// assigning values, overridden
small.set(5, 7);
large.set(15, 20);
// group[0] uses default
for(int index=1; index<4; index++)
group[index].set(index + 10, 10);
cout<<"Sending message, small.get_area(): ";
cout<<"Area of the small wall is "<<small.get_area()<<endl<<endl;
cout<<"Sending message, medium.get_area(): ";
cout<<"Area of the medium wall is "<<medium.get_area()<<endl<<endl;
cout<<"Sending message, large.get_area(): ";
cout<<"Area of the large wall is "<<large.get_area()<<endl<<endl;
cout<<"New length/width group[index].set(index + 10, 10)"<<endl<<endl;
for(int index=0; index<4; index++)
{
cout<<"Sending message using an array, group"<<"["<<index<<"].get_area(): ";
cout<<"An array of wall area #"<<index<<" is "<<group[index].get_area()<<endl<<endl;
}
cout<<"extra_data = 1, extra_data++"<<endl;
cout<<"Sending message using, small.get_extra() or array, group[0].get_extra()"<<endl<<endl;
cout<<"Extra data value is "<<small.get_extra()<<endl;
cout<<"New Extra data value is "<<medium.get_extra()<<endl;
cout<<"New Extra data value is "<<large.get_extra()<<endl;
cout<<"New Extra data value is "<<group[0].get_extra()<<endl;
cout<<"New Extra data value is "<<group[3].get_extra()<<endl;
return;
}
Output example:
Sending message, small.get_area(): Area of the small wall is 35
Sending message, medium.get_area(): Area of the medium wall is 64
Sending message, large.get_area(): Area of the large wall is 300
New length/width group[index].set(index + 10, 10)
Sending message using an array, group[0].get_area(): An array of wall area #0 is 64
Sending message using an array, group[1].get_area(): An array of wall area #1 is 110
Sending message using an array, group[2].get_area(): An array of wall area #2 is 120
Sending message using an array, group[3].get_area(): An array of wall area #3 is 130
extra_data = 1, extra_data++
Sending message using, small.get_extra() or array, group[0].get_extra()
Extra data value is 1
New Extra data value is 2
New Extra data value is 3
New Extra data value is 4
New Extra data value is 5
Press any key to continue . . .