Presentation is loading. Please wait.

Presentation is loading. Please wait.

LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having.

Similar presentations


Presentation on theme: "LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having."— Presentation transcript:

1 lThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having initialising parameter lInheritance of class properties to subclasses, instances lCalling order of constructors The basics of object oriented programming in C++ lConstructor and destructor lExample program on class hierarchy lCalling order of destructors lRemarks on example program

2 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./1. lConstructor and destructor The objects of real world come into being and come to an end. It may be needed to set certain default values, make certain starting steps at birth of object. It may be likewise needed to perform certain operations at the moment of cessation of the object in order to clear it without any trace. Let us recall our earlier class example: class List {private : int count; char names[300][25]; // Fixed amount of reserved memory!? public :... }; From the declaration of the class is evident that an object of this type will always allocate space for 300 names not depending on the real requirements: if we need more or less. This problem can be avoided easily if the actual value of count and hereby the amount of required memory is given at creation of object so at definition of object-variable. To achieve this goal the next form of class declaration is required:

3 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./2. typedef char t_name[25]; //This type for names makes easy to modify the program later class List {private : int count; t_name * names; // Here is given a pointer that points to the names. public : List( int count0 = 0 ); // The declaration of the constructor function. // The count0 parameter will give the number of required // elements when an instance of the class will be created. // If the parameter is missed at calling then the count0 will be equal to 0 ~List( ); // the declaration of the destructor function, // it has parameter never ! … // here are the other member functions mentioned earlier }; // The definitions of the constructor and destructor: List :: List( int count0 ) // To give again the 0 default value here is prohibited! {count = count0 ; names = new t_name[count0 ]; // Allocating the required memory space dinamically for ( int i = 0; i< count; i++) names[ i ][0]= '\0'; //The elements will be empty strings. } List :: ~List( ) { if (names) delete[ ] names; } // Freeing the dinamically allocated space.

4 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./3. Example on definition of an instance - object variable - of the List class that waits for a starting value for the parameter: List dwarfs(7); // Creation of a List-type object for storing the names of seven dwarfs. If all parameters of the constructor get default value then the absence of actual values at every instances does not result problem at defining arrays of class type elements: List lists[25]; // A vector including 25 List-type objects. If one of the parameters has not default value then a new constructor without parameters has to be applied in the class: class List { … public: List( int count0 ) ; //This constructor has not default parameter value. List( ){ } // A second - default - constructor without parameter. … }; List lists[25]; // In such a case the default constructor will be invoked.

5 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./4. Remarks on constructors: If there is not given constructor then the compiler creates a hidden default one. A constructor given by the programmer can resolve the problem of passing default value to the object (object initialisation). A constructor has not a return type or value declared even it has not a void type. If the programmed constructor has not parameters then the empty parentheses at definition of objects (instances) can be left: E.g.: List2 :: List2( ) {...} Using such a class definition List2 listObject1( ), listObject2; object definitions are valid. A class may have more than one constructors if they have different parameter signature. If the class type is intended for definition of arrays default constructor is needed. It can be automatic; programmed without parameters or programmed having only parameters with default parameter values. Dynamic allocation of objects: List * dwarfsPtr = new List(7); List * listsPtr = new List[25]; freeing: delete dwarfsPtr ; delete[ ] listsPtr ;

6 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./5. lInheritance of class properties to subclasses, instances List Group GK201 GK301 count names[ ] SetCount() GetCount() SetElement() GetElement() groupNo SetGroupNo() GetGroupNo() class instance Let us inherit from the base class List the class Group that do has a new data member besides the inherited data members and member functions : groupNo and two new member functions to set and get the value of this new data member: SetGroupNo( ) and GetGroupNo( ). Let us create the two objects or instances that fall under Group class : GK201 and GK301 study groups! The List class is the base class or ancestor of Group class. The Group class is the subclass or descen- dant of List class.

7 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./6. lExample program on class hierarchy #include #include #include typedef char t_name[25]; // User defined type for storing a name class List { protected: int count; t_name * names; public: List(int count0 = 0); // Constructor, having default 0 count of elements. ~List( ); // Destructor. void SetCount(int countIn) {count = countIn; } int GetCount( ) { return count; } void SetElement(int i, char* nameIn) {strcpy(names[ i ], nameIn); } void GetElement(int i, char* nameOut ) { strcpy(nameOut, names[ i ]); } }; // The definitions for constructor and destructor are given at the end of program.

8 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./7. class Group : public List // Inheriting the class Group from the class List { protected: char groupNo[ 6 ]; // E.g.: "GK201" public: Group(int count0 = 0, char * groupNo0 = " " ); // Constructor, // having 0 default count value and empty string as default group number. ~Group( ){ }; // Destructor. void SetGroupNo(char* groupNoIn) {strcpy(groupNo, groupNoIn); } void GetGroupNo(char* groupNoOut) {strcpy(groupNoOut, groupNo); } }; // The definition of the constructor is given at the end of program. // Defining object variables: Group GK201(5, "GK201"), GK301(8, "GK301"); // The constructor was called 2 times void main( ) { clrscr(); puts("Creation and usage of Group objects\n\n"); puts("Give the names of the GK201 group! " ); //Continued.

9 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./8. t_name nameStr; for ( int k=0; k<GK201.GetCount( ); k++) { printf("Enter the name of the %d th student: ", k); gets( nameStr ); GK201.SetElement(k, nameStr); } char groupNoOut [6]; GK201.GetGroupNo(groupNoOut ); // Asking for the group number. printf("Students of the %s group are the next: \n", groupNoOut ); for ( k=0; k<GK201. GetCount(); k++) { printf(" The name of the %d th student: ", k ); GK201.GetElement(k, nameStr); puts(nameStr ); } printf("Number of students= %d ", GK201.GetCount( ) ); getch( ); } // The end of main function //Continued.

10 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./9. // Definitions for the constructors and destructor: List :: List( int count0 ) // Definition of the constructor of List class. { count= count0; names = new t_name[ count0 ]; // Dynamic allocation of vector. for (int i = 0; i < count0; i++ ) names[ i ][0]= '\0' ; } List :: ~List( ) // Destructor of List class. { // Freeing of the dynamically allocated names vector. if ( names ) delete[ ] names; } Group :: Group( int count0, char * groupNo0 ) : List( count0 ) { strcpy( groupNo, groupNo0 ); } // The constructor of the base class List can receive default value such a way only, // because the constructor of the List is called and creates the names array first, // then will be called this constructor.

11 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./10. // At performing the following object definition, so at creation of object the constructor of // List class will be invoked by the constructor of the Group class passing to it the count0 // parameter. After executing the constructor of the List class the program jumps back to // the block of Group class and the instructions in it will be executed. Group GK201(5, "GK201"); // Object definition, creation. List :: List( int count0 ) // The definition of constructor of the List class. { count= count0; names = new t_name[ count0 ]; // Dynamic vector allocation. for (int i = 0; i < count0; i++ ) names[ i ][0]= '\0' ; } Group :: Group( int count0, char * groupNo0 ): List(count0 ) // Passing the param. { strcpy(groupNo, groupNo0 ); } lCalling order of constructors 1 2

12 1 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./11. // The order of execution of destructors is reversed, fist is executed the destructor of the // object to be destroyed, then, stepping up in the hierarchy, the destructors of the // ancestor classes, one after the another. // The GK201 object will be destroyed at reaching the end of program (in this example). List :: ~List( ) // Destructor of the List class { if ( names ) delete[ ] names; // Freeing the dinamically allocated names vector: } ~Group( ) { }; // This destructor was declared as an automatic inline function // in the class declaration. lCalling order of destructors 2

13 Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./12. Short member functions defined in the class declaration become inline function automatically without explicit indication of the inline modifier. If the member function definition is given out of class declaration then to connect it to the owner class the scope has to be given: type Class :: MemberFunction(...) {... } Using the :: scope operator the ambiguity can be avoided in case of using member functions having the same name and parameter signature in different classes. The own data members and member functions can be referred without scope operator in the own member function definitions. There is a hidden pointer - named this - among parameters of every member function that actually points to that object that was used at function call in the object.Function() format. This pointer can be used directly in the function block making possible to refer to such an address that becomes known after creation of the object only. lRemarks on example program


Download ppt "LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having."

Similar presentations


Ads by Google