C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Similar documents
Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Expert C++/CLI:.NET for Visual C++ Programmers

C#: framework overview and in-the-small features

Introduction to C++/CLI 3. What C++/CLI can do for you 6 The rationale behind the new syntax Hello World in C++/CLI 13

C# Programming in the.net Framework

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

Object-Oriented Programming

.Net Technologies. Components of.net Framework

C++ (Non for C Programmer) (BT307) 40 Hours

Introduce C# as Object Oriented programming language. Explain, tokens,

Interview Questions of C++

Fast Introduction to Object Oriented Programming and C++

Course Hours

DAD Lab. 1 Introduc7on to C#

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

Short Notes of CS201

Appendix G: Writing Managed C++ Code for the.net Framework

CS201 - Introduction to Programming Glossary By

C# 6.0 in a nutshell / Joseph Albahari & Ben Albahari. 6th ed. Beijin [etc.], cop Spis treści

VALLIAMMAI ENGINEERING COLLEGE

Chapter 1 Getting Started

Programming in Visual Basic with Microsoft Visual Studio 2010

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Introduction to Programming Using Java (98-388)

DOT NET COURSE BROCHURE

C++/CLI in Action NISHANT SIVAKUMAR MANNING. Greenwich (74 w. long.)

An Introduction to.net for the J2EE Programmer

CS

