C for Java Developers.

Uploaded on:
Can be utilized to make non specific capacities and classes: take the sort of information they are ... make systems that you can apply and reapply to an entire scope of ...
Slide 1

C++ for Java Programmers Lecture 12

Slide 2

Yesterday waaay back last lectuer we talked about: What is Inheritance? Base Class Access Control Protected Members Inheriting Multiple base classes Constructors & Destructors in Inheritance Different sorts of Access A word on Style

Slide 3

One Hour Before I can go home! Until then I\'ll be discussing: Template Functions Template Classes The Standard Template Library String Class Documentation Coursework2

Slide 4

Standard Template Library STL was one of the real points amid institutionalization It gives us numerous broadly useful classes and capacities Implements mainstream calculations and information structures Because they are formats, the classes and capacities can be utilized with any information sort – tremendous adaptability.

Slide 5

Templates Pretty contemporary element. Bolstered by all current C++ compilers. Can be utilized to make non specific capacities and classes: take the sort of information they are following up on as a parameter We can have one capacity or class that works with a few sorts of information No compelling reason to recode particular forms for every information sort

Slide 6

Function Templates If capacities are indistinguishable separated from the kind of information they are following up on you ought to have a layout . An air pocket sort for instance is indistinguishable whether you are utilizing numbers or buoys. So we could make a non specific capacity that contains a calculation yet is autonomous of any information. Basically we are making capacities that can over-burden themselves .

Slide 7

So how would I think of one? layout <class type> ret-sort func-name(parameters) { //body of the code } The class sort is only a placeholder It can speak to anything – an int, glide, scorch, and so forth. It can likewise speak to a client characterized sort, a class, anything. A placeholder for the sort of information we will utilize Function names and return sorts watchword Takes in a rundown of capacity parameters simply like an ordinary capacity.

Slide 8

OUTPUT: first is presently 7 second is currently 5 OUTPUT: first is currently 7.77 second is presently 5.55 OUTPUT: first is presently J second is presently G despite everything I don\'t get it… layout <class X> void swap(X &a, X &b) { X temp; temp = a; a = b; b = temp; } int principle() { int first = 7; int second = 5; swap(first, second); cout << "first is presently : " << first << endl; cout << "second is currently : " << second << endl; } coast first = 5.55; glide second = 7.77; burn first = \'J\'; scorch second = \'G\';

Slide 9

Bubble-sorted for e\'s and whizz format <class X> void bubbleSort(X *elements, int size) { X temp; for(int i=1; i<size; i++) for(j=size-1; j>=i; j- - ) if (elements[j-1] > elements[j]) { temp = elements[j-1]; elements[j-1] = elements[j]; elements[j] = temp; } } int fundamental() { int intArray = {7, 3, 9, 4, 12, 2}; double doubleArray = {4.5, 7.77, 5.55, 4.123}; bubbleSort(intArray, 6); bubbleSort(doubleArray, 4); }

Slide 10

Added Extras We can utilize more than one class sort when we setup a layout: template <class X, class Y> void show(X &a, Y &b) We can over-burden formats simply like we would some other capacity - The compiler will work out which layout you are attempting to utilize. We can likewise send ordinary parameters – not only the sorts we indicated toward the begin of the format presentation. You can likewise make specializations of layouts that have somewhat distinctive code than your general form, for particular sorts.

Slide 11

Class Templates Everything we\'ve accomplished for capacities above we can likewise apply to class structures. Thus utilizing the layout catchphrase we can make nonexclusive classes as well. layout <class type> class className { /header for the class } To make a particular example of this class utilize the structure: className <type> objectName;

Slide 12

Templates = Confusing Code We additionally need to say that we are in a class format when we are composing the strategies for that class. Be cautious since this can make your code begin to look truly confounded: Method Syntax: layout <class type> ret-Type className::func-name(parameters); { /body of the capacity } same as an ordinary Member capacity

Slide 13

Example Template Class format <class X, class Y> class myClass X first; Y second; open: myClass(X a, Y b) { first = a; second = b; } void appear(); }; layout <class X, class Y> void myClass::show() { cout << "The primary worth is : " << first << endl; cout << "The second esteem is : " << second << endl; }

Slide 14

