[C # & C ++] class

Source: Internet
Author: User
I have been eager to learn C ++ for a long time, but I have never really entered the C ++ world because I have always insisted on C #. Article Microsoft only has c ++ to C #, and few have written articles about C #'s entry into C ++. I am very interested in game development. As I can see, C ++ is the first choice in the game industry. I mean large games, not mobile phones. At the same time, a friend will ask you to write a small Program For example, you can't ask a calculator to install it. net Framework, so it is necessary for me to learn C ++. I don't know how big brother and sisters think. Here I want to record the differences and connections between C # And C ++. Now let's take a look at classes in C # And C ++ and their inheritance.

[C ++] Here we will focus on C ++ and focus on VC ++. First, let's take a look at the composition of c ++ files. We all know that C ++ has header files (. h), and the implemented file (. CPP), declare the variables and methods in the header file, and implement the methods in the CPP file. The implementation file should contain the header file of the current header file (a bit like the namespace introduced by using in C #), and implement the methods declared in the current header file. Define a class format in C ++ as follows (usually stored in the header file ):

1 Class < Class Name >
2 {
3 Private :
4 < Declaration list of private data members and private member functions > ;
5 Public :
6 < Declaration list of public data members and public member functions > ;
7 Protected :
8 < List of declarations for protecting data members and member functions > ;
9 };
10

[C #] It is easy to define a class in C.. NET introduces a namespace to better differentiate the layers of each class. Each class belongs to a specific namespace. Define a class as follows: Using introduces the namespace to be used

Namespace
{
Public/private/protected/internal [statics] <Class Name>
{
Member :...
}
}

[C ++] C ++ is also object-oriented.Programming LanguageOf course, it can also be inherited. It allows multiple inheritance. A class can have several base classes. The member visibility in the inherited base class is specified in the inheritance. In MFC, all classes directly or indirectly inherit from the cobject class, this is the same as that of all classes in C # inherited from objects. The format is as follows:

Class <name of the derived class>
: [<Derived method>] <Base Class Name>

{


... // List of newly added member declarations of the derived class

};

<Derivation method>:

Public [Common]: The access level of all base class members is retained except for private members. That is to say, in the current class, you can access the public and protected members in the base class, and in the subclass of the current class, you can also access the members of the grandfather class (this is the same as the inheritance in C );

Protected: All the public and protected members of the base class become protected members in the derived class (in the current class or subclass, all the members of the base class are protected ), you can only access non-Private Members of the base class in the member functions of the derived class and the member functions of the derived class.

Private [Rarely used]: All public and protected members of the base class become private members in the derived class, and only allow access to non-Private Members of the base class in the member functions of the derived class.

[C #] the inheritance in C # is the same as the public inheritance in C ++, but C # can only inherit one base class, but can inherit multiple interfaces. Classes that decide to inherit or do not inherit can also be limited by constructors or sealed, so that the current class cannot be inherited.

Friendly reminder (good naming can helpCodeReadability ):

The class name in C ++ starts with C (and C # does not have this provision );

G _: global scope;

M _: member variable;

L _: local scope.

I can know their differences through continuous learning. Many people are using them now. net, and many people intend to turn to C ++. I have just started to learn C ++ and recorded what I have come up with. I hope I can give my colleagues some help, you can also consolidate your learning !!!

Related Article

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.