A classmate of the C + + story (presumably the origin of C + +) (i)

Source: Internet
Author: User


1: Definition of the understood class

Process-oriented programming weaves the world into a web, with its nodes interlocking

Object-oriented programming divides the world into chunks, blocks and blocks that are interconnected and independent

Before C + + did not appear, the father of C + + may have found a lot of C language drawbacks, so introduced the class, but why to introduce classes, here, I stood at the time of a position, for example, I was in the late 70, at that time only C language and other process-oriented language, along this train of thought, Start to make up for the lack of C language, and then how to introduce the class, there may be many problems, but also to help me from another angle to understand C + +

2: Time pushed to 1979

This time the father of C language, he has completed the C language development of Linux all work, a student at that time their computer course programming class may have C language, have B language

Every time he wrote some relevant data, he had to write it again and again in C, in particular, and, when he re-used the code, he was going to rewrite the old code again, and we could think, in the early 80, when the computer was mostly IBM's big computer, Microsoft has not been established, the DOS system has not come out, at that time only memory only 16k of space, that time, the classmate, his many pieces of code may be handwritten in this, some of the most commonly used code, he may exist on the memory, after all, the memory is particularly small and particularly expensive, In this environment, the classmate on a few years of college, he has been very dissatisfied with the C language of the cumbersome, but after all, for the time of the compilation, C still has a great improvement, the classmate in school may have written a few years of code, he is particularly good at some useful code or some similar things to classify, This may help him to solve the problem of searching for code, do not underestimate the method of code classification, it may be a lot of programmers often headaches

He was in life, he would put some hardware memory together, some hardware put together, some of the electronics put together, and all the numbers, when he used the processor, he will be in the processor that box to look for storage, know the number of storage, he will be in smaller boxes to find, this method, Industrial 5s management may be the embodiment of this method, anyway, this method helps the classmate save a lot of time, daily life, he will also book, clothing, tableware and other categories, this can greatly reduce his daily chores time, let him spend more time to deal with the computer code

Although he categorized the code, but still did not meet his needs, and the repeated code may allow him to buy a lot of memory to store code snippets, computer code is not the daily life of things so simple, the code of more and more categories, he found that many times he could not find his code.

He is a thoughtful person, not just to improve the code snippet, the C language to improve the data structure, to simplify his workload, so he took the first step, the data began to classify, the method began to classify

Perhaps, he was just beginning to think of the structure, at the beginning may not think of the concept of class, just start and no access to the idea, just try to follow the way of living habits, some process of collective collation, for example, daily my bed has sheets, pillows, mat, and so on, but these are bedding, Every time I go to the supermarket, if I need quilt, definitely to the bedding area to find, and then the Futon area to find, finally find the quilt, the data can be like this

For example, I see a person, this person's characteristics are too many, there are many characteristics I can not think of, but I know his name is Jim, his height 185, weight 80 pounds, male, after the characteristics of my in addition, I just need to remember this person on the line, so this classmate wrote his concept, He uses the keyword of the struct to indicate that this is some of this Jim's data, this data is probably the structure

But he also knew Sam, still have to re-define all these, special trouble, the back will know a lot of people, people can not be transparent, this is not in line with the daily norms around, so he also defined the class, using class keyword to indicate that class default is private, in order to avoid the appearance of Jim , Sam, and so on repeatedly rewrite the malpractice, the classmate he simply use some more abstract things, he may after a few months of research, may have developed his first object of the code, the code functions as follows:

#include <iostream>using namespace Std;class student{private:char *name;int age;int sex;public:void setstuname ( Char *sname), void setstuage (int sage), void setstusex (int ssex);p ublic:void getstuname (), void Getstuage (), void Getstusex ();}; void student::setstuage (int ssage) {age=ssage;} void Student::setstuname (char *sname) {name=sname;} void student::setstusex (int ssex) {sex=ssex;} void Student::getstuage () {if (age<0) {cout<< "Age not Set" <<ENDL;} else{cout<< "Age:" <<age<<endl;}} void Student::getstusex () {switch (sex) {Case 1:cout<< "Gender: Male" <<endl;break;case 2:cout<< "Gender: Female" < <endl;break;default:break;}} void Student::getstuname () {cout<< "name:" <<NAME<<ENDL;} int main () {student stu;stu.setstuage () stu.setstusex (1); Stu.setstuname ("Lucy"); Stu.getstuname (); Stu.getstuage ( ); Stu.getstusex (); return 0;}
Of course, there's no namespace at that time, and iostream hasn't been standardized yet.

His code, successfully summed up some data and methods in the Class keyword definition of the student classes, and the name, age, gender, the three attributes of the class as a private object, this class by instantiating a member, solve the problem of Jim and Sam, greatly facilitates the efficiency of programming

With the first piece of code, it is found that writing this code he is too bloated, and particularly complex, the implementation of the function is particularly simple, can not set a special function, the class inside the instantiation of some of the initialization of the full inclusion of it, this may be the predecessor of the constructor

After months of change, he changed the code to the following code

#include <iostream>using namespace Std;class student{private:char *name;int age;int sex;public:student (char * Sname,int sage,int ssex); ~student ();p ublic:void getstuname (); void Getstuage (); void Getstusex ();}; Student::student (char *sname,int sage,int ssex) {age=sage;name=sname;sex=ssex;} Student::~student () {}void student::getstuage () {if (age<0) {cout<< "Age not Set" <<ENDL;} else{cout<< "Age:" <<age<<endl;}} void Student::getstusex () {switch (sex) {Case 1:cout<< "Gender: Male" <<endl;break;case 2:cout<< "Gender: Female" < <endl;break;default:break;}} void Student::getstuname () {cout<< "name:" <<NAME<<ENDL;} int main () {student Stu ("Lucy", 15,1); Stu.getstuname (); Stu.getstuage (); Stu.getstusex (); return 0;}

After this revision, the main addition of constructors and destructors, the constructor to complete the instantiation of some classes of the initialization of the problem, the destructor to achieve a number of release resources to close the work, this improvement, the classmate he has an object-oriented prototype

He transformed the world of programming into an object-oriented process, allowing programming to be closer to the environment.

For constructors, to make up for some classes that do not need to be initialized, the classmate also provides a default constructor and default destructor for each class, making this feature even more extensive.

Then, with this basic object-oriented concept, the classmate began his next work, he will now he wrote all the code, to see whether the object-oriented thinking can be written, so in class and struct two keywords, with "." Symbol, he began to instantiate his previous code, as well as some of the computer resources of the instantiation, here may have some of the STL's predecessor, of course, these are not perfect

In the crazy writing behind, the classmate gradually some unbearable, he suddenly found that he was trapped in the C language just the beginning of the problem, before the programming object, he managed a lot of code snippets, and now, he wants to manage a lot of classes, classes, he defined the cat class, defined the dog class, Define the Tiger class, found that more and more, simply delete all, first define a mammal, suddenly, but the cat, Tiger each has its own characteristics, there are similarities, there are different points, simple through public and protect and private few can not completely distinguish the many classes, How to solve the problem.

He looked at his own class, although some of the classes are structured together, but there are a lot of repetition of these classes, the instance of the tiger between the save a lot of code, tigers and Tigers have been instantiated by the name can be completely separated, but tigers and cats such as they will run, this run is also a dog, Is it possible to create a tiger like Tiger and Tiger in between classes?

This asks the classmate to build his base class according to his idea of constructing class, mammal class and even the upper level animal class

C language Then there are inheritance and derivation, the next section to continue to analyze it




A classmate of the C + + story (presumably the origin of C + +) (i)

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.