Download presentation
Presentation is loading. Please wait.
1
The C++ programming language
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./0. The C++ programming language Friends. Operator overloading Dynamic creation of objects Friends and relatives Assigning new functionality to operators: overloading
2
Dynamic creation of objects
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./1. Dynamic creation of objects The advantage of dynamic creation of objects in running time is that the objects can be created with parameters determined in runtime or the amount of created objects can be calculated or entered by the user in runtime. The dynamically created objects are allocated in the HEAP memory and they exist until the program destroys them. Theirs deletion and the freeing of memory is made by the program. The dynamic creation of objects is performed by the new operator too, theirs destruction is achieved by delete operator. At objects defined such manner the constructor is invoked too and the destructor works when they disappear. Example: Creation of a single object and a vector dynamically List * listptr, * dynamlists; // The first step is the pointer definition. listptr = new List(12); // One list object having an internal vector with 12 elements. dynamlists = new List[ 3 ] ; // A vector having three list objects // At object vector definition the giving of the number of elements is impossible. // The use of object is here. delete listptr; delete [ ] dynamlists; // It is very comfortable that constructors and destructors perform the dynamic creation and // deletion of internal names vectors!
3
Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./2. Friends and relatives Objects inherited from the same ancestor are relatives in the C++. The data hiding among these is weaker relatively to inaccessibility of private or protected data members and member functions of entirely unfamiliar objects. In certain cases some functions have link to members of different classes. Inserting the name of the function with friend specifier into the friend class the access to the local private data members or member functions from its owner class can be allowed to it. The friend function may be the member function of one of the classes and can have access to the internal parts of an other class such a way or may be a single function out of the classes that gets these rights. Let us see an example on the last case! The names of a group of students be stored in a derived object of the previously introduced List class! An other object that is derived from a new class stores the names and level of memorial medal that the students have got for the excellent school achievements. The same student can be find more times in this list. We would like to determine the summated amount of gold medals that were got by the students of the first given group. It is perceptible that the function that can give the answer can not be connected closely to any class. To get the solution every member of the first group has to be examined using the second list if she or he got gold medal and how many times.
4
Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./3. The next code has to be considered as an addition to the code that was given previously. Only new parts are shown. #define NHS 50 // Number of honoured students, is given here to make the code simple. enum t_medal { gold, silver, bronze }; … class AwardedGroup; // Forward referencing to the AwardedGroup class to make … // possible to refer to it. class List {… public: friend int NumberOfGoldMedals(List& list, AwardedGroup& rewardedGroup); // Reference parameters are applied to avoid copying vectors }; // continued
5
Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./4. class AwardedGroup; { private: t_name awardedVect [NHS]; // Given size for simplicity t_medal medalVect [NHS]; public: void SetAward(int i, t_name nameIn, t_medal medalIn) {strcpy(awardedVect [ i ], nameIn); medalVect [ i ]= medalIn);} void GetAward(int i, t_name nameOut, t_medal& medalOut ) {strcpy(nameOut, awardedVect [ i ]); medalOut = medalVect [ i ];} friend int NumberOfGoldMedals(List& list, AwardedGroup& awardedGroup); }; int NumberOfGoldMedals(List& list, AwardedGroup& awardedGroup) { int sum=0; for (int i =0; i<list.count; i++) // direct access to count! for ( int j =0; j<NHS; j++) { if (!strcmp(awardedGroup. awardedVect [ j ], list.names[ i ]) && awardedGroup.medalVect [ j ] == gold ) sum++; } // direct access to data members ! return sum; } //continued
6
Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./5. … List list(40); AwardedGroup awardedGroup; void main(void) { … // Filling up: list. t_name nameIn; t_medal medalIn; for (int m=0; m<list.GetCount(); m++) { printf("\nName of the %d th student: ", m); gets(nameIn); list.SetElement(m, nameIn); } // Filling up: rewardedGroup for (m=0; m<NHS; m++) { printf("\nName of the %d th awarded student: ", m); scanf("%s", nameIn); printf("\nLevel of award of the %d th student (0,1,2): ", m); // For simplicity scanf("%d",&medalIn); awardedGroup. SetAward(m, nameIn, medalIn); } printf("The students given on the list got %d gold medals. ", // Calling the function NumberOfGoldMedals(list, awardedGroup) ); } ...
7
The access of a member function to private part of an other class
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./6. The access of a member function to private part of an other class This case is shown in the next sketch: class A { … public: void fnA( ); // It can use the private members of B, }; class B { private: … public: void fnB( ); friend void A:: fnA(); // because fnA( ) is a friend of class B … }; At last a remark: if the access to private parts of class B has to be allowed for more functions of class A then the class B can receive class A as a friend, instead of the individual functions of A.
8
Assigning new functionality to operators: overloading
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./7. Assigning new functionality to operators: overloading Operations having the same names can be performed on different objects in the real world, e.g. adding two reagents, two vectors, two sets, two lists, two texts, two numbers. For the two last case the + operator can be used in Pascal programming language. The C++ language makes possible to apply the + operator for every case. This possibility for redefining the functions of operators is given by the operator overloading. It can be regarded as polymorphism of operators. The redefinition does not change the precedence, the unary or binary character of the operator and the applied syntax. Let us see some simpler examples to expand the possibilities of the List class! It would be advantageous if the elements of the names[ ] vector could be accessed indexing the object itself, as if the object would have elements. An other possibility is to give to the < operator such an interpretation as if it could compare two List objects in l1 < l2 form and the result would be true (+1) if l1 had less elements than l2, or false (0) otherwise. The += operator could be interpreted for List type in such a way that l1+= l2 do mean adjoining l2 to the end of l1 and actualising the number of elements of l1.
9
Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./8. The resolutions: … class List {… public: t_name& operator[ ] (int i ) {return names[ i ];} // The object can be indexed int operator< (List otherList) {return count < otherList.count ; } void operator += (List otherList); // Declaration only. … }; … // The data members of otherList object can be accessed directly because it is // a family member. void List::operator+= (List otherList ) // Definition. { t_name * namesNew = new t_name[count + otherList.count ]; for (int i =0; i < count; i++) strcpy(namesNew [ i ], names[ i ] ); for (i =0; i< otherList.count ; i++) strcpy(namesNew [count +i ], otherList.names[ i ] ); delete [ ] names; // The original content can be freed up in the HEAP memory. names = namesNew ; // The original pointer is set to the united list. count += otherList.count ; // The original count is actualised. }
10
Which function call is equivalent to the list[2] expression?
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./9. … // Usage of operators: strcpy( list [2], "Peter Brown"); // Assignment to 3rd element of vector in the list object. puts(list [2]); // Referring to the same element of the names vector.(3rd element, index=2) List l1(5), l2(6); // Creation of two list objects having 5 and 6 elements. if (l1<l2) puts("l1<l2"); else puts("l1>=l2"); // Using the < operator between lists. strcpy(l1[2], "Clara Taylor"); // Element having index 2 is filled up in l1 list. strcpy(l2[2], "Leo Big"); // Element having index 2 is filled up in l2 list that has 6 elements. l1 += l2; // Adjoining the content of the l2 list to the end of l1 list using the += operator. puts(l1[2]); // "Clara Taylor" puts(l1[5+2]); // "Leo Big" , the list became longer really! … Which function call is equivalent to the list[2] expression? list.operator[ ] (2) // Gives a reference to the element. Which function call is equivalent to the l1 < l2 expression? l1.operator< (l2) // 0, or 1 Which function call is equivalent to the l1 += l2; instruction? l1.operator+= (l2); // Performs complex operations.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.