C#
  Home arrow C# arrow Page 5 - 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 - 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

    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