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

Source: Internet
Author: User
Tags visual studio 2010

// ================================================ ====================================
// 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:
Class cmyclass <br/>{< br/> // class members <br/> }; 

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:
Internal class cmyclass <br/>{< br/> // class members <br/> }; 

If you want to use this code in other projects, you must use public for modification, such:
Public class cmyclass <br/>{< br/> // class members <br/> }; 

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:
Public abstract class cmyclass <br/>{< br/> // class members <br/>} 

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:
 // Parent class <br/> class cmybase <br/>{< br/> // declare the constructor and destructor as private <br/> PRIVATE: <br/> cmybase () {}; <br/> virtual ~ Cmybase () {}; <br/>}; </P> <p> // subclass <br/> class cmyclass: <br/> Public cmybase <br/>{< br/> Public: <br/> cmyclass () {}; <br/> virtual ~ Cmyclass () {}; <br/>}; </P> <p> // errors occurred during compilation of this statement <br/> cmyclass AB; <br/>

When cmyclass inherits from cmybase and declares a cmyclass object, the compiler reports the following error:
Error c2248: 'cmybase: 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:
Class cmybase <br/>{< br/> Public: <br/> cmybase () {}; <br/> virtual ~ Cmybase () {}; </P> <p> protected: <br/> // defines a pure virtual function, so that this class cannot be instantiated <br/> virtual void abstract () = 0; <br/>}; </P> <p> class cmyclass: <br/> Public cmybase <br/>{< br/> public: <br/> cmyclass () {}; <br/> virtual ~ Cmyclass () {}; </P> <p> protected: <br/> // You must implement pure virtual functions, otherwise, this class cannot be instantiated. <br/> virtual void abstract () {}; <br/>}; </P> <p> // a pure virtual function is available, this statement is incorrect <br/> cmybase mybase; </P> <p> // The statement is correctly passed <br/> 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.