C#
  Home arrow C# arrow Page 2 - C++ Classes Vs. C# Classes
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C++ Classes Vs. C# Classes
By: Jordan Leverington
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2002-01-22

    Table of Contents:
  • C++ Classes Vs. C# Classes
  • How have classes changed?
  • Limiting access to classes with class modifiers
  • Virtual functions in C and C#
  • Polymorphism in C and C#
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C++ Classes Vs. C# Classes - How have classes changed?


    (Page 2 of 6 )

    As you may well know, C# is based partly on C++ and partly on the Java language syntax. The rest of the C# language syntax was created totally from scratch, giving it a more modern design.

    This is noticeable as soon as you begin to create classes with C#. Let's start of by looking at how we can create and instantiate a very simple class with both C++ and C#:

    C++ Version:

    #include <iostream>
    class MyClass
    {
        public: void doSomething()
        {
            std::cout << "This is some text";
        }
    };
    void main()
    {
        MyClass mc;
        mc.doSomething();
    }


    C# Version:


    using System;
    class MyClass
    {
        public void doSomething()
        {
           Console.WriteLine("This is some text");
        }
    }
    class EntryPoint
    {
        public static void Main()
        {
            MyClass mc = new MyClass();
            mc.doSomething();
        }
    }


    There are a couple of differences between these two snippets of code. Firstly, our C++ example uses the #include directive to physically include the contents of the iostream.h library. The C# code snippet contains a using directive which tells the C# compiler that we would like access to all of the other namespaces and classes contained within the System namespace. Using directives are not the same as C++ #include directives: using directives only tell the C# compiler the name of the namespace we're after (in this case, the level one namespace, system), and don't physically include any other files into our C# application.

    Secondly, in C#, the main function is now declared as Main (note the capital M).

    Thirdly, a C++ class declaration always has a semi-colon after its last curly bracket. In C#, this semi-colon is optional and is usually left out.

    Fourthly, you'll notice that in C#, we have to explicitly append the access specifier to each of our method and member declarations. If we don't, then they are assumed to be private (accessible only by the instance of the class in which they were created) just like in C++. In C#, there are five access specifiers that we can specify to denote how our class members and methods can be used:

    • public: Accessible by any other class
    • private: Accessible only by the class in which it is declared
    • protected: Accessible only by the class in which it is declared, as well as any derived classes
    • internal: Accessible only from within the same assembly (in C#, an assembly is a package of inter-related data that contains both code and meta data)
    • protected internal: Accessible only by the class in which it is declared, as well as any derived classes in the same source code file
    Lastly, just like in Java, C# methods can also be declared as static. This works in the same way as both C#'s and C++'s static keyword for variables. In C#, we can create and call a static method of a class like this:

    using System;
    class MyClass

        public static void doSomething()
        {
            Console.WriteLine("This is some text");
        }
    };
    class EntryPoint
    {
        public static void Main()
        {
            MyClass.doSomething();
        }
    }


    Notice how we refer directly to the class declaration and not an instantiation? This is an extremely handy addition to the C# language, and saves us both time and memory resources (i.e. We don't have to instantiate a class just to call one of its generic methods).

    More C# Articles
    More By Jordan Leverington


       · if we don't specify any access modifier to a class then it is internal by default...
     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway