Object-oriented programming and interface-oriented programming

Source: Internet
Author: User
Tags data structures exception handling

1. Implementation-oriented programming and interface-oriented programming:

Implementation-oriented Programming: Programming code logic and implementation

Interface-oriented programming: programming interfaces; interface-oriented programming in C + + is implemented through abstract classes, the first step is class inheritance reuse, and the second is to define object families with the same interface; Customers simply manipulate objects based on interfaces in abstract classes, without knowing that they use specific types of objects. There is no need to know what classes they use to implement the object.

Design pattern Design Principle 1: Programming for interfaces, not for implementation. Instead of declaring a variable as a strength object of a specific class, you let it conform to an interface defined by an abstract class.

2. Comparison of class inheritance with object composition:

Class Inheritance: black-box multiplexing; (Implementation) class inheritance is statically defined at compile time; (advantages) can be directly used, class inheritance can easily change the implementation of the reuse; (disadvantage) the implementation of the parent class inheritance cannot be changed again, class inherits "corrupt encapsulation", and if the inherited class is not suitable for the new application, The parent class needs to override the function or take advantage of a more appropriate class, and dependencies limit flexibility.

Object combination: White elephant multiplexing; (implementation) dynamically defined at run time by obtaining references to other objects, requiring more careful definition of the interface; (advantages) without damaging encapsulation, one object can be substituted for another, and as long as the interface is the same, the object's implementation is based on interfaces and there are fewer dependencies.

Design Pattern Principle 2: Prioritize the use of object combinations rather than class inheritance.

The difference between 3.C and C + +:

(1) c is a structured language, which focuses on algorithms and data structures. The first consideration of C program design is how to process the input (or the environment condition) to get output (or implement process (transaction) control) through a procedure.

(2) C + +, the first consideration is how to construct an object model, so that the model can fit with the corresponding problem domain, so you can obtain the object's state information to obtain output or implementation process (transaction) control. So the most important difference between C versus C + + is that they are not the same way of thinking about solving problems. C + + is more advanced than C, because "the concept of design has been incorporated into C + +."

Compared to the C language, C + + has the advantages of:
 
1. Support object-oriented programming, referencing the concept of classes (class). In the C language of the array, structures, enumerations, and so on can be implemented uniformly in C + +, and not just by adding member variables to a class, but also by encapsulating functions (i.e., methods) associated with member variables into a class, which is better for the integrity of the program. It also supports all the algorithms for classes, such as constructors, destructors, overloads, inheritance, polymorphism, and so on.
 
2. Introducing the new and delete operators, the essence of new is the malloc in the C language, and the essence of delete is free in C. The advantage of new is that the code is concise, and since new is an operator and not a function, it eliminates the inevitable coercion type conversion in malloc.
 
3. You can define variables at any time without necessarily defining them at the beginning of the program.
 
4. For type checking more stringent, C language of A and &a is not divided, and C + + is differentiated, in compile will be prompted. So scanf ("%d", a); This statement does not detect errors in C language, the program crashes at run time, and in C + + prompts that the parameter type cannot be converted from an integer to a pointer to an integer.
 
5. Introduce the concept of input/output stream. In C language to output a sentence to give the format string, and in C + + as long as the parameters of the line, the system will directly select the corresponding format according to the parameter type output.
such as the C language of this statement: printf ("%d\n", a);
Just write:cout<<a<<endl; in C + +

C's relationship with C + +: C is a subset of C + +: C Implementation of C + + in the process of control and other related functions, while in C + + (called it "c+"), compared to the original C has also been strengthened, the introduction of overloading, inline functions, exception handling and so on, C + + It expands the content of object-oriented design, such as class, inheritance, virtual function, template and containment class and so on. In C + +, data encapsulation, type These are nothing new, need to consider such as: the choice of object granularity, object interface design and inheritance, combination and inheritance and so on.

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.