Comparison between C ++ and C #: Object-oriented, interface, and abstract class

Source: Internet
Author: User
Tags modifiers
Document directory
  • The above is a conceptual thing. Now let's talk about the syntax details.
  • C ++ abstract class
An Object-Oriented story

 

The most important concept of object-oriented programming language is undoubtedly class, and everything is object. (but if I write code in front of a computer every day to study object-oriented languages, it is very difficult to find my wife. none of the girls in the world are your targets .) class is an abstract concept. It is similar to the classification of things in our real life, such as beauty, handsome guy, second milk, and second generation, represents a type of thing with similar attributes. after each class is instantiated, it is an object. A specific beauty in the beauty category is an object and may become your object. in the real world, all things and events can be divided into one category. When a brother called vitegenstan stays in jail, he is so anxious to split the whole world from the perspective of linguistic logic, what are some atomic events. then these atomic events can form a complex real world through logical combinations. his book is called <logical philosophical theory>. it is rumored that the idea of object-oriented is evolved from his thoughts, but the legend is the same as that of gossip. many smart people may come to the same conclusion when thinking about the problem. some people say that the invention of binary is also inspired by the Yijing, and the eight-Dimensional diagram has a binary principle.

 

Comparison between abstract classes and interfaces

Abstract class is slightly different from interface in syntax. It can be regarded as a special class. it is of great significance only from the perspective of design. It is only possible for architects to consider things, coders like us are still honest in understanding the syntax and refining the code. an abstract class fully embodies the concept of a class. It abstracts a class of things with the same attributes, except that it is used as a parent class for subclass inheritance. therefore, many functions can only be declared there without defining implementation. the interface does not fully reflect the concept of classes, it does not abstract the same attributes, or it defines a series of behavior rules. the so-called interface, as the name suggests, we have a uniform standard for the flat fashion bulb, USB interface on the computer, and ear plug interface. Your USB flash drive can be plugged into any computer, if there is no such unified interface standard, it cannot be inserted. Interfaces are generally used for calling between different projects. Other items in the project are encapsulated. You only need to know the interface, in particular, if C ++ wants to call the COM component of C #, the COM component must use the interface. therefore, you only need to call the functions in the interface. of course, the interface cannot be instantiated, but you can point to the class that implements the interface through the interface pointer. If there is no pointer in C #, you can then infaceclass
Instance = new classcompleteinterface (); this is actually very similar to the pointer. in C ++, each class can have a head file. The header file is actually like an interface. it also embodies the idea of interfaces. however, the header file concept does not exist in C. however, C # has a concept of partial class, so that the code of a class can be distributed in multiple files. If you want to simulate the header file of C ++, you can use it.

Besides being useful in different projects, interfaces are also useful in the same project. especially in C #, because the class cannot implement multiple inheritance as in C ++, it can only be a single inheritance, but the interface can inherit more. if the project needs to expand some functions, add new functions in a class. you can add an interface to declare the functions to be implemented, and then inherit the interface from a class and implement it. from the designer's point of view, one is to hide the implementation details, that is, the so-called encapsulation. the other is to facilitate expansion and add new content.

Designers first use data modeling to abstract a system and divide it into small pieces. each small block corresponds to a class. of course, they do not care about the specific code implementation details during the design, so the classes designed are basically the declaration of some functions. that is, abstract classes and interfaces (this is not necessarily a concept in code. It may be abstract functions, interfaces, but general classes when it is finally converted into code)

 

The above is a conceptual concept. Now let's talk about the syntax details C # Interface Usage

Public Delegate void iampointer (string input); // This is not required, but is defined here only for the following event.

Interface weiwenhp

{

// String name; // This will cause an error. fields are not used in the interface.

Int number {Get; set;} // attribute

Void doit (string cmd); // Function

Int this [int Index] {Get; set;} // Indexer

Event iampointer myevent; // event

}

1. the interface cannot be instantiated, but if there is a class Arwen that inherits weiwenhp. you can use weiwenhp instance = new Arwen (); // This will not cause errors. weiwenhp instance = new weiwenhp (); No way

2. When defining an interface, the available access modifier public or internal. The default value is internal (only accessible within the Assembly). However, the function declaration in the interface cannot use any access modifier.

3. the interface can only contain functions, attributes, indexers, and event declarations. all three are newly added features in C. the attribute and indexer can be simply understood as a special function that encapsulates the fields inside the class and prompts an interface. the attribute is typically encapsulated by the basic type, and the indexer typically encapsulates the array type. events can be simply understood as encapsulation of proxies, while proxies are somewhat similar to function pointers. in addition, static and virtual functions cannot be declared in the interface.

 

C # Abstract class

It is basically the same as a general class, except that a modifier abstract is added during definition, for example, abstract class myclass {}

There are only a few minor differences from general classes.

1 .. first, it cannot be instantiated, but it can be instantiated like an interface. in other classes, abstract functions can be used (but there can be no Abstract Functions), such as public abstract void fun (); // abstract functions cannot be used in general classes.

2. access modifiers such as functions in an abstract class cannot be private, because they are designed to be inherited by others. because the class is private by default when no modifier is added inside, all functions in the abstract class must be added with modifiers. except for the field, it can be private.

3. Apart from the above differences, the abstract class is exactly the same as the general class. In fact, if you take a general class and add a abstract directly, it will become an abstract class. There is no problem at all.

 

C ++ Interface

It is said that there is no interface in the Standard C ++. The interfaces used at ordinary times are actually implemented through some macros, And the principle behind them is still the virtual pure base class. This remains to be studied.

 

C ++ abstract class

The abstract class of C ++ differs greatly from that of C. the mark of the abstract class in C # is that there is an abstract in front of the class. then the class is similar to the general class.

C ++ abstract classes do not use abstract keywords. The mark of the abstract keywords is that there must be at least a pure virtual function in the class (this is a concept unique to C ++), which is not in C.

For example

Class Arwen

{

Int age;

Public:

Virtual void fun (INT num) = 0; // This is a pure virtual function.

}

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.