[C ++ programming language design Note 1] basic introduction to object-oriented programming abstraction, inheritance, and rewriting

Source: Internet
Author: User
Today is a good day. Not only have I been an apprentice, but I have learned some about C ++, because I liked plug-ins some time ago, I also used some MFC knowledge and some windows APIs programming, but I still have no fundamental understanding of C ++. I have never had a computer teacher in the real sense (those who only read the courseware to me), because I am the only one along the way, and the people around me are all studying and researching: people who want to take college exams in bioengineering know how popular this subject is-the top ten fraud majors in China. I gave up all my major and turned my head to computers, probably because I started playing "hacker technology" in high school and my admiration for computers. Thanks to this master and a girl, even though we know each other on the internet ..... some time ago, I did not have a deep understanding of Windows core programming. Everyone knows that it is difficult for me to understand the essence of a good book without reading it several times. net 《. the net entry-level classics are four times in total. I don't know what to say for the first time; the second compilation is like this; the third is basically a bit familiar; the fourth time, he actually brought me from VB to C #, and he was unable to extricate himself. In fact, the intermediate version has also changed. Now it should be version 4th. <doubt> ). Today, the master recommended c ++ Program Language design I will start reading it slowly when I come back from work this evening. The father of C ++ should have a good book. Article I will record some of my understanding of C ++, and I will take his semester from 0. His writing method is worthy of being a generation of masters, and it is easy to bring me into the status.

In C ++, header files and implementation files are used together to complete a class or other methods. More often, one is Declaration and the other is definition, declarations allow people to know how many things you actually have between different program pages or different DLL pages. Here, they can also explain how many things you want to know; the definition is how the declared function (method) completes one thing, that is, the implementation of the method. A stack example is given in C ++ programming language design. h declares a class, and in the stack. CPP implements the method of this class (including header files of course). When we need to use the Stack class on other pages, we need to reference it to stack. h file, as shown in:

Let's take a look at the definition of stack. h:

Class Stack
{
Public:
Stack (int s );
~ Stack (void );

PRIVATE:
Const int max_size;
Char * V;
Int Top = 0;

Public:
Void push (char C );
Char POP ();
Class overflow {}; // exception
Class underflow {}; // exception
Class badsize {}; // exception
};

The following is the definition of stack. cpp for implementing stack. h:

1 # Include " Stack. h "
2
3 STACK: Stack ( Int S)
4 {
5 Top =   0 ;
6 If (S <   0   | S >   10000 )
7 Throw Badsize ();
8
9 Max_size = S;
10 V =   New   Char [S];
11 }
12
13 STACK :: ~ Stack ( Void )
14 {
15 Delete [] V;
16 }
17
18 STACK: Push ( Char C)
19 {
20 If (Top = Max_size)
21 Throw Overflow ();
22
23 V [Top] = C;
24 Top ++ ;
25 }
26
27 STACK: Pop ()
28 {
29 If (Top =   0 )
30 Throw Underflow ();
31 Top -- ;
32 Return V [Top];
33 }


From the above, we can see how to define a class. This is just a type of stack. Different types of stacks may have different implementation methods of push and pop, in this way, we can use the object-oriented feature "inherit" to provide multiple types ". The Stack class is defined as an abstract type, and different types of methods in the abstract type are also called polymorphism types. We need to declare with the keyword Virtual. For example, we only need to change the declaration of the push and pop methods above to the following:

Virtual   Void Push ( Char C) = 0 ;
Virtual   Char Pop () = 0 ;

From the above we can see that after the Declaration, "= 0" indicates that this method must be rewritten in the inherited subclass. [If you are familiar with C #, I think this is abstract]. Of course, when we apply it to other functions, we can declare a reference variable as follows:

Void F (Stack & S );

 
In this way, the inherited subclass instance can be referenced to the F parameter to implement [C # is very similar to this, maybe C # is what is taken from here, I am. NET developers are also familiar with this.] When our subclass inherits the base class, it has several access permissions. Generally, we use public. We will not talk about this in the future. We need to inherit a base class, the header file of the base class must be included and inherited by ":" When declaring the class, such as liststack: Public stack. The sub-classes are implemented in the same way as others.

Reading a master's book is really nice. He introduced it without knowing it. I was born from a simple concept. Today I understand these things and want to complete the task. OK, turn off the computer and go to bed! Thank you again for your suggestion!

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.