Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,

Similar presentations


Presentation on theme: "Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,"— Presentation transcript:

1

2 Overview of Previous Lesson(s)

3 Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs, written in any of several supported languages, allowing them to share common object oriented classes written in any of the languages.  Microsoft refers to this Common Language Runtime as a "managed execution environment”.  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs, written in any of several supported languages, allowing them to share common object oriented classes written in any of the languages.  Microsoft refers to this Common Language Runtime as a "managed execution environment”.

4 Over View..  CLR Programming – Input: Console::ReadLine()  Reads complete line of input as a string using the function. String^ line = Console::ReadLine();  The variable line is of type String^ and stores a reference to the string that results from executing the Console::ReadLine() function. Console::Read()  This function can read a single character. char ch = Console::Read();  With the Read() function, you could read input data character by character, and then, analyze the characters read and convert the input to a corresponding numeric value. 4

5 Over View… Console::ReadKey()  This function returns the key that was pressed as an object of type ConsoleKeyInfo, which is a value class type defined in the System namespace. ConsoleKeyInfo keyPress = Console::ReadKey(true); 5

6 Over View…  Safe_cast:  The safe_cast operation is for explicit casts in the CLR environment. double value1 = 10.5; double value2 = 15.5; int whole_number = safe_cast (value1) + safe_cast (value2);  The last statement casts each of the values of type double to type int before adding them together and storing the result in whole_number 6

7 Over View.. 7

8 Over View... 8 Private Members:-

9 Over View… 9  Friends Function  Functions outside the class that are able to access all the members of a class i.e private or public.  They are defined using the keyword friend  Either include the prototype of a friend function in the class definition, or you can include the whole function definition. friend double BoxSurface(Cbox aBox);

10 Over View…  Constant Objects:  When an object is declared as const, it can’t be modified.  Values such as pi or inchesPerFoot that are might be declared as const double. const CBox standard(3.0, 5.0, 8.0);  A const member function guarantees that it will never modify any of its class’s member data.  Memory Sharing  A facility of sharing the same memory by more than one variable is called a union 10

11 Over View…  Static Classes  Both data members and function members of a class can be declared as static. 11

12 12

13 Contents  Operator Overloading  Comparative Operator overloading  Assignment Operator overloading  Class templates  Defining a Class Template  Template Member Functions  Objects of a Template Class  Discovering Types  C++/Cli Enumerations  Specifying a Type for Enumeration Constants  Specifying Values for Enumeration Constants  Operations on Enumeration Constants  Using Enumerators as Flags 13

14 Operator Overloading  Operator overloading is a very important capability, it enables to make standard C++ operators, such as +, -, *, and so on, work with objects of user defined data types.  Operator overloading doesn’t allow to invent new operators, nor can you change the precedence of an operator, so your overloaded version of an operator will have the same priority in the sequence of evaluating an expression as the original base operator. 14

15 Operator Overloading..  Operators that can’t be overloaded:  Anything else is fair in this game, which gives quite a bit of scope. 15

