Presentation is loading. Please wait.

Presentation is loading. Please wait.

The C++ programming language

Similar presentations


Presentation on theme: "The C++ programming language"— Presentation transcript:

1 The C++ programming language
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./0. The C++ programming language The basics of object oriented programming in C++ Constructor and destructor Example on definition of an object having initialising parameter Inheritance of class properties to subclasses, instances Example program on class hierarchy Calling order of constructors Calling order of destructors Remarks on example program

2 Constructor and destructor
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./1. Constructor 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 t_name * names; // Here is given a pointer that points to the names.
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./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 List lists[25]; // A vector including 25 List-type objects.
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./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 Remarks on constructors:
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./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 Inheritance of class properties to subclasses, instances
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./5. Inheritance of class properties to subclasses, instances count names[ ] SetCount() GetCount() SetElement() GetElement() class instance List The List class is the base class or ancestor of Group class. The Group class is the subclass or descen-dant of List class. 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( ). groupNo SetGroupNo() GetGroupNo() Group GK201 GK301 Let us create the two objects or instances that fall under Group class : GK201 and GK301 study groups!

7 Example program on class hierarchy
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./6. Example program on class hierarchy #include <stdio.h> #include <conio.h> #include <string.h> 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 { clrscr(); puts("Creation and usage of Group objects\n\n");
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./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 GK201.GetGroupNo(groupNoOut ); // Asking for the group number.
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./8. t_name nameStr; for ( int k=0; k<GK201.GetCount( ); k++) { printf("Enter the name of the %d th student: ", k); gets( nameStr ); 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 ); 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
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./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 Calling order of constructors
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./10. Calling order of constructors // 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 ); } 1 2

12 Calling order of destructors
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./11. Calling order of destructors // 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. 2 1

13 Remarks on example program
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./12. Remarks on example program 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.


Download ppt "The C++ programming language"

Similar presentations


Ads by Google