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

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
True or false A variable of type char can hold the value 301. ( F )
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
Basic Elements of C++ Chapter 2.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
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.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2006 Pearson Education, Inc. All rights reserved Arrays.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Overview of Previous Lesson(s) Over View  Operator Overloading:  Operator overloading is a very important capability, it enables to make standard C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
(continue) © by Pearson Education, Inc. All Rights Reserved.
Defining New Types Lecture 21 Hartmut Kaiser
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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:
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Lecture 12 Pointers and Strings Section 5.4, ,
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Overview of Previous Lesson(s) Over View VP is the methodology in which development allows the user to grab and use the desired tools like menus, buttons,
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
Overview of Previous Lesson(s) Over View  I nteractive development environment is a software application that provides comprehensive facilities to computer.
Eine By: Avinash Reddy 09/29/2016.
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Introduction to C++ Systems Programming.
Structure, Unions, typedef and enumeration
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Chapter 5 Classes.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Basic Elements of C++ Chapter 2.
COP 3330 Object-oriented Programming in C++
Chapter 8 (Part 2) Destructor & Operator Overloading (P.331)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Overview of Previous Lesson(s)

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

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

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

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

Over View.. 7

Over View... 8 Private Members:-

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

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

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

12

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C++ / CLI Programming 31

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

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

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

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

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

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

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

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

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

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

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

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,

Thank You 44