Home arrow C# arrow Page 2 - C++ Classes Vs. C# Classes
C#

C++ Classes Vs. C# Classes


With its implementation of C#, Microsoft have given way to some of C++'s nitty-gritty object-orientated features such as templates, and have also changed the way we create classes. In this article, Jordan compares and contrasts classes in both C++ and C#. He focuses on demonstrating how Microsoft has changed certain aspects of creating and using classes in C# including reference types, virtual functions and polymorphism.

Author Info:
By: Jordan Leverington
Rating: 4 stars4 stars4 stars4 stars4 stars / 39
January 22, 2002
TABLE OF CONTENTS:
  1. · C++ Classes Vs. C# Classes
  2. · How have classes changed?
  3. · Limiting access to classes with class modifiers
  4. · Virtual functions in C and C#
  5. · Polymorphism in C and C#
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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).

blog comments powered by Disqus
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#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials