C#
  Home arrow C# arrow Page 3 - 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 - Limiting access to classes with class modifiers


    (Page 3 of 6 )

    Gone are the days where you could only limit access to specific methods and members within a class and not the actual class itself. C# allows us to limit access to class instantiation by simply appending that classes declaration with a class access modifier, just like we did for methods and members in the previous section.

    In C++ we can't restrict access to a class as a whole. Take a look at this C++ class declaration:

    class Car
    {
        public: Car();
        Car(Car &c);
        virtual ~Car();
        private: int numCars;
        Car* previous;
        Car* next;
    };


    We have two types of access specifiers here: public and private. If the Car class is derived or instantiated from within our code, then there's no way that we can make it so that it must be inherited only from within the same source code file, or even make it so that it can't be used as the base class for a derived class.

    In C#, this has all changed. We can append our class declarations with class access modifiers to restrict both access to its internal members and methods, as well as instantiation rights. These eight access specifiers are:

    1. public: This class is accessible to all other classes. If a class is declared without explicitly specifying an access modifier for it, then it's public by default.
    2. private: Accessible only by the class in which it is declared.
    3. protected: Accessible only by the class in which it is declared, as well as any derived classes.
    4. protected internal: Accessible only by the class in which it is declared, as well as any derived classes in the same source code file.
    5. 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).
    6. sealed: Prevents a class from every being derived. If another class tries to use this class as its base class either directly or indirectly then the C# compiler will raise an error.
    7. abstract: Similar to the concept of a pure virtual function in C++, an abstract class can't actually be instantiated. It contains a signature, but can only be used when it is the base class of a derived class.
    8. new: Using the new keyword as an access modifier for a nested class allows us to hide an inherited method of a parent class by providing the compiler with a new version of that class.
    For example, if I wanted to create a class that could never be used as the base class of a derived class, then I would use the sealed access modifier, like this:

    sealed class Car
    {
        public void paintCar()
        {
            // Code to paint car goes here
        }
    }


    If I wanted to create a RedCar class that is derived from the Car class like this:

    internal class RedCar : Car
    {
        // Won't work.
    }


    Then the C# compiler would spit an error indicating that a sealed class cannot be inherited from:

    error CS0509: 'RedCar' : cannot inherit from sealed class 'Car'

    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 4 hosted by Hostway
    Stay green...Green IT