Microsoft.NET Programming (C#, ASP.NET,ADO.NET, VB.NET, Crystal Report, Sql Server) Goal: Make the learner proficient in the usage of MS Technologies

Introduction To C#.NET

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

S.Sakthi Vinayagam Sr. AP/CSE, C.Arun AP/IT

Tokens, Expressions and Control Structures

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

.Net. Course Content ASP.NET

C++ for System Developers with Design Pattern

PES INSTITUTE OF TECHNOLOGY

UNIT I An overview of Programming models Programmers Perspective

Migrate Your Skills to Microsoft.NET Framework 2.0 and 3.0 using Visual Studio 2005 (C#)

Table of Contents Preface Bare Necessities... 17

Assignment operator string class c++ Assignment operator string class c++.zip


Question And Answer.

Basic Types, Variables, Literals, Constants

Microsoft Visual C# Step by Step. John Sharp

Programming C# 5.0. Ian Griffiths O'REILLY' Beijing Cambridge * Farnham Kbln Sebastopol Tokyo

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Absolute C++ Walter Savitch

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Programming in Visual C++.NET TM. Rex Jaeschke

This tutorial has been prepared for the beginners to help them understand basics of c# Programming.

Understand Computer Storage and Data Types

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Implementing Subprograms

Introduction to C# Applications

Get Unique study materials from

Object-Oriented Programming in C# (VS 2015)

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

CS304 Object Oriented Programming Final Term

Object-Oriented Programming in C# (VS 2012)

Variables. Data Types.

AN OVERVIEW OF C++ 1

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

G52CPP C++ Programming Lecture 13

Introduction to Java

CS3157: Advanced Programming. Outline

Advanced Systems Programming

Multimedia-Programmierung Übung 3

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

STRUCTURING OF PROGRAM

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

3. Basic Concepts. 3.1 Application Startup

CS93SI Handout 04 Spring 2006 Apr Review Answers

Instantiation of Template class

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

And Even More and More C++ Fundamentals of Computer Science

CE221 Programming in C++ Part 1 Introduction

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Homework 4. Any questions?

CHAPTER 1: INTRODUCING C# 3

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS 376b Computer Vision

2 ADT Programming User-defined abstract data types

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

COMP6771 Advanced C++ Programming

Industrial Programming

Dot Net Online Training

September 10,

CSE 307: Principles of Programming Languages

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Object-Oriented Programming

A brief introduction to C++

Chapter 15 - C++ As A "Better C"

An Introduction to C++

END TERM EXAMINATION

CPSC 427: Object-Oriented Programming

Transcription:

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 model: Static objects live for the duration of the program. Objects on stack live within a scope defined by { and }. Objects on heap live at the designer s discretion. Semantics based on deep copy model. That s the good news. That s the bad news. For compilation, a source file must include information about all the types it uses. That s definitely bad news. But it has a work-around, e.g., design to interface not implementation. Use object factories..net Managed Object Model More Spartan memory model: Value types are stack-based only. Reference types (all user defined types and library types) live on the managed heap. Non-deterministic life-time model: All reference types are garbage collected. That s the good news. That s the bad news. Semantics based on a shallow reference model. For compilation, a source file is type checked with metadata provided by the types it uses. That is great news. It is this property that makes.net components so simple.

.Net Object Model Reference Type value type on stack handle on Stack bool, byte, char, decimal, double, float, int, long, sbyte, short, struct, uint, ulong, ushort Body on Heap Example: int x = 3; object, string, user defined type Example: myclass mc = new myclass(args); string mystr = "this is some text";

Language Comparison Standard C++ Is an ANSI and ISO standard. Has a standard library. Universally available: Windows, UNIX, MAC Well known: Large developer base. Lots of books and articles. Programming models supported: Objects Procedural Generic Separation of Interface from Implementation: Syntactically excellent Implementation is separate from class declaration. Semantically poor See object model comparison..net C#, Managed C++, Is an ECMA standard, becoming an ISO standard. Has defined an ECMA library. Mono project porting to UNIX New, but gaining a lot of popularity Developer base growing quickly. Lots of books and articles. Programming models supported: objects. Separation of Interface from Implementation: Syntactically poor Implementation forced in class declaration. Semantically excellent See object model comparison.

Library Comparison Standard C++ Library Portable across most platforms with good standards conformance I/O support is stream-based console, files, and, strings Flexible container facility using Standard Template Library (STL) But no hash-table containers No support for paths and directories Strings, no regular expressions No support for threads No support for inter-process and distributed processing No support for XML Platform agnostic.net Framework Class Library Windows only but porting efforts underway I/O support is function-based console and files Fixed set of containers that are not very type safe. Has hash-table containers Strong support for paths and directories Strings and regular expressions Thread support Rich set of inter-process and distributed processing constructs Support for XML processing Deep support for Windows but very dependent on windows services like COM

Managed Classes Syntax: class N { }; // native C++ class ref class R { }; // CLR reference type value class V { }; // CLR value type interface class I { }; // CLR interface type enum class E { }; // CLR enumeration type N is a standard C++ class. None of the rules have changed. R is a managed class of reference type. It lives on the managed heap and is referenced by a handle: R^ rh = gcnew R; delete rh; [optional: calls destructor which calls Dispose() to release unmanaged resources] Reference types may also be declared as local variables. They still live on the managed heap, but their destructors are called when the thread of execution leaves the local scope. V is a managed class of value type. It lives in its scope of declaration. Value types must be bit-wise copyable. They have no constructors, destructors, or virtual functions. Value types may be boxed to become objects on the managed heap. I is a managed interface. You do not declare its methods virtual. You qualify an implementing class s methods with override (or new if you want to hide the interface s method). E is a managed enumeration. N can hold values, handles, and references to managed types. N can hold values, handles, and references to value types. N can call methods of managed types. R can call global functions and members of unmanaged classes without marshaling. R can hold a pointer to an unmanaged object, but is responsible for creating it on the C++ heap and eventually destroying it.

Comparison of Library Functionality Functionality.Net Framework Libraries Standard C++ Library Extendable I/O Weak Strong strings Strong Strong Composable Containers Moderately good Strong Paths and Directories Strong No Threads Strong Strong Sockets Moderately good No XML Strong No Forms, WPF Strong No Reflection Strong No

Managed C++ Syntax Include system dlls from the Global Assembly Cache (GAC): #include < System.Data.dll> Include standard library modules in the usual way: #include <iostream> Use scope resolution operator to define namespaces using namespace System::Text; Declare.Net value types on stack Declare.Net reference types as pointers to managed heap String^ str = gcnew String( Hello World );

Mixing Pointers and Arrays Managed classes hold handles to reference types: ref class R 2{ private: String^ rstr; }; Managed classes can also hold pointers to native types: ref class R1 { private: std::string* pstr; }; Unmanaged classes can hold managed handles to managed types: class N { private: gcroot<string^> rstr; }; Using these handles and references they can make calls on each other s methods. Managed arrays are declared like this: Array<String^>^ ssarr = gcnew array<string^>(5); ssarr[i] = String::Concat( Number, i.tostring()); 0<= i <= 4 Managed arrays of value types are declared like this: array<int>^ strarray = gcnew array<int>(5); Siarr[i] = i; 0<=i<=4;

Type Conversions C++ Type CTS Signed Type CTS Unsigned Type char Sbyte Byte short int Int16 UInt16 int, int32 Int32 UInt32 long int Int32 UInt32 int64 Int64 UInt64 float Single N/A double Double N/A long double Double N/A bool Boolean N/A

Extensions to Standard C++ Managed classes may have the qualifiers: abstract sealed A managed class may have a constructor qualified as static, used to initialize static data members. Managed classes may have properties: property int Length { int get() { return _len; } void set(int value) { _len = value; } } A managed class may declare a delegate: delegate void somefunc(int anarg);

Managed Exceptions A C++ exception that has a managed type is a managed exception. Application defined exceptions are expected to derive from System::Exception. Managed exceptions may use a finally clause: try { } catch(myexcept &me) { } finally { } The finally clause always executes, whether the catch handler was invoked or not. Only reference types, including boxed value types, can be thrown.

Code Targets An unmanaged C++ program can be compiled to generate managed code using the /clr option. You can mix managed and unmanaged C++ code in same file. Managed C++ can call C# code in a separate library and vice versa.

Mixing Managed and Unmanaged Code You may freely mix unmanaged and managed C++ classes in the same compilation unit. Managed classes may hold pointers to unmanaged objects. Unmanaged classes may hold handles to managed objects wrapped in gcroot: #include <vcclr.h> Declare: gcroot<system::string^> pstr; That helps the garbage collector track the pstr pointer. Calls between the managed and unmanaged domains are more expensive than within either domain. Note, all of the above means, that you can use.net Framework Class Libraries with unmanaged code, and you can use the C++ Standard Library with managed code.

Limitations of Managed Classes Only single inheritance of implementation is allowed. Managed classes can not inherit from unmanaged classes and vice versa. This may be a future addition. No copy constructors or assignment operators are allowed. Member functions may not have default arguments. Friend functions and friend classes are not allowed. const and volatile qualifiers on member functions are currently not allowed.

Platform Invocation - PInvoke Call Win32 API functions like this: [DllImport( kernel32.dll )] extern C bool Beep(Int32,Int32); Where documented signature is: BOOL Beep(DWORD,DWORD) Or, you can call native C++ which then calls the Win32 API Can call member functions of an exported class

Additions to Managed C++ in VS 2005 Generics Syntactically like templates but bind at run time No specializations Uses constraints to support calling functions on parameter type Iterators Support for each construct Anonymous Methods Essentially an inline delegate Partial Types, new to C#, were always a part of C++ Class declarations can be separate from implementation Now, can parse declaration into parts, packaged in separate files

End of Presentation