Packaging the C++ class: creating a simple C++ class library
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: Packaging the C++ class to create a simple C++ class library
To show: How to repackage the C++ class related files for distribution
Create an empty Win32 console mode application and add wall.h header file to the project, which is the class declaration and definition file.
// repackaging the C++ class files, on the way of creating our own distributable class library program wall.h, the header file
//
// preprocessor directive to avoid the similar file re-inclusion
#ifndef WALL_H
#define WALL_H
// class declaration
class wall
{
int length;
int width;
public:
// constructor method
wall(void);
// methods, what the class object can do or provide
void set(int new_length, int new_width);
int get_area(void) {return (length * width);}
// destructor method
~wall(void);
};
#endif
// add a new header file named wall.h to your existing project, add this code and save it
// this header file cannot be compiled or run
Then add the source file, wall.cpp an implementation part
// wall.cpp, this is implementation program for the wall.h, on the way of creating our own C++ class library
#include <iostream>
using namespace std;
#include "wall.h"
// constructor implementation, provide all the initial object construction resource
wall::wall(void)
{
cout<<"I'm in wall() constructor, constructing object lor!"<<endl;
length = 8;
width = 8;
}
// this method implementation will set a wall size to the two input parameters
void wall::set(int new_length, int new_width)
{
cout<<"I'm in set() method, 'modifying' what the object does or provides"<<endl;
length = new_length;
width = new_width;
}
// destructor implementation, destroy all the created object and return the resources to the system
wall::~wall(void)
{
cout<<"I'm in ~wall() destructor, destroying all the created object!"<<endl;
length = 0;
width = 0;
}
// add another C++ source file named wall.cpp to your project then compile it without error.
// this implementation program should be compiled without error, generating
// object file but can't be run because there is no main() entry point.
Finally add the main program file, mywall.cpp
// the main() program
#include <iostream>
// user defined header file containing class declaration & definition
#include "wall.h"
using namespace std;
// well our main program becomes simpler and our header (wall.h) and object (wall.cpp) files can be redistributed!
int main()
{
// three objects of class wall been instantiated
wall small, medium, large;
cout<<endl<<"In this part, we have divided our program into ";
cout<<"three parts."<<endl<<"1. Declaration part, wall.h that include the interfaces"<<endl;
cout<<"2. Implementation part, wall.cpp"<<endl<<"3. Main program as usual...."<<endl;
cout<<endl<<"The output from the main() program are similar to the previous"<<endl;
cout<<"program example, but here we repackage the class declaration & definition"<<endl;
cout<<"-------------------------------------------------------------------------"<<endl<<endl;
// the small wall uses the values supplied by the constructor
cout<<"Area of the small wall surface, value from constructor is = "<<small.get_area()<<endl;
// small override the default or constructor value
small.set(5, 7);
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 big wall surface, value from constructor is = "<<large.get_area()<<endl;
// large override the default or constructor value
large.set(15, 20);
cout<<"Area of the big wall surface is = "<<large.get_area()<<endl<<endl;
return 0;
}
Output example:
I'm in wall() constructor, constructing object lor!
I'm in wall() constructor, constructing object lor!
I'm in wall() constructor, constructing object lor!
In this part, we have divided our program into three parts.
1. Declaration part, wall.h that include the interfaces
2. Implementation part, wall.cpp
3. Main program as usual....
The output from the main() program are similar to the previous
program example, but here we repackage the class declaration & definition
-------------------------------------------------------------------------
Area of the small wall surface, value from constructor is = 64
I'm in set() method, 'modifying' what the object does or provides
Area of the small wall surface is = 35
Area of the medium wall surface is = 64
Area of the big wall surface, value from constructor is = 64
I'm in set() method, 'modifying' what the object does or provides
Area of the big wall surface is = 300
I'm in ~wall() destructor, destroying all the created object!
I'm in ~wall() destructor, destroying all the created object!
I'm in ~wall() destructor, destroying all the created object!
Press any key to continue . . .