C ++ vs c # (8): Class Definition

Source: Internet
Author: User

// ================================================ ====================================
// TITLE:
// C ++ vs c # (8): Class Definition
// AUTHOR:
// Norains
// DATE:
// Thursday 17-jaruary-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================

1. Class Definition

Both C ++ and C # Have the object-oriented feature. Naturally, both of them have this kind of thing. Whether it is C ++ or C #, the key words of the defined class are class. That is to say, both languages can declare a class as follows:
View plaincopy to clipboardprint?
Class CMyClass
{
// Class members
};
Class CMyClass
{
// Class members
};

So far, the C ++ statement is like this. However, C # is still evolving on this basis. There are two keywords available before class: internal and public. By default, the class declaration is internal and only the code of the current project can access it. In other words, the above declaration is equivalent to this in C:
View plaincopy to clipboardprint?
Internal class CMyClass
{
// Class members
};
Internal class CMyClass
{
// Class members
};

If you want to use this code in other projects, you must use public for modification, such:
View plaincopy to clipboardprint?
Public class CMyClass
{
// Class members
};
Public class CMyClass
{
// Class members
};

In addition to these two access modifiers, you can also specify whether the class is abstract or sealed. Abstract keywords are abstract, indicating that they cannot be instantiated and can only be inherited. sealed indicates that they cannot be inherited. The two keywords are mutually exclusive.

If we declare an abstract public class, the code can be written as follows:
View plaincopy to clipboardprint?
Public abstract class CmyClass
{
// Class members
}
Public abstract class CmyClass
{
// Class members
}

The order of public and abstract keywords is irrelevant and can be reversed at will.

Let's discuss a very interesting question. C ++ cannot implement functions similar to C # Without these two keywords? Of course not. Let's take a look at how to use C ++ to implement these two features of C.

First, we can see that the class cannot be inherited. Although C ++ does not have the sealed keyword, it is not necessary to implement this function. We only need to declare the constructor or destructor as private, as shown in the following code:
View plaincopy to clipboardprint?
// Parent class
Class CMyBase
{
// Declare the constructor and destructor as private
Private:
CMyBase (){};
Virtual ~ CMyBase (){};
};

// Subclass
Class CMyClass:
Public CMyBase
{
Public:
CMyClass (){};
Virtual ~ CMyClass (){};
};

// This declaration will cause an error during compilation
CMyClass AB;
// Parent class
Class CMyBase
{
// Declare the constructor and destructor as private
Private:
CMyBase (){};
Virtual ~ CMyBase (){};
};

// Subclass
Class CMyClass:
Public CMyBase
{
Public:
CMyClass (){};
Virtual ~ CMyClass (){};
};

// This declaration will cause an error during compilation
CMyClass AB;

When CMyClass inherits from CmyBase and declares a CmyClass object, the compiler reports the following error:
Error C2248: CMyBase: cannot access private member declared in class CMyBase
Error C2248: CMyBase ::~ CMyBase: cannot access private member declared in class CMyBase

By declaring constructor and destructor as private, the class cannot be inherited.

Next let's look at the abstraction, that is, the class cannot be instantiated and can only be inherited. This C ++ can also be implemented, but it is not perfect. We all know that C ++ classes cannot be instantiated under what circumstances? When a class contains pure virtual functions. With this feature, we can also achieve the purpose that classes cannot be instantiated, as shown in the following code:
View plaincopy to clipboardprint?
Class CMyBase
{
Public:
CMyBase (){};
Virtual ~ CMyBase (){};

Protected:
// Define a pure virtual function so that the class cannot be instantiated
Virtual void Abstract () = 0;
};

Class CMyClass:
Public CMyBase
{
Public:
CMyClass (){};
Virtual ~ CMyClass (){};

Protected:
// You must implement pure virtual functions. Otherwise, this class cannot be instantiated.
Virtual void Abstract (){};
};

// There is a pure virtual function, and this declaration is incorrect
CMyBase MyBase;

// This statement is correct
CMyClass MyClass;
Class CMyBase
{
Public:
CMyBase (){};
Virtual ~ CMyBase (){};

Protected:
// Define a pure virtual function so that the class cannot be instantiated
Virtual void Abstract () = 0;
};

Class CMyClass:
Public CMyBase
{
Public:
CMyClass (){};
Virtual ~ CMyClass (){};

Protected:
// You must implement pure virtual functions. Otherwise, this class cannot be instantiated.
Virtual void Abstract (){};
};

// There is a pure virtual function, and this declaration is incorrect
CMyBase MyBase;

// This statement is correct
CMyClass MyClass;

Although we have implemented the objective that classes cannot be instantiated through pure virtual functions, it also brings the burden of pure virtual functions, so that the subclass has to implement this meaningless pure virtual function in order to instantiate it. In this respect, C # is indeed much easier than C ++.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.