The C++ programming language

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Chapter 14: Overloading and Templates
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Object-Oriented Programming in C++
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 4./0. lThe object oriented view of the world.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Memory Management.
Constructors and Destructors
Learning Objectives Pointers as dada members
The C++ programming language
Static data members Constructors and Destructors
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Polymorphism &Virtual Functions
CISC181 Introduction to Computer Science Dr
classes and objects review
Polymorphism & Virtual Functions
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
Data structures in C++.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Chapter 15 Pointers, Dynamic Data, and Reference Types
The C++ programming language
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
CISC/CMPE320 - Prof. McLeod
The C++ programming language
Chapter 11: Inheritance and Composition
The C++ programming language
The C++ programming language
VIRTUAL FUNCTIONS RITIKA SHARMA.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
The C++ programming language
Presentation transcript:

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

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!

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.

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

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

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) ); } ...

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.

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.

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. }

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.