Home arrow C# arrow Page 5 - 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 - Polymorphism in C and C#
(Page 5 of 6 )

Polymorphism allows an entity to take a variety of representations. Treating objects polymorphically is very similar in both C++ and C#. Let's look at a simple implementation in both languages:

C++ Version:

#include <iostream>
#include <string>
using namespace std;
class Person
{
    public: Person()
    {
        classType = "Person";
    }
    friend void ShowType(Person& p);
    private:
        string classType;
};
class Manager : public Person
{
    public: Manager()
    {
        classType = "Manager";
    }
    friend void ShowType(Person& p);
    private: 
        string classType;
};
void ShowType(Person& p)
{
    cout << p.classType << endl;
}
void main()
{
    Person p;
    Manager m;
    ShowType(p);
    ShowType(m);
}


C# Version:


using System;
class Person
{
    public Person()
    {
        classType = "Person";
    }
    public string classType;
}
class Manager : Person
{
    public Manager()
    {
        classType = "Manager";
    }
    public new string classType;
}
class EntryPoint
{
    public static void ShowType(ref Person p)
    {
        Console.WriteLine(p.classType);
    }
    public static void Main()
    {
        Person p = new Person();
        Person m = new Manager();
        ShowType(ref p);
        ShowType(ref m);
    }
}


In the C# sample above, we have two classes: Person, which is a base class, and Manager, which is derived from the Person class. Within our EntryPoint class, we have created a static function named ShowType. ShowType's signature looks like this:

public static void ShowType(ref Person p)


Notice the ref keyword specified in the argument list? This tells the C# compiler that we will be passing a reference to a Person object.

------------------------------- Update 12th August, 2003 -----------------------------------

The phrase that was omitted was:

If we omitted the ref keyword, then C# would pass any objects by value, creating a copy and passing it to the function.

Reason:

What is happening when there is no ref keyword is that a copy of the variable pointing to the object (the object reference) is made, and that this copy of the reference is passed by value.

Thus far, it makes no difference for objects whether "they" are passed by value or reference. You cannot have an object variable, only references to objects.

Therefore, it is impossible to pass an object by value. You can only pass its reference by value, so that the object is in effect passed by reference, even if you do not specify the ref keyword.

--------------------------------------- End of Update ------------------------------------------

The equivalent C++ code uses the reference operator, like this:

void ShowType(Person& p)


In C++, the "&" reference symbol can be confusing for new programmers, especially those coming from Visual Basic. In the Main function (entry point) of our C# sample, we create two new Person objects, p and m:

Person p = new Person();
Person m = new Manager();


It's important to note that C# doesn't use the new keyword in the same way that C++ does. In C#, the new keyword simply creates an instance of an object. The object is still created on the managed heap, but it doesn’t return a pointer to that objects address in memory. In our C# code sample, we create two new Person objects. The second object, m, is a Manager object however, and will call the constructor of the Manager class, not the constructor of the Person class. To demonstrate C#'s polymorphic abilities, we pass both of our Person objects (remember m is a Manager object which is derived from Person, so polymorphically speaking it is actually a Person object) to the ShowType function as references:

ShowType(ref p);
ShowType(ref m);


Note that if you declare a function parameter to accept a reference, then you must append that parameter with the ref keyword when you are passing it in as well. As far as the ShowType function is concerned, it's only dealing with Person objects. Internally, C# can tell that m is derived from a Person object, and thus treats it as one. The output from calling the ShowType function with references to both p and m looks like this:

Person
Person


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