Example Template Class int principle() { myClass<int, double> object1(10, 0.23); myClass<char, char*> object2(\'J\', "Goulding"); object1.show(); cout << endl; object2.show(); } OUTPUT: The main quality is 10 The second esteem is 0.23 The principal worth is J The second esteem is Goulding

Slide 15

The Power of Templates The template<> develop can be inconceivably valuable. You can make systems that you can apply and reapply to an entire scope of programming circumstances. Once repaired they are a dependable, strong programming segment. You can re-use them and they\'re ensured to work.

Slide 16

Back to the STL All this format sentence structure can appear to be very scary. However once you get used to them, formats are moderately straightforward. They are utilized in the Standard Template library to offer you off-the-rack answers for an assortment of programming issues. Thus the general purpose of them is to attempt and make your life less demanding 

Slide 17

3 Different Section At the center of the STL are three reasonable things, that permit us to utilize such things as: vectors maps records strings These are the establishments of everything in the STL: compartments calculations iterators

Slide 18

Types of Containers are articles that hold different items. There are three sorts of holder: Sequence compartments Associative Containers Adapted Containers Sequence Containers – basically straight records : vector class list class deque class string class basically only a dynamic cluster a direct connected rundown a twofold finished line A character exhibit – invalid ended string

Slide 19

Associative Containers Associative Containers – permit proficient recovery of qualities in view of keys. Cases : Every compartment class characterizes its own particular capacities that can be connected to itself. However there are likewise gneral calculations… map class multiset class gives access to qualities one of a kind keys – along these lines it injuries a key/esteem pair. advance for quick acquainted query.

Slide 20

Algorithms The STL library gives a gathering of nonexclusive calculations <algo.h> which can be connected to an assortment of various information structures. STL accompanies more than 70 calculations, extending from exceptionally easy to the greatly mind boggling. Most STL calculations work on an arrangement of components, and the calculations access information through the utilization of iterators. An illustration is the calculation for check(). count(i.begin(), i.end(), 2, n);. This tallies the quantity of events of component 2 in the vector, putting the outcome in n.

Slide 21

Iterators An iterator is a sort of generali s ed pointer that might be utilized to cross the components of a holder class. Anytime an iterator is situated at precisely one spot in an accumulation, and will stay there until moved. There are five primary classes of iterators: info yield forward bidirectional irregular get to most intense iterator – bolstered by vector and deque classes

Slide 22

Using the STL Internally, the way the STL works and connects is extremely complex. ...in any case, you don\'t have to. You simply the straightforward interface capacities. Simply choose the sort of holder is most appropriate to the circumstance, make it and after that utilization its capacities. We will consider vectors, records, maps and strings on this course.

Slide 23

Strings Up to now we\'ve been doing it the most difficult way possible. We know C++ does not have worked in backing for strings. However standard C++ can deal with strings in 2 ways The first we\'ve taken a gander at in point of interest – Null Terminated Strings (frequently called C-Strings) which are confounded yet useful for the spirit. Yet, the STL likewise gives us a string class as well.

Slide 24

The String Classes This is a more summed up format class from which string is determined. basic_string string wstring This is the essential class used. It bolsters 8-bit roast strings. comparative class yet wstring bolsters wide-character strings.

Slide 25

Are these Classes Necessary? We\'ve all gotten to be bosses of the invalid ended string! Why do we require a string class? The reason is to do with what we\'ve considered in the last few addresses – administrator over-burdening. The essential administrators, for example, =,+,- thus on can\'t be over-burden to have the capacity to handle invalid ended strings. For instance, consider the accompanying code: char str1[50], str2[50], str3[50] str1 = "Gordon"; /can\'t do str2 = "the Gopher"; /can\'t do str1 = str1 + str2; /blunder

Slide 26

Reasons for Strings Having to utilize strcpy and not having the capacity to utilize administrators can make even simple string operation ungainly. So a String class was grudgingly executed into the STL. This permits strings to be utilized as completely coordinated sorts, with over-burden administrators and basic strategies. However Strings likewise add a security net to your coding.

Slide 27

Part of the STL As we talked about before STL has holders, calculations and iterators. the String class is a holder. To incorporate the string classes simply incorporate the <string> library at the highest point of your code. 3 constructors: string(); string(const burn *str); string(const string &str); Note that the string class is all in lowercase Takes in an invalid ended string Takes in another string object as a parameter all lowercase

Slide 28

Now we can utilize: = Assignmen

View more...