16 Comparative Operator Overloading  Here is an example in which we redefine the operator > so that, when it was used with objects of the class Cbox, it would return true if the first Cbox argument had a greater volume than the second.  To implement an overloaded operator for a class, you have to write a special function. Assuming that it is a member of the class CBox, the declaration for the function to overload the > operator within the class definition will be as follows: class CBox { public: bool operator > (CBox & aBox) const; // Overloaded 'greater than' // Rest of the class definition... }; 16

17 Comparative Operator Overloading..  The word operator here is a keyword, combined with an operator symbol or name, in this case, >, it defines an operator function.  The function name in this case is operator >.  With the operator > () operator function, the right operand of the operator will be defined by the function parameter.  The left operand will be defi ned implicitly by the pointer this. if(box1 > box2) cout < < endl < < "box1 is greater than box2"; equivalent to box1.operator > (box2); 17

18 Comparative Operator Overloading..  Let ’ s look at how the code for the operator > () function works: // Operator function for 'greater than' which // compares volumes of CBox objects. bool CBox::operator > (const CBox & aBox) const {return this- > Volume() > aBox.Volume(); }  You use a reference parameter to the function to avoid unnecessary copying when the function is called.  Because the function does not alter the object for which it is called, we can declare it as const. If we don ’ t do this, we cannot use the operator to compare const objects of type CBox at all. 18

19 Comparative Operator Overloading..  The return expression uses the member function Volume() to calculate the volume of the Cbox object pointed to by this, and compares the result with the volume of the object aBox using the basic operator >.  The basic > operator returns a value of type int (not a type bool ), and thus, 1 is returned if the CBox object pointed to by the pointer this has a larger volume than the object aBox passed as a reference argument, and 0 otherwise.  The value that results from the comparison will be automatically converted to the return type of the operator function, type bool. 19

20 Comparative Operator Overloading..  Lets look at an example program. 20

21 Assignment Operator Overloading  By not providing an overloaded assignment operator function for your class, the compiler will provide a default.  The default version will simply provide a member - by - member copying process, similar to that of the default copy constructor.  The default assignment operator is called when the left side and the right side of an assignment statement are objects of the same class type.  Try.. 21

22 Class templates  A class template is not, in itself, a class, but a sort of recipe for a class that will be used by the compiler to generate the code for a class. 22

23 Defining a Class Templates  Suppose we want to define classes that can store a number of data samples of some kind, and each class is to provide a Max() function to determine the maximum sample value of those stored.  You can define a class template, which will generate a class CSamples to store samples of various types. 23

24 Defining a Class Templates template class CSamples { public: // Constructor definition to accept an array of samples CSamples(const T values[], int count) { m_Free = count < 100 ? Count : 100; // Don't exceed the array for(int i = 0; i < m_Free; i++) m_Values[i] = values[i]; // Store count number of samples } 24

25 Defining a Class Templates.. // Constructor to accept a single sample CSamples(const T & value) {m_Values[0] = value; // Store the sample m_Free = 1; // Next is free } // Default constructor CSamples(){ m_Free = 0 } // Nothing stored, so first is free // Function to add a sample bool Add(const T & value) {bool OK = m_Free < 100; // Indicates there is a free place if(OK) m_Values[m_Free++] = value; // OK true, so store the value return OK; } 25

26 Defining a Class Templates.. // Function to obtain maximum sample T Max() const { // Set first sample or 0 as maximum T theMax = m_Free ? m_Values[0] : 0; for(int i = 1; i < m_Free; i++) // Check all the samples if(m_Values[i] > theMax) theMax = m_Values[i]; // Store any larger sample return theMax; } private: T m_Values[100]; // Array to store samples Int m_Free; // Index of free location in m_Values }; 26

27 Template Member Functions template class CSamples { // Rest of the template definition... T Max() const; // Function to obtain maximum sample // Rest of the template definition... }  This declares the Max() function as a member of the class template, but doesn’t define it.  We need to create a separate function template for the definition of the member function. So lets do it … 27

28 Template Member Functions..  Now we use the template class name plus the parameters in angled brackets to identify the class template to which the function template belongs: template T CSamples ::Max() const { // Set first sample or 0 as maximum T theMax = m_Free ? m_Values[0] : 0; for(int i = 1; i < m_Free; i++) // Check all the samples if(m_Values[i] > theMax) theMax = m_Values[i]; // Store any larger sample return theMax; } 28

29 Template Member Functions..  Defining a constructor or a destructor outside of the class template definition is very similar. We could write the definition of the constructor that accepts an array of samples as: template CSamples ::CSamples(const T values[], int count) { m_Free = count < 100 ? count : 100; // Don't exceed the array for(int i = 0; i < m_Free; i++) m_Values[i] = values[i]; // Store count number of samples }  The class to which the constructor belongs is specified in the template in the same way as for an ordinary member function. 29

30 Objects  When we use a function defined by a function template, the compiler is able to generate the function from the types of the arguments used.  To create an object based on a class template, always specify the type parameter following the class name in the declaration.  For example, to declare a CSamples object to handle samples of type double, it could be declared as: CSamples myData(10.0);  This defines an object of type CSamples that can store samples of type double, and the object is created with one sample stored with the value 10.0. 30

31 C++ / CLI Programming 31

32 Discovering Types  The native typeid operator does not work with CLR reference types.  C++/CLI has its own mechanism for discovering the type of an expression.  For variables x and y, (x*y).GetType() will produce an object of type System::Type that encapsulates the type of an expression.  This will automatically be converted to a System::String object when you output it. int x = 0; double y = 2.0; Console::WriteLine(L"Type of x*y is {0}", (x*y).GetType());  Executing this fragment will result in the following output: Type of x*y is System.Double 32

33 Discovering Types  Of course, you could use the native typeid operator with the variables x and y and get a type_info object.  C++/CLI also has its own version of typeid that can only be apply to a single variable or a type name.  x::typeid can be used to get the System::Type object encapsulating the type of x.  You can also write String::typeid to get the System::Type object for System::String. 33

34 Enumerations  Enumerations in a C++/CLI program are significantly different from those in an ISO/IEC C++ program. enum class Suit{Clubs, Diamonds, Hearts, Spades};  This defines an enumeration type, Suit, and variables of type Suit can be assigned only one of the values defined by the enumeration - Hearts, Clubs, Diamonds, or Spades.  Because a C++/CLI enumeration is a class type, you cannot defi ne it locally, within a function, for example, so if you want to defi ne such an enumeration for use in main(), for example, you would define it at global scope. 34

35 Enumerations  When we refer to the constants in a C++/CLI enumeration, we always qualify the constant we are using with the enumeration type name. For example: Suit suit = Suit::Clubs;  This statement assigns the value Clubs from the Suit enumeration to the variable with the name suit.  The :: operator that separates the type name, Suit, from the name of the enumeration constant, Clubs, is the scope resolution operator that you have seen before, and it indicates that Clubs exists within the scope of the Suit enumeration. 35

36 Type for Enumeration Constants  The constants in a C++/CLI enumeration can be any of the following types:  To specify the type for the constants in an enumeration, you write the type after the enumeration type name, but separated from it by a colon. 36

37 Type for Enumeration Constants  For example, to specify the enumeration constant type as char, you could write:  The constants in this enumeration will be of type System::Sbyte and the underlying fundamental type will be type char.  The first constant will correspond to code value 0 by default, and the subsequent values will be assigned in sequence.  To get at the underlying value, you must explicitly cast the value to the type. 37

38 Values for Enumeration Constants  You don ’ t have to accept the default for the underlying values. You can explicitly assign values to any or all of the constants defi ned by an enumeration. enum class Face : char {Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};  This will result in Ace having the value 1, Two having the value 2, and so on, with King having the value 13. 38

39 Operations on Enumeration Constants  You can increment or decrement variables of an enum type using ++ or - -, providing the enumeration constants are of an integral type other than bool.  For example, consider this fragment using the Face type from the previous section: Face card = Face::Ten; ++card; Console::WriteLine(L"Card is {0}", card);  Here, you initialize the card variable to Face::Ten and then increment it. The output from the last statement will be: Card is Jack 39

40 Operations on Enumeration Constants..  Incrementing or decrementing an enum variable does not involve any validation of the result, so it is up to you to ensure that the result corresponds to one of the enumerators so that it makes sense.  We can also use the + or – operators with enum values: card = card – Face::Two;  This is not a very likely statement in practice, but the effect is to reduce the value of card by 2 because that is the value of Face::Two 40

41 Operations on Enumeration Constants…  Finally, we can compare enum values using the relational operators: 41

42 Enumerations as Flags  We can define an enumeration such that the enumeration constants represent flags or status bits for something.  Most hardware storage devices use status bits to indicate the status of the device before or after an I/O operation, for example, and you can also use status bits or flags in your programs to record events of one kind or another.  Defining an enumeration to represent flags involves using an attribute. Attributes are additional information that you add to program statements to instruct the compiler to modify the code in some way or to insert code. 42

43 Enumerations as Flags..  Ex of an enum defining flags: [Flags] enum class FlagBits{ Ready = 1, ReadMode = 2, WriteMode = 4, EOF = 8, Disabled = 16};  The [Flags] part of this statement is the attribute and it tells the compiler that the enumeration constants are single bit values; note the choice of explicit values for the constants.  It also tells the compiler to treat a variable of type FlagBits as a collection of flag bits rather than a single value, FlagBits status = FlagBits::Ready | FlagBits::ReadMode | FlagBits::EOF; The status variable will have the value, 0000 0000 0000 0000 0000 0000 0000 1011 43

44 Thank You 44


Download ppt "Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,"

Similar presentations


Ads by Google