C++: Differences between C++ and C#

Contributor Icon Contributed by William_Wilson Date Icon February 27, 2006  
Tag Icon Tagged: Computer programming

Key differences between C# and C++


It was a surprise that there was no thread for C++ recipes. Though C++ and C# are quite similar there are some very key and major differences.
-I find C# resembles the style of java more than that of C++ i many ways.

(I will be following this up with some coding recipes, to hopefully start a C++ reciped thread)

Method/Function Declarations:
C++:
public:
Constructor() { }
void aMemberFunction() { }
void aFunctionDeclaration();
private:
void someOtherFunction() { }
int aVariable;

C#:
private aVariable;
public void aMemberFunction() { }
public void aFunctionDeclaration();
private void someOtherFunction() { }

Class Declaration:
For those who know what a managed class is:
C++:
__gc class A_Class{ };
*NOTE that C++ classes end with a ;

C#:
automatically done so:
class A_Class{ }

Inheritence:
C++:
will allow multiple inheritence. eg. allows a method to be overriden many times in subclasses (derived classes).

C#:
supports inheritence, but not multiple, only 1 override per method/function/

Includes:
C++:
allows header files and other class files to be included using the #include keyword.

C#:
does not have such a quality, but the using directive allows other types to be referenced from another class (namespace as it is formerly called) without declaring it’s context.

Switch: *Props to PCurd for pointing out my careless mistake :)
C++:
supports the switch statement, and fall through.

C#:
does not support fall through in the switch statement, and i have not come across any replacement.

*These are the largest of the differences i have noticed so far, if i come across any further impending differences i will be sure to add them.

Questions/Comments: william_a_wilson@hotmail.com
-William. § (marvin_gohan)

Previous recipe | Next recipe |
 
  • Anonymous
    C# does support the switch statement.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheswitchstatement.asp

    The only difference with C++ that I can see is that C# does not allow fall through.

    -
    Peter
  • William_Wilson
    My bad, guess i should have been more specific, I have always felt that the fall through on a switch pretty much makes the switch, without it, time saving is minimal to writting several if statements.

    Thanks for the post, keeps me on my toes that much more, and lets me know that people are actually reading these things seriosusly!

    I will get this recipe edited to include the correction :)
    -William. § (marvin_gohan)
  • Anonymous
    Just glad to be of use :)

    I know what you mean about the fall through, but if you want to be very dirty you can use goto's to get the same behaviour. Although I'm not sure if it's wise recommending that!

    -
    Peter
  • Guidii
    A couple others:

    Garbage collection (C#) vs programmer-managed memory (C++).
    No deterministic finalization in C#.
    C++ Templates are a compile-time concept, while C# generics are a run-time concept.
blog comments powered by Disqus