Some differences between C# and C++ - Entry 1

Up to chapter 8 now in my Visual C++ book. Here are some of the things I have learned so far:

  • You declare the prototypes of your functions and member variables in a header file before you use them (usually anyways).
  • Your function code is in a source file (.cpp) with no class {} wrapping them (that is in the header file).
  • In unmanaged C++ you use the keyword "delete" to clean up your objects when you are done with them.
  • Managed classes are defined with __gc in front of them (ie. __gc class Foo {})
  • In the header file, there is a section for different scoped items like (classes also end with a ";"):
    __gc class Foo {
    public:
     Foo();
     ~Foo();
     void SomeMethod();
    private:
     int SomeID;
    };
    
    Instead of C#:
    class Foo {
     private int _someID;
     public Foo()
     {}
     ~Foo()
     {}
     public void SomeMethod()
     {}
    }
    
  • It seems for strings you use "S" instead of "@" (ie. Console::WriteLine(S"Hello World"); instead of Console.WriteLine(@"Hello World");), only noticed this through usage - not 100% sure if it is a 1-to-1 comparison on this one
  • Unlike VB.Net and C#, C++ can force a Finalize by using the delete keyword, which will then call the destructor and then the Finalize method
  • Syntax for implementing an interface seems a little different - have to add "public"
    __gc class Tester : public IDisposable {
    }
    

posted on Thursday, April 29, 2004 5:27 PM

Feedback

# re: Some differences between C# and C++ - Entry 1

"It seems for strings you use "S" instead of "@" (ie. Console::WriteLine(S"Hello World"); instead of Console.WriteLine(@"Hello World");), only noticed this through usage - not 100% sure if it is a 1-to-1 comparison on this one"

They're not quite the same thing. In C#, putting an @ before a string specifies that this string is a file path, so you don't have to escape \'s, i.e. @"C:\Windows\System32" instead of "C:\\Windows\\System32".

In MC++, the S denotes that it is a managed string (System::String*) rather than an unmanaged string (char*).

"Syntax for implementing an interface seems a little different - have to add "public""

The reason for this is due to how C++ inheritance works. There are different kinds - public, protected, private, and virtual. You rarely use anything other than public, but basically the main difference is how the access level of inherited member is changed in the derviced class. I'm sure that'll be in your book later. :)
4/29/2004 3:28 PM | milbertus

# re: Some differences between C# and C++ - Entry 1

Milbertus: Thanks for pointing that out! Its great to have readers like you who know what they are doing and comment on things (whether it be me screwing something up or whatever). Great comments
4/30/2004 12:30 AM | Jason haley

# re: Some differences between C# and C++ - Entry 1

c++ suport multiple inheritance, c# does not support multiple inheritance
12/15/2004 9:05 PM | Hitesh

# re: Some differences between C# and C++ - Entry 1

In c# the first alphabet of the class Main is capitalised where as in c++ it is not
5/4/2005 7:17 PM | Rupesh

# re: Some differences between C# and C++ - Entry 1

The switch statement: Unlike the C++ switch statement, C# does not support fall through from one case label to another.
8/19/2005 3:32 AM | Navin Gupta

# re: Some differences between C# and C++ - Entry 1

No header files or #include directives in C#: The using directive is used to reference types in other namespaces without fully qualifying the type names.
8/19/2005 3:34 AM | Navin Gupta

# re: Some differences between C# and C++ - Entry 1

The foreach keyword allows you to iterate through arrays and collections.
8/19/2005 3:35 AM | Navin Gupta

# re: Some differences between C# and C++ - Entry 1

C++ can run on unix/linux box,but c# cant.
3/16/2006 6:21 AM | das

# re: Some differences between C# and C++ - Entry 1

We can implement multiple inheritance in C# by using Interface
4/10/2006 9:58 PM | Simod

# re: Some differences between C# and C++ - Entry 1

In the case of switch statement case-fallthrough is not allowable.
4/19/2006 2:02 AM | Pramod

# re: Some differences between C# and C++ - Entry 1

stupid differences
7/27/2006 4:17 AM | raj

# re: Some differences between C# and C++ - Entry 1

we can use pointer variable in c but not in c#,
vartual function.
1/3/2007 9:36 PM | chitta ranjan mishra

# re: Some differences between C# and C++ - Entry 1

Hi,

Like we have _getvideoconfig(&vc) in c/c++ to get video mode of the screen, what is the function used to get the video mode in C# ?

Please reply.

Thank you,

Harish.R.Hanchinal
5/2/2007 12:25 AM | Harish R Hanchinal

# re: Some differences between C# and C++ - Entry 1

c++ is not an purely object oriented..

c# is purely object oriented
12/16/2007 7:41 AM | differnce

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *