Before programming, it is necessary to review some basic concepts of object-oriented in C ++. C ++ has many advantages over C, mainly reflected in encapsulation, continuation and polymorphism. Encapsulation not only makes the program structure more compact, but also organizes functions of data and operation data.
In addition, the security of internal data is improved. The extensibility and code reusability of software are increased. polymorphism allows designers to better abstract problems during program design, it facilitates code maintenance and reuse. Visual C ++ is not only a compiler, but also a comprehensive application development environment. You can use the object-oriented C ++ language to develop professional Windows applications. Mastering the content of this chapter will lay a good foundation for the study of subsequent chapters.
- In-depth analysis of C ++ code compilation and statement Sequence
- Programming in the Visual C ++ Environment
- Senior Scholars Talk About C ++ programming skills
- Exploring flexible C/C ++ language commands
- Detailed description of the Test C ++ Test Tool
In the C ++ language, we can define the struct type and encapsulate multiple related variables for a whole usage. The variables in the struct can be identical, partially identical, or completely different data types. In C, struct cannot contain functions. In object-oriented programming, objects have statuses and behaviors.
The status is stored in the member variable, and the behavior is implemented through the member method. The struct in C ++ can only describe the status of an object, but cannot describe the behavior of an object. In C ++, the struct is extended. The struct of C ++ can contain functions.
Let's take a look at the program shown in Example 1:
- # Include
- Struct point
- ;
- Void main
In this program, we define a struct point, in which two integer variables are defined as the X and Y coordinates of a vertex. The main function defines the pt variable of a struct and assigns values to the two member variables of pt, then, the cout object of the output stream class of C ++ is called to output the coordinates of the point.
In C ++, three standard input and output stream objects are pre-defined: cin, cout, and cerr. Cin and the input operator are used together to read data from the standard input. cout and the output operator are used together to output data to the standard output. cerr and the output operator are used together to output errors to standard errors. The default standard input is usually the keyboard.
The default standard output and standard error output are usually displays. Using cin and cout is much simpler than using scanf and printf in C language. When using cin and cout, you do not need to consider the types of input and output data. cin and cout can automatically adjust the input and output formats based on the data types.