Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
-
Upload
robert-daniel -
Category
Documents
-
view
228 -
download
1
Transcript of Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
![Page 1: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/1.jpg)
Differences between C# and C++
Dr. Catherine StringfellowDr. Stewart Carpenter
![Page 2: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/2.jpg)
.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;
![Page 3: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/3.jpg)
Control Structures
if, if-else, switch similar to C++ Loops similar to C++
But there is a foreach for arrays
![Page 4: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/4.jpg)
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)
ConstantsMath.PI = 3.1415926535…
![Page 5: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/5.jpg)
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 );
![Page 6: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/6.jpg)
Value and Reference Types
Value typesContain data of the specified typeBuilt in types (int, float, double,…)Programmer created - structs and enumerations
Reference typesContain an address Built-in (array, object and string) Programmer created – Classes, Interfaces
and Delegates
![Page 7: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/7.jpg)
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
![Page 8: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/8.jpg)
Declaring Arrays
int[] x; // declare reference to an arrayx = new int[10]; // dynamically allocate array
Must use new operator to allocate dynamically the number of elements in the array
![Page 9: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/9.jpg)
Array Methods and Properties
Since sorting data is important in many applications, .NET Framework includes high-speed sorting capabilities
// sort elements in array aArray.Sort( x );
// Determine number of elements in x by propertyx.Length
![Page 10: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/10.jpg)
Multiple-Subscripted Arrays
Rectangular arrays – syntax a little different from C++
Jagged ArraysAn array of arrays of different lengths
![Page 11: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/11.jpg)
// 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 };
![Page 12: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/12.jpg)
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;}
![Page 13: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/13.jpg)
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
![Page 14: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/14.jpg)
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
![Page 15: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/15.jpg)
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;
![Page 16: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/16.jpg)
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
![Page 17: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/17.jpg)
Everyone in C#.NET community uses ToString to obtain an object’s string representation.
//Method of class Point to return string representation of Pointpublic override string ToString ( ){ return “(" + x + ", " + y + “)";}
// call method to display new point valuestring output += "\n\nThe new location of point is " + point;
ToString
![Page 18: Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.](https://reader036.fdocuments.us/reader036/viewer/2022082407/56649dcf5503460f94ac36bc/html5/thumbnails/18.jpg)
Other “interesting” variations from familiar C++ constructs
abstract classes use keyword abstract sealed classes that cannot be overridden Interfaces use inheritance notation Delegates provide mechanism for passing
method references Exception handling includes a finally block
to release resources