Creating delegate, actually creating an object that can hold a reference to a method.
|
|
- Melissa Byrd
- 6 years ago
- Views:
Transcription
1 Delegates A delegate is an object that can refer to a method. Creating delegate, actually creating an object that can hold a reference to a method. The method can be called through this reference. A delegate can invoke the method to which it refers. Same delegate can be used to call different methods during runtime of a program by simply changing the method to which the delegate refers. thus, the method that will be invoked by a delegate is not determined at compile time but rather run time. This is the principal i advantage of a delegate. 266
2 Syntax of a delegate declaration: delegate ret-type name (parameter-list) ret-type type is the type of the value returned by the methods that the delegate will be calling. The parameters required by the methods called through the delegate are specified in the parameter-list. t Once created, a delegate instance can refer to and call methods whose return type and parameter-list match those specified in the delegate declaration. A delegate can be used to call any method that agrees with its signature and return type. The method can be either an instance method associated with an object or a static method associated with a class. All that matters is that the return type and signature of the method agrees with those of the delegate. 267
3 A Delegate is a special user defined type that is declared globally, ll like a class. Created like an interface, it provides a template for a method. Like interface, a delegate is not defined. Its role is to show what a useful method would look like. Delegates can be specified in their own namespace, or else can be specified within another class. Each delegate is limited to referencing methods of a particular kind only. Function Pointers of C & C++ Delegates in.net Framework Callback functions of Microsoft Win32 Library Programming 268
4 Delegates in action using System; //declaring a delegate type. delegate string StrMod(string str); Class DelegateTest { //replaces space with hyphens. static string ReplaceSpaces(string s) { Console.WriteLine( Replacing spaces with hyphens. ); return s.replace(, - ); //remove spaces. static string RemoveSpaces(string s) { string temp = ; int i; Console.WriteLine( Removing Spaces. ); for ( i=0; i < s.length ; i++) if(s[i]!= ) temp += s[i]; return temp; //reverse a string. static string reverse(string s) { string ti temp = ; int i, j; Console.WriteLine( Reversing the string. ); 269
5 for (j=0, i = s.length-1; i>=0; i--, j++) temp += s[i]; return temp; static void Main() { //Construct a delegate. StrMod strop = new StrMod (ReplaceSpaces ); string str; //call methods through the delegates. str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); strop = new StrMod ( RemoveSpaces ); str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); strop = new StrMod ( reverse ); str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); 270
6 The output of this program is: Replacing spaces with hyphens. Resulting String: This-is-a-test. Removing spaces. Resulting String: Thisisatest. i t Reversing the string. Resulting String:.tset a si siht 271
7 The program declares a delegate type called StrMod as delegate string StrMod(string str); StrMod takes one string parameter and returns a string. In DelegateTest class, three static methods are declared, each with a single parameter of type string and a return type of string. Thus they match the StrMod delegate. In Main(), a StrMod reference called strop is created. {delegate instantiation and assigned a reference to ReplaceSpaces(). StrMod strop = new StrMod (ReplaceSpaces ); Only name is used while passing the function name to the delegate instance, no parameters are specified. While instantiating a delegate, you specify only the name of the method to which you want the delegate to refer. Of course, the method signature must match that of the delegate s declaration. If it doesn t, a compile-time error will result. 272
8 Next, ReplaceSpaces() is called through the delegate instance strop, as str = strop( This is a test. ); Because strop refers to ReplaceSpaces(), ReplaceSpaces() is invoked. Next, strop is assigned reference to RemoveSpaces(), and then strop is called again. strop = new StrMod ( RemoveSpaces ); str = strop( This is a test. ); Finally, strop is assigned a reference to reverse() and strop is called. This results in reverse() being invoked. strop = new StrMod ( reverse ); str = strop( This is a test. ); The key point is invocation of strop results into a call of the method referred to by strop at the time at which the invocation occurs. Thus, the method to call is resolved at runtime, not compile time. 273
9 Use of Delegate static methods. using System; //delegate declaration delegate int Operation(int t x, int y); Class MathOpr { public static int Add (int a, int b) Class Test { {returna+b; public static int Sub (int a, int b) { return a-b; public static int Mul (int a, int b) { return a*b; public static void Main() { //delegate instantiation Operation Opr1 = new Operation (MathOpr.Add); Operation Opr1 = new Operation (MathOpr.Sub); Operation Opr1 = new Operation (MathOpr.Mul); //invoking of delegates int ans1 = Opr1(200,100); 100); int ans2 = Opr2(200,100); int ans3 = Opr3(20,10); Console.WriteLine( Addition : + ans1); Console.WriteLine( Subtraction : + ans2); Console.WriteLine( Multiplication : + ans3); 274
10 Use of Delegate instance methods. //delegates can refer to instance methods too. using System; //declaring a delegate type. delegate string StrMod(string str); Class StringOps { //replaces space with hyphens. public string ReplaceSpaces ( string s ) { Console.WriteLine( Replacing spaces with hyphens. ); return s.replace(, - ); //remove spaces. public string RemoveSpaces ( string s ) { string temp = ; int i; Console.WriteLine( Removing Spaces. ); for ( i=0; i < s.length ; i++) if(s[i]!= ) temp += s[i]; return temp; //reverse a string. public string reverse ( string s ) { string temp = ; int i, j; Console.WriteLine( Reversing the string. ); 275
11 for (j=0, i = s.length-1; i>=0; i--, j++) temp += s[i]; return temp; Class DelegateTest { static void Main() { StringOps so = new StringOps(); // creating an instance of class StringOps //Initialize a delegate StrMod strop = so.replacespaces; string str; //call methods through the delegates. str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); strop = so.removespaces; str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); strop = so.reverse; str = strop( This is a test. ); Console.WriteLine( Resulting String: + str); 276
12 The output of this program is: This program produces the same output as the first one, but in this case, the delegate refers to the methods on an instance of StringOps class. Replacing spaces with hyphens. Resulting String: This-is-a-test. Removing spaces. Resulting String: Thisisatest. Reversing the string. Resulting String:.tset a si siht 277
13 Multicasting / Multicast Delegates One of the most exciting features of a delegate is it support for multicasting. Multicasting is the ability to create and invocation list, or chain, of methods that will be automatically called when a delegate is invoked. Process: Instantiate a delegate Use the + or += operator to add methods to the chain Use the or -= operator to remove a method from the chain. If the delegate returns a value, then the value returned by the last method invoked in the list becomes the return value of the entire delegate invocation. A delegate that makes use of multicasting will often have a void return type. 278
14 Use of Multicast Delegates Example 1 using System; delegate void MultiCDel(); class MD { class Test { static public void Hello() { Console.WriteLine ( Hello ) ; static public void Show() { Console.WriteLine ( Hi ) ; public static void Main() { //delegate instances MultiCDel M1 = new MultiCDel (MD.Hello); MultiCDel M2 = new MultiCDel (MD.Show); //combine the delegates MultiCDel M3 = M1 + M2; MultiCDel M4 = M2 + M1; //extracting the delegates MultiCDel M5 = M3 - M2; MultiCDel M6 = M4 M1; //invoking delegates M3(); M4(); M5(); M6(); Output: Hello Hi Hi Hello Hello Hi 279
15 Use of Multicast Delegates Example 2 using System; delegate void StrMod(ref string str); Class MultiCastDemo td { //replaces space with hyphens. static void ReplaceSpaces (ref string s ) { Console.WriteLine( WriteLine( Replacing spaces with hyphens. ); s = s.replace(, - ); //remove spaces. static void RemoveSpaces (ref string s ) { string temp = ; int i; Console.WriteLine( Removing Spaces. ); for ( i=0; i < s.length ; i++) if(s[i]!= ) temp += s[i]; s= temp; //reverse a string. static void reverse (ref string s ) { string temp = ; int i, j; Console.WriteLine( Reversing the string. ); 280
16 for (j=0, i = s.length-1; i>=0; i--, j++) temp += s[i]; s = temp; static void Main() { //Construct delegates. StrMod strop; StrMod replacesp = ReplaceSpaces; StrMod removesp = RemoveSpaces; StrMod reversestr = reverse; string str = This is a test. ; //set up Multicast. strop = replacesp; strop += reversestr; //Call Multicast. strop(ref str); Console.WriteLine( Resulting String: + str); //remove replace and add remove. strop -= replacesp; strop to += removesp; str = This is a test. ; // reset string //call Multicast. strop (ref str); Console.WriteLine( Resulting String: + str); 281
17 The output of this program is: Replacing spaces with hyphens. Reversing the String. Resulting String:.tset a si siht Reversing the String. Removing spaces. Resulting String:.tsetasisihT 282
18 System.Delegate All delegates are classes that are implicitly derived from System.Delegate. Why Delegates? First, delegates support events. Second, delegates give programs a way to execute methods at runtime without having to know precisely what those methods are at compile time. 283
19 Partial contextual t keyword Started with C# 2.0 A class, structure or interface definition can be broken into two or more pieces, with each piece residing in separate files. When program is compiled, the pieces are united. partial class XY { public XY(int a, int b) { X=a; Y=b; xy1.cs partial class XY { public int X { get; set; xy2.cs partial class XY { public int Y { get; set; xy3.cs using System; // demonstrating the use of partial class definitions class Test { static void Main() { XY xy = new XY(1,2); Console.WriteLine(xy.X +, + xy.y); to compile Test: csc test.cs xy1.cs xy2.cs xy3.cs 284
20 Partial methods Started with C# 3.0, creating Partial Methods. Declaration in one part & implementation in another. Key aspect of a partial method is that the implementation is not required. When a partial method is not implemented by another part of the class or structure, then all calls to the partial method are silently ignored. //Demonstrate a partial method. using system; partial class XY (int a, int b) { X=a; Y=b; //Declare a partial method. 285
21 partial void Show(); partial class XY { public int X{ get; set; //Implement a partial method. partial void Show() { Console.WriteLine( {0,{1, X,Y; partial class XY { public int Y {get; set; //call a partial method. public void ShowXY() { Show(); class Test { static void Main() { XY xy = new XY(1,2); xy.showxy(); 286
22 Notice that the show() is declared in one part of XY and implemented by another part. The implementation displays the values of X & Y. This means that when Show() is called by ShowXY(), the call has effect, and it will, indeed, display X & Y. Comment the implementation of Show(), then calling Show() inside ShowXY() does nothing. Restrictions on Partial Methods: They must return void. They can not have access modifiers. They can not be virtual. They can not use OUT parameters. 287
Delegates, Events, Namespaces, and Advanced Topics
Blind Folio 12:519 Module12 Delegates, Events, Namespaces, and Advanced Topics The Goals of This Module Understand delegates Use events Examine namespaces Create conversion operators Examine the preprocessor
More informationDC69 C# &.NET DEC 2015
Q.2 a. Briefly explain the advantage of framework base classes in.net. (5).NET supplies a library of base classes that we can use to implement applications quickly. We can use them by simply instantiating
More informationPrevious C# Releases. C# 3.0 Language Features. C# 3.0 Features. C# 3.0 Orcas. Local Variables. Language Integrated Query 3/23/2007
Previous C# Releases C# 3.0 Language Features C# Programming March 12, 2007 1.0 2001 1.1 2003 2.0 2005 Generics Anonymous methods Iterators with yield Static classes Covariance and contravariance for delegate
More informationObject Oriented Programming in C#
Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create
More informationIntroduction to C# Applications
1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making
More informationPaytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value
Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object
More informationDiscussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 4, April 22) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Passing Arguments By Value and By Reference So far, we have been passing in
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II
CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 3: C# language basics II Lecture Contents 2 C# basics Methods Arrays Methods 3 A method: groups a sequence of statement takes input, performs actions, and
More informationFunction Overloading
Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures
More information02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211
02 Features of C#, Part 1 Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211 Module Overview Constructing Complex Types Object Interfaces and Inheritance Generics Constructing
More information2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17
CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 3: C# language basics II Lecture Contents 2 C# basics Methods Arrays 1 Methods : Method Declaration: Header 3 A method declaration begins with a method header
More informationUnderstanding delegates
Understanding delegates A delegate is a reference to a method. Declaring Delegates The syntax for declaring delegates looks like this: delegate void IntMethodInvoker(int x); delegate double TwoLongsOp(long
More informationPES INSTITUTE OF TECHNOLOGY
Seventh Semester B.E. IA Test-I, 2014 USN 1 P E I S PES INSTITUTE OF TECHNOLOGY C# solution set for T1 Answer any 5 of the Following Questions 1) What is.net? With a neat diagram explain the important
More informationObject Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes
1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist
More informationBapatla Engineering College::Bapatla II/IV B.Tech(supplementary)Degree examination November 2016
1. Answer all questions. 1x12=1 a. List important features of C#. (1M) 1. Simple 2. Modern 3. Object oriented 4. Type safe 5. Interoperability b. Describe Call Method. (1M) If we want to call an instance
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationObject-Oriented Programming
Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object
More informationHierarchical inheritance: Contains one base class and multiple derived classes of the same base class.
1. What is C#? C# (pronounced "C sharp") is a simple, modern, object oriented, and type safe programming language. It will immediately be familiar to C and C++ programmers. C# combines the high productivity
More informationPreview from Notesale.co.uk Page 3 of 36
all people who know the language. Similarly, programming languages also have a vocabulary, which is referred to as the set of keywords of that language, and a grammar, which is referred to as the syntax.
More information204111: Computer and Programming
204111: Computer and Programming Week 4: Control Structures t Monchai Sopitkamon, Ph.D. Overview Types of control structures Using selection structure Using repetition structure Types of control ol structures
More informationJAVA: A Primer. By: Amrita Rajagopal
JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently
More informationIntroduction To C#.NET
Introduction To C#.NET Microsoft.Net was formerly known as Next Generation Windows Services(NGWS).It is a completely new platform for developing the next generation of windows/web applications. However
More informationObject Oriented Design
Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following
More informationChapter 1: A First Program Using C#
Chapter 1: A First Program Using C# Programming Computer program A set of instructions that tells a computer what to do Also called software Software comes in two broad categories System software Application
More informationAdvanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition
11 Features Advanced Object-Oriented Programming C# Programming: From Problem Analysis to Program Design C# Programming: From Problem Analysis to Program Design 1 4th Edition Chapter Objectives Learn the
More informationUnit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.
1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are
More informationMethod Resolution Approaches. Dynamic Dispatch
Method Resolution Approaches Static - procedural languages (w/o fcn ptrs) Dynamically determined by data values C with function pointers Compile-time analysis can estimate possible callees Dynamically
More informationDelegates (Visual Basic)
Delegates (Visual Basic) https://msdn.microsoft.com/en-us/library/ms172879(d=printer).aspx 1 of 4 02.09.2016 18:00 Delegates (Visual Basic) Visual Studio 2015 Delegates are objects that refer to methods.
More information.Net Technologies. Components of.net Framework
.Net Technologies Components of.net Framework There are many articles are available in the web on this topic; I just want to add one more article over the web by explaining Components of.net Framework.
More informationString sequence of characters string Unicode Characters immutable they cannot be changed after they have been created.
String A string is basically a sequence of characters A string in C# is an object of type String The string type represents a string of Unicode Characters. String objects are immutable that is they cannot
More informationPolymorphism Part 1 1
Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid
More informationFast Introduction to Object Oriented Programming and C++
Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming
More informationCSE 307: Principles of Programming Languages
1 / 26 CSE 307: Principles of Programming Languages Names, Scopes, and Bindings R. Sekar 2 / 26 Topics Bindings 1. Bindings Bindings: Names and Attributes Names are a fundamental abstraction in languages
More informationBuilding Applications
V B. N E T P r o g r a m m i n g, T h e B a s i c C o n c e p t s a n d To o l s E n g. H a s a n A l h o u r i Building Applications analyzing designing coding testing deploying Eng. Hasan Alhouri 1 Programming
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationLearn C# Errata. 3-9 The Nullable Types The Assignment Operators
1 The following pages show errors from the original edition, published in July 2008, corrected in red. Future editions of this book will be printed with these corrections. We apologize for any inconvenience
More informationC++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.
C++11: 10 Features You Should be Using Gordon Brown @AerialMantis R&D Runtime Engineer Codeplay Software Ltd. Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null
More informationPOLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors
POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual
More informationPOLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE
Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public
More informationOOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.
OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.
More information3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal
APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can
More informationObject-Oriented Programming, Iouliia Skliarova
Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout
More informationSubprograms. Copyright 2015 Pearson. All rights reserved. 1-1
Subprograms Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Calling Subprograms Indirectly
More information6.096 Introduction to C++ January (IAP) 2009
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture
More informationImplementing an ADT with a Class
Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the
More informationPointers. Pointers. Pointers (cont) CS 217
Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More information10. Functions (Part 2)
10.1 Overloaded functions 10. Functions (Part 2) In C++, two different functions can have the same name if their parameters are different; either because they have a different number of parameters, or
More informationGE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004
GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout
More informationPES INSTITUTE OF TECHNOLOGY (BSC) V MCA, Second IA Test, OCTOBER 2017 Programming Using C#.NET (13MCA53) Solution Set Faculty: Jeny Jijo
PES INSTITUTE OF TECHNOLOGY (BSC) V MCA, Second IA Test, OCTOBER 2017 Programming Using C#.NET (13MCA53) Solution Set Faculty: Jeny Jijo 1. What is Encapsulation? Explain the two ways of enforcing encapsulation
More informationAsynchronous Functions in C#
Asynchronous Functions in C# Asynchronous operations are methods and other function members that may have most of their execution take place after they return. In.NET the recommended pattern for asynchronous
More informationC# Language. CSE 409 Advanced Internet Technology
C# Language Today You will learn Building a basic class Value Types and Reference Types Understanding Namespaces and Assemblies Advanced Class Programming CSE 409 Advanced Internet Technology Building
More informationC# Asynchronous Programming Model
Spring 2014 C# Asynchronous Programming Model A PRACTICAL GUIDE BY CHRIS TEDFORD TABLE OF CONTENTS Introduction... 2 Background Information... 2 Basic Example... 3 Specifications and Usage... 4 BeginInvoke()...
More informationProgramming in C++: Assignment Week 8
Programming in C++: Assignment Week 8 Total Marks : 20 September 9, 2017 Question 1 Consider the following code segment. Mark 2 void myfunction(int test) { try { if (test) throw test; else throw "Value
More informationProgramming Languages: OO Paradigm, Polymorhism and Class Members
Programming Languages: OO Paradigm, Polymorhism and Class Members Onur Tolga Şehitoğlu Computer Engineering,METU 1 May 2009 Outline 1 2 Abstract Classes Inheritance inclusion polymorphism Binding is still
More informationVIRTUAL FUNCTIONS Chapter 10
1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions
More informationFundamental C# Programming
Part 1 Fundamental C# Programming In this section you will find: Chapter 1: Introduction to C# Chapter 2: Basic C# Programming Chapter 3: Expressions and Operators Chapter 4: Decisions, Loops, and Preprocessor
More informationIT 374 C# and Applications/ IT695 C# Data Structures
IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation
More informationOpening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i
More informationThe list abstract data type defined a number of operations that all list-like objects ought to implement:
Chapter 7 Polymorphism Previously, we developed two data structures that implemented the list abstract data type: linked lists and array lists. However, these implementations were unsatisfying along two
More information1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?
1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity
More informationCS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM
CS 103: Introduction to Programming Fall 2017 - Written Final Exam 12/11/16, 4:30PM 6:30PM Name: USC Email Address: Lecture (Circle One): Redekopp 2:00 TTh Goodney: 2 MW 9:30 TTh 11:00 TTh Complete the
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationException Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011
More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors
More informationA First Look At Java. Didactic Module 13 Programming Languages - EEL670 1
A First Look At Java Didactic Module 13 Programming Languages - EEL670 1 Outline Thinking about objects Simple expressions and statements Class definitions About references and pointers Getting started
More information! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?
A Binding Question! Variables are bound (dynamically) to values Subprogram Activation! Those values must be stored somewhere! Therefore, variables must somehow be bound to memory locations! How? Function
More informationTable of Contents Preface Bare Necessities... 17
Table of Contents Preface... 13 What this book is about?... 13 Who is this book for?... 14 What to read next?... 14 Personages... 14 Style conventions... 15 More information... 15 Bare Necessities... 17
More informationUsing EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli
Using EnScript to Make Your Life Easier Session 1 Suzanne Widup, James Habben, Bill Taroli 1 Master Title Session 1 Getting Started with EnScript 2 EnScript Basics To Begin With EnScript is similar to
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More informationWhat is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1
Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult
More informationTypes. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus
Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type
More informationPIC 10A Objects/Classes
PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous
More informationIn Java there are three types of data values:
In Java there are three types of data values: primitive data values (int, double, boolean, etc.) arrays (actually a special type of object) objects An object might represent a string of characters, a planet,
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding
More informationCSC 8400: Computer Systems. Using the Stack for Function Calls
CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationPolymorphism. Zimmer CSCI 330
Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)
More informationProgramming, numerics and optimization
Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428
More informationJAVA GUI PROGRAMMING REVISION TOUR III
1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)
More informationCIT Week13 Lecture
CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there
More informationChapter 9. Subprograms
Chapter 9 Subprograms Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Calling
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010
CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)
More informationDigiPen Institute of Technology
DigiPen Institute of Technology Presents Session Two: Overview of C# Programming DigiPen Institute of Technology 5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu 2005 DigiPen
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationBinghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program
Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other
More informationMore C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com
More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes
More informationWhat goes inside when you declare a variable?
Stack, heap, value types, reference types, boxing, and unboxing Introduction This article will explain six important concepts: stack, heap, value types, reference types, boxing, and unboxing. This article
More informationType Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works
Type Aliases A name may be defined as a synonym for an existing type name. Traditionally, typedef is used for this purpose. In the new standard, an alias declaration can also be used C++11.Thetwoformsareequivalent.
More information#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }
1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to
More informationClasses. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).
UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members
More informationC++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017
C++\CLI Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017 Comparison of Object Models Standard C++ Object Model All objects share a rich memory model: Static, stack, and heap Rich object life-time
More informationA Short Summary of Javali
A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More information8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );
- 50 - Control Structures: 8. Functions (II) Arguments passed by value and by reference. Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 20 November 14, 2016 CPSC 427, Lecture 20 1/19 Templates Casts and Conversions CPSC 427, Lecture 20 2/19 Templates CPSC 427, Lecture 20
More informationLecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8
Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How
More informationCompilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1
Compilers 8. Run-time Support Laszlo Böszörmenyi Compilers Run-time - 1 Run-Time Environment A compiler needs an abstract model of the runtime environment of the compiled code It must generate code for
More informationCMSC 202 Midterm Exam 1 Fall 2015
1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.
More informationC++_ MARKS 40 MIN
C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float
More informationSri Vidya College of Engineering & Technology
UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the
More information