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


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