C#
  Home arrow C# arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 - Virtual functions in C and C#


    (Page 4 of 6 )

    Both C++ and C# support virtual functions. Consider the following C++ sample in which we have two classes, one named Base and the other named Derived:

    #include <iostream>
    using namespace std;
    class Base
    {
        public:
            void doWork()
            {
                cout << "Base class working";
            }
        protected:
            virtual void doWork1() = 0;
    };
    class Derived : public Base
    {
        public: void doWork2()
        {
            cout << "Derived class working";
        }
        void doWork1()
        {
            cout << "Dervied pure virtual function working";
        }
    };
    void main()
    {
        Derived d;
        d.doWork1();
    }


    The Base class contains two functions: one named doWork, and another pure virtual function named doWork1. doWork1 can only be used if it is implemented in a class derived from Base. In our Derived class (which is publicly inherited from Base), we have a new function, doWork2, as well as the actual implementation of the pure virtual function which was derived from Base, named doWork1.

    Implementing this sort of functionality in C# is easy. Take a look at the following C# source code:

    using System;
    abstract class Base
    {
        public void doWork()
        {
            Console.WriteLine("Base class working");
        } 
        public abstract void doWork1(); 

    class Derived : Base 

        public void doWork2() 
        { 
            Console.WriteLine("Derived class working"); 
        } 
        public override void doWork1() 
        { 
            Console.WriteLine("Dervied pure virtual function working"); 
        } 
    }; 
    class EntryPoint 

        public static void Main() 
        { 
            Derived d = new Derived(); 
            d.doWork1(); 
        } 
    }


    By using the abstract access modifier for both our class declaration and the declaration of its doWork1 method, we are implementing the same sort of pure virtual function behaviour that we have in our C++ sample. This tells the C# compiler that the Base class cannot be referenced unless it is the base class of another class that contains an implementation of the doWork1 function.

    To tell the C# compiler that we want to override the abstract version of the doWork1 method in our Derived class, we prefix it with the override keyword. When the C# compiler sees a function prefixed with the override keyword in a derived function, it will check its base class for a function with the same signature and always use the derived one in its place; unless we explicitly call the base classes version.

    To access any inherited member or function in a base class from a derived class, C# provides us with a class alias named base. Using this class, we can explicitly reference methods and members of our base class from within a derived class, like this:

    using System;
    class first
    {
        public void writeIt()
        {
            Console.WriteLine("Writing from base class"); 
        }
    }
    class second : first
    {
        public second()
        {
            base.writeIt();
        }
    }
    class EntryPoint
    {
        public static void Main()
        {
            second s = new second();
        }
    }


    In our example above we have one base class (first) and one derived class (second). When a new instance of the second class is created, its constructor automatically calls the writeIt method of its base class, which uses the Console.WriteLine method to output some text to the screen. This brings us to another feature common to both C++ and C#: polymorphism.

    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++ also have sealed classes. Sealed is implemented by derived from specific class...
     

    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
    Stay green...Green IT