=============================MODULE13====================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ========================================================================= //program obarray.cpp, object and an array //For VC++/VC++ .Net or other C++ standard compliance compiler, //change the header files accordingly... //#include //#include //using namespace std; #include #include //-------class declaration-------- class wall { int length; int width; static int extra_data; //declaration of the extra_data static type public: wall(void); void set(int new_length, int new_width); int get_area(void); int get_extra(void) { return extra_data++;} //inline function }; //---------class implementation----------- int wall::extra_data; //Definition of extra_data //constructor, assigning initial values wall::wall(void) { length = 8; width = 8; extra_data = 1; } //This method will set a 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 a wall instance int wall::get_area(void) { return (length * width); } //----------main program----------- void main() { wall small, medium, large, group[4]; //7 objects are instantiated, including an array small.set(5, 7); //assigning values large.set(15, 20); for(int index=1; index<4; index++) //group[0] uses default group[index].set(index + 10, 10); cout<<"Sending message-->small.get_area()\n"; cout<<"Area of the small wall is "<medium.get_area()\n"; cout<<"Area of the medium wall is "<large.get_area()\n"; cout<<"Area of the large wall is "<group"<<"["<small.get_extra() or \n"; cout<<"array, group[0].get_extra()\n"; cout<<"Extra data value is "< #include //--------class declaration part----------- class wall { int length; int width; char *line_of_text; //pointer variable public: wall(char *input_line); //constructor declaration void set(int new_length, int new_width); int get_area(void); }; //---------class implementation part-------- wall::wall(char *input_line) //constructor implementation { length = 8; width = 8; line_of_text = input_line; } //This method will set a 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 a wall instance int wall::get_area(void) { cout< #include //-------class declaration part------ class wall { int length; int width; int *point; //declaration of the pointer variable public: wall(void); //constructor declaration void set(int new_length, int new_width, int stored_value); int get_area(void) { return (length * width); } //Inline function int get_value(void) { return *point; } //Inline function ~wall(); //destructor }; //----------class implementation part---------- wall::wall(void) //constructor implementation { length = 8; width = 8; point = new int; //new keyword *point = 112; } //This method will set a 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; } wall::~wall(void) //destructor { length = 0; width = 0; delete point; //delete keyword } //---------main program---------- void main() { wall small, medium, large; //objects instance small.set(5, 7, 177); large.set(15, 20, 999); cout<<"Area of the small wall surface is "< #include //-------Class declaration part------- class wall { int length; int width; //two member variables public: wall(void); //constructor declaration void set(int new_length, int new_width); int get_area(void); //two methods }; //---------class implementation part---------- wall::wall(void) //constructor implementation { length = 8; width = 8; } //This method will set a 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); } //---------main program-------- void main() { wall small, medium, large; //objects are instantiated of type class wall wall *point; //a pointer to a class wall small.set(5, 7); large.set(15, 20); point = new wall; //new operator //use the defaults value supplied by the constructor cout<<"Use small.set(5, 7)\n"; cout<<"-------------------\n"; cout<<"Area of the small wall surface is "<get_area()\n"; cout<<"------------------------------------------------\n"; cout<<"New surface area of wall "<get_area()<<"\n\n"; cout<<"Use new value, point->set(12, 12)\n"; cout<<"---------------------------------\n"; point->set(12, 12); cout<<"New surface area of wall "<get_area()<<"\n"; delete point; //delete operator system("pause"); } -------------------------------------------------------------------------------------------------------------------- //Program oblist.cpp //object and list #include #include //---------class declaration part--------- class wall { int length; int width; wall *another_wall; //pointer variable public: wall(void); //constructor declaration 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------- wall::wall(void) //constructor implementation { length = 8; width = 8; another_wall = NULL; } //This method will set a 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 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() { wall small, medium, large; //objects are instantiated, of type class wall wall *wall_pointer; //wall *point; //a pointer to a wall small.set(5, 7); large.set(15, 20); //point = new wall; //use the defaults value supplied by the constructor cout<<"Using small.set(5, 7):\n"; cout<<"----------------------\n"; cout<<"Area of the small wall surface is "<get_next(); cout<<"The wall’s pointer pointed to has area "<get_area()<<"\n"; system("pause"); } ------------------------------------------------------------------------------------------------------------------ //using the this pointer explicitly //to refer to object members #include #include class ThiPoint { int c; public: ThiPoint(int); void display(); }; ThiPoint::ThiPoint(int a){ c = a;} //just a constructor void ThiPoint::display() { cout<<"c = "<c = "<c< #include //---------class declaration part------- class wall { int length; int width; wall *another_wall; //pointer variable public: wall(void); //constructor declaration 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------- wall::wall(void) //constructor implementation { length = 8; width = 8; another_wall = NULL; } //This method will set a 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 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() { 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++) { wall_pointer = new wall; //new object instances wall_pointer->set(index+1, index+3); if(start == NULL) start = wall_pointer; //first element in list else temp->point_at_next(wall_pointer); //next element, link list temp = wall_pointer; //print the list cout<<"Starting with wall_pointer->set("<<(index+1)<<","<<(index+3)<<")"<<"\n"; cout<<" New Wall's surface area is " <get_area() << "\n"; } //clean up temp = start; do { temp = temp->get_next(); delete start; start = temp; } while (temp != NULL); system("pause"); } ------------------------------------------------------------------------------------------------------------------ //Program obnest.cpp #include #include //-----first class declaration------- class mail_info { int shipper; int postage; public: void set(int input_class, int input_postage) { shipper = input_class; postage = input_postage; } int get_postage(void) { return postage;} }; //------Second class declaration--------- class box { int length; int width; mail_info label; public: void set(int l, int w, int ship, int post) { length = l; width = w; label.set(ship, post); //Accessing the first class, mail_info set() method } int get_area(void) { return (length * width);} }; //------main program------ void main() { box small, medium, large; //object instances small.set(2,4,1,35); medium.set(5,6,2,72); large.set(8,10,4,98); cout<<"Normal class-->small.get_area()\n"; cout<<"-------------------------------\n"; cout<<"Area of small box surface is "<medium.get_area()\n"; cout<<"-------------------------------\n"; cout<<"Area of medium box surface is "<large.get_area()\n"; cout<<"-------------------------------\n"; cout<<"Area of large box surface is "< #include //----class declaration part------- class wall { public: int length; int width; public: void set(int l,int w) {length = l; width = w;} int get_area(void) {return length * width;} //operator overloading friend wall operator + (wall aa, wall bb); //add two walls friend wall operator + (int aa, wall bb); //add a constant to a wall friend wall operator * (int aa, wall bb); //multiply a wall by a constant }; //-------class implementation part---------- wall operator + (wall aa, wall bb) //add two walls widths together { wall temp; temp.length = aa.length; temp.width = aa.width + bb.width; return temp; } wall operator + (int aa, wall bb) //add a constant to wall { wall temp; temp.length = bb.length; temp.width = aa + bb.width; return temp; } wall operator * (int aa, wall bb) //multiply wall by a constant { wall temp; temp.length = aa * bb.length; temp.width = aa * bb.width; return temp; } void main() { wall small, medium, large; //object instances wall temp; small.set(2,4); medium.set(5,6); large.set(8,10); cout<<"Normal values\n"; cout<<"-------------\n"; cout<<"Area of the small wall surface is "<2 * (4 + 6)\n"; cout<<"New area of the small wall surface is "<2 * (10 + 4) \n"; temp = 10 + small; cout<<"New area of the medium wall surface is "<(4 * 8) * (4 * 10)\n"; temp = 4 * large; cout<<"New area of the large wall surface is "< #include //-------class declaration part--------- class many_names { int length; int width; public: many_names(void); many_names(int len); many_names(int len, int wid); //constructors with different number and type //of parameter list – overloaded functions void display(void); void display(int one); void display(int one, int two); void display(float number); //methods with different number and type //of parameter list – overloaded functions }; //-------implementation part-------- many_names::many_names(void) //void { length = 8; width = 8; } many_names::many_names(int len) //one parameter { length = len; width = 8; } many_names::many_names(int len, int wid) //two parameter { length = len; width = wid; } void many_names::display(void) //void for display { cout<<"From void display function, Area = "<small.display()\n"; small.display(); cout<<"\n-->small.display(100)\n"; small.display(100); cout<<"\n-->small.display(gross,100)\n"; small.display(gross,100); cout<<"\n-->small.display(payroll)\n"; small.display(payroll); cout<<"\n-->medium.display()\n"; medium.display(); cout<<"\n-->large.display(pi)\n"; large.display(pi); system("pause"); } ------------------------------------------------------------------------------------------------------------------ //program deftmeth.cpp, default method #include #include #include //------class declaration part------- class def { int size; //A simple stored value char *string; //A name for the stored data public: //This overrides the default constructor def(void); //This overrides the default copy constructor def(def &in_object); //This overrides the default assignment operator def &operator=(def &in_object); //This destructor should be required with dynamic allocation ~def(void); //And finally, a couple of ordinary methods void set_data(int in_size, char *in_string); void get_data(char *out_string); }; //------class implementation------- def::def(void) { size = 0; string = new char[2]; strcpy(string, ""); } def::def(def &in_object) { size = in_object.size; string = new char[strlen(in_object.string) + 1]; strcpy(string, in_object.string); } def& def::operator=(def &in_object) { delete [] string; size = in_object.size; string = new char[strlen(in_object.string) + 1]; strcpy(string, in_object.string); return *this; //this pointer } def::~def(void) { delete [] string; } void def::set_data(int in_size, char *in_string) { size = in_size; delete [] string; string = new char[strlen(in_string) + 1]; strcpy(string, in_string); } void def::get_data(char *out_string) { char temp[10]; strcpy(out_string, string); strcat(out_string, " = "); itoa(size, temp, 10); strcat(out_string, temp); } //--------main program---------- void main() { char buffer[80]; def my_data; my_data.set_data(8, " small size, override default constructor "); my_data.get_data(buffer); cout<<" content of buffer!!\n"< #include class PlayText { char *NewPointerVariable; public: PlayText(char *InputFromMainProgPointer); int GetData(void); }; PlayText::PlayText(char *InputFromMainProgPointer) { cout<<"Location: constructor implementaion part\n"; cout<<"Examine the flow of execution!!!\n"; cout<<"String brought by object from main program is "<<"\""< #include class LearnPointer { int *MemVarPoint; public: LearnPointer(void); void SetData(int InputData); int GetData(void) { return *MemVarPoint; }; ~LearnPointer(void); }; LearnPointer::LearnPointer(void) { *MemVarPoint = 100; cout<<"In Constructor: Address of pointer *MemVarPoint is "<<&MemVarPoint<<"\n"; } void LearnPointer::SetData(int InputData) { cout<<"In Method: Address of pointer *MemVarPoint is "<<&MemVarPoint<<"\n"; *MemVarPoint = InputData; } LearnPointer::~LearnPointer(void) { delete MemVarPoint; } void main() { LearnPointer SimpleData; int x; cout<<"First time: This is constructor value = "<>x; SimpleData.SetData(x); cout<<"Third time: New data from user = "< #include //----class declaration part------- class TestNewDel { //member variables... private: int TestVar; char *TestPtr; //member functions, constructor and destructor... public: TestNewDel(); int DisplayValue(); char* DisplayStr(); ~TestNewDel(); }; //---class implementation part-------- //constructor... TestNewDel::TestNewDel() { //test how the constructor is invoked... static int x=1; cout<<"In Constructor, pass #"<DisplayStr()<<"\'"< #include #include //----class declaration part------ class TestArray { //these overloaded << and >> must be friend of TestArray class... //so the overloaded << and >> of TestArray class are available //for the ostream and istream classes, u will learn later... //ostream and istream are C++ file I/O... //equal to operator(cout, object) function, friend ostream &operator<<(ostream &,TestArray &); //equal to operator(cin, object) function friend istream &operator>>(istream &,TestArray &); public: //default constructor TestArray(int = 4); //copy constructor TestArray(const TestArray &); //destructor ~TestArray(); //return the size of the an array int GetSize() const; //overloaded the assignment operator const TestArray &operator=(const TestArray &); //overloaded the == operator int operator==(const TestArray &) const; //overloaded the != operator int operator!=(const TestArray &) const; //overloaded the [] operator int &operator[](int); private: int *ptr; int size; }; //-----class implementation part------- //default constructor for TestArray class TestArray::TestArray(int ArraySize) { size = ArraySize; ptr = new int[size]; assert(ptr != 0); for(int i= 0;i= 0) && (index < size)); return ptr[index]; } //== comparison, return 1 if true, 0 if false int TestArray::operator==(const TestArray &rightside) const { if (size != rightside.size) return 0; for(int i = 0; i< size; i++) if(ptr[i] != rightside.ptr[i]) return 0; return 1; } //!= comparison, return 1 if true, 0 if false int TestArray::operator!=(const TestArray &rightside) const { if (size != rightside.size) return 1; for(int i = 0; i> operator istream &operator>>(istream &input, TestArray &x) { for(int i = 0; i < x.size; i++) input>>x.ptr[i]; return input; } //overloaded the << operator ostream &operator<<(ostream &output, TestArray &x) { int i; for(i = 0; i < x.size; i++) { output<>One>>Two; cout<<"\nThen, the arrays content:\n" <<"One[]: "< #include class complexnum { private: double p, q; public: //constructor forming the a + bi //overloaded function, with two arguments... complexnum(double a, double b) { p = a; q = b; } //Constructor forming the a + 0i //overloaded function, with one argument... complexnum(double a) { p = a; q = 0; } //Returns the real part double realpart() {return p; } //Returns the complex part double imaginarypart() { return q; } //Addition operation of two complex numbers //overloaded operator + complexnum operator+(complexnum a) { return complexnum(a.p + p, a.q + q); } //Addition a complex number and a double //overloaded operator + complexnum operator+(double a) { return complexnum(p + a, q);} //Subtraction operation of two complex numbers... //overloaded operator - complexnum operator-(complexnum a) { return complexnum(p - a.p, q - a.q); } //Addition a complex number and a double //overloaded operator - complexnum operator-(double a) { return complexnum(p - a, q); } }; //display format for complex number... //overloaded operator << ostream& operator<<(ostream& s, complexnum r) { //if no imaginary part... if(r.imaginarypart() == 0) //return real part only... return s<>a>>b; complexnum r = complexnum(a,b); cout<<"\nThe complex form is r = "< #include using namespace std; //--------class declaration part----------- class def { int size; //A simple stored value char *string; //A name for the stored data public: //This overrides the default constructor def(void); //This overrides the default copy constructor def(def &in_object); //This overrides the default assignment operator def &operator=(def &in_object); //This destructor should be required with dynamic allocation ~def(void); //And finally, a couple of ordinary methods void set_data(int in_size, char *in_string); void get_data(char *out_string); }; //--------class implementation--------- def::def(void) { size = 0; string = new char[2]; strcpy(string, ""); } def::def(def &in_object) { size = in_object.size; string = new char[strlen(in_object.string) + 1]; strcpy(string, in_object.string); } def& def::operator=(def &in_object) { delete [] string; size = in_object.size; string = new char[strlen(in_object.string) + 1]; strcpy(string, in_object.string); return *this; //this pointer } def::~def(void) { delete [] string; } void def::set_data(int in_size, char *in_string) { size = in_size; //delete the string size object… delete [] string; string = new char[strlen(in_string) + 1]; strcpy(string, in_string); } void def::get_data(char *out_string) { char temp[10]; strcpy(out_string, string); strcat(out_string, " = "); itoa(size, temp, 10); strcat(out_string, temp); } //--------main program---------- void main() { char buffer[80]; def my_data; my_data.set_data(8, " small size, override default constructor "); my_data.get_data(buffer); cout<<" content of buffer!!\n"< using namespace std; //-------class declaration------ class wall { int length; int width; static int extra_data; //declaration of the extra_data static type 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--------- int wall::extra_data; //Definition of extra_data //constructor, assigning initial values wall::wall(void) { length = 8; width = 8; extra_data = 1; } //This method will set a 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 a wall instance int wall::get_area(void) { return (length * width); } //------main program---------- int main() { //instantiates 7 objects wall small, medium, large, group[4]; //assigning values small.set(5, 7); large.set(15, 20); //group[0] uses the default for(int index=1; index<4; index++) group[index].set(index + 10, 10); cout<<"Sending message-->small.get_area()\n"; cout<<"Area of the small wall is "<medium.get_area()\n"; cout<<"Area of the medium wall is "<large.get_area()\n"; cout<<"Area of the large wall is "<group"<<"["<small.get_extra() or \n"; cout<<"array, group[0].get_extra()\n"; cout<<"Extra data value is "<