Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Road Map Introduction to object oriented programming. Classes
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
OOP Languages: Java vs C++
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Chapter 12: Adding Functionality to Your Classes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java and C++, The Difference An introduction Unit - 00.
Overview of C# CS331. Structure of a C# Program // Specify namespaces we use classes from here using System; using System.Threading; // Specify more specific.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Introduction to C# By: Abir Ghattas Michel Barakat.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Topic: Classes and Objects
C# for C++ Programmers 1.
2.7 Inheritance Types of inheritance
Java Primer 1: Types, Classes and Operators
Computing with C# and the .NET Framework
CS360 Windows Programming
C# In many ways, C# looks similar to Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Advanced Programming Lecture 02: Introduction to C# Apps
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
Abstract Classes and Interfaces
Presentation transcript:

Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter

. NET Framework Class Library and Namespaces In both C++ and C#, one can develop Console and Windows applications Window apps in C++ use MFCs, in C# use the FCL for the.NET platform FCL is composed of many namespaces using System.Windows.Forms;

Review Every program is a class! and Main is a method Input can be in terms of strings or bytes Must be parsed to the desired type

Classes and Methods Building blocks of C# programs Every program is a class! (Like in Java) The Main method Each application must have exactly one

1 // Welcome.cs 2 // A first console program in C#. 3 4 using System; 5 6 class Welcome 7 { 8 static void Main( string[] args ) 9 { 10 Console.WriteLine( "Welcome to C# Programming!" ); 11 } 12 }

Getting input: Example // read second number from user as string Console.Write( "\nPlease enter the integer: " ); String sNumber = Console.ReadLine(); // convert numbers from type string to type int int number = Int32.Parse(sNumber ); int number = Int32.Parse( Console.ReadLine() ); Combining steps

Control Structures if, if-else, switch similar to C++ Loops similar to C++ But there is a foreach for arrays

Math Class Class Math is located in namespace System (unnecessary to add an assembly reference) Using methods of static classes ClassName.MethodName( argument1, arument2, … ) Example: Math.Sqrt (900.0) Constants Math.PI = …

Type Promotion Implicit Conversion Coercion of arguments to a higher type when passed to methods or in mixed-type expressions; Explicit Conversion Done with cast or class Convert in namespace System Cast Example: int result = Square ( (int ) y );

Value and Reference Types Value types Contain data of the specified type Built in types (int, float, double,…) Programmer created - struct s and enumerations Reference types Contain an address Built-in (array, object and string) Programmer created – Classes, Interfaces and Delegates

Passing Arguments by Value vs. by Reference Value types are passed by value and reference types are passed by reference by default To pass a value type by reference so you can modify the original variable? Use the ref keyword with variables already initialized Use the out keyword when the called method will initialize it

Declaring Arrays int[] x; // declare reference to an array x = new int[10]; // dynamically allocate array Must use new operator to allocate dynamically the number of elements in the array

Array Methods and Properties Since sorting data is important in many applications,.NET Framework includes high-speed sorting capabilities // sort elements in array a Array.Sort( x ); // Determine number of elements in x by property x.Length

Multiple-Subscripted Arrays Rectangular arrays – syntax a little different from C++ Jagged Arrays An array of arrays of different lengths

// declaration of rectangular array int[,] array1 = new int[5,10]; // declaration and initialization of jagged array int [][] array2 = new int[ 3 ][]; array2[ 0 ] = new int[] { 1, 2 }; array2[ 1 ] = new int[] { 3 }; array2[ 2 ] = new int[] { 4, 5, 6 };

foreach Repetition Structure The foreach repetition structure is used to iterate through values in arrays No counter A variable is used to represent the value of each element foreach ( int grade in gradeArray ) { if ( grade < lowGrade ) lowGrade = grade; }

Initializing Class Objects: Constructors If the constructor does not explicitly initialize data members, the data members are initialized by default Primitive numeric types are set to 0 Boolean types are set to false Reference types are set to null

Properties Public properties allow clients to: Get (obtain the values of) private data and may control formatting and display Set (assign values to) private data and may scrutinize attempts to modify value

class Time { private int hour; // property Hour public int Hour { get { return hour; } set { hour = ( ( value >= 0 && value < 24 ) ? value : 0 ); } } Use it in caller as cout << time.Hour; or time.Hour = 5;

Garbage Collection When objects are no longer referenced, the CLR performs garbage collection Use finalizers in conjunction with the garbage collector to release resources (database connections, file access, etc.) explicitly

readonly Members const members are implicitly static (they are shared by all instances of the class) and must be initialized when they are declared Use keyword readonly to declare members that will be initialized in the constructor but not changed after that public readonly int radius;

Everyone in C#.NET community uses ToString to obtain an object’s string representation. //Method of class Point to return string representation of Point public override string ToString ( ) { return “(" + x + ", " + y + “)"; } // call method to display new point value string output += "\n\nThe new location of point is " + point; ToString

Other “interesting” variations from familiar C++ constructs abstract classes use keyword abstract sealed classes that cannot be overridden Interface s use inheritance notation Delegate s provide mechanism for passing method references Exception handling includes a finally block to release resources

Abstract Classes and Methods Must use keyword abstract in the declaration of an abstract class. Any class with an abstract method or property must be declared abstract To declare a method or property abstract, use keyword abstract in the declaration; abstract methods and properties have no implementation virtual methods do have an implementation that may be overridden.

sealed Classes and Methods sealed methods cannot be overridden in a derived class Methods that are declared static and private, are implicitly sealed sealed classes cannot have any derived-classes Creating sealed classes can allow some runtime optimizations e.g., virtual method calls can be transformed into non-virtual method calls

Creating and Using Interfaces Interfaces are defined using keyword interface Use inheritance notation to specify a class implements an interface (ClassName : InterfaceName) Classes may implement more then one interface (a comma separated list of interfaces) Classes that implement an interface must provide implementations for every method and property in the interface definition SYNTAX is differerent from C++ (and better)

Delegates C# does not allow passing of method references directly as arguments as in C++, but does provide delegates A Delegate is a set of references to methods Delegate objects can be passed to methods; methods can then invoke the methods the delegate objects refer to Delegates that contain one method are known as singlecast delegates and are created or derived from class Delegate Delegates that contain multiple methods are known as multicast delegates and are created or derived from class MulticastDelegate Methods that can be referred to by a delegate, must have the same signature as the delegate

#include using namespace std; typedef int ( * fntype) (int x); int f (int x) { return (x+1); } int g (int x) { return (x -1); } int foo ( fntype h, int z) { return (h(z)); } int main () { int a = 5; int b; b = foo (f, a); cout << b << endl; b = foo (g, a); cout << b << endl; return 0; }

Exception Handling Same as in C++, except may include a finally block to release resources. Exception classes have different names