Input, output and simple classes of instantiated C + + objects:
Idle to boring, learn C + +. (the foundation of C language is pretty good. I hope it will be easier to learn C + +)
1, A simple framework for C + +:
#include<iostream> #include<cstdio> using namespace std; int main() { cout<<"Hello everyone!"<<endl; return 0; }
The library introduced in C + + is different from C, so there is no need to add h. And generally add a C before the library name of C, such as stdio in C h. In C + +, it is cstdio.
Note:
using namespace std;
This sentence is the visible range of the identifier. The objects provided by C + + libraries are stored in std, a standard namespace. If you want to use cin, cout, endl, etc., you must add this sentence.
2, Hello world?:
When you first learn a language, the simplest example is to output hello world. Hahaha, so here you can also write a simple example with the input and output stream of C + +:
#include<iostream> #include<cstdio> using namespace std; int main() { int a,b; cout<<"Hello everyone!"<<endl;//endl stands for line feed cin>>a>>b; cout<<"a+b The sum of is equal to:"<<a+b<<endl; return 0; }
cout is used for output. After < < is the output content. You can put a variable and output the value of the variable; You can also put a string directly. If you want multiple outputs, you can use multiple < <. endl stands for line feed.
cin is used to obtain keyboard input. In the above code, the obtained number is stored in a and b, provided that a and b have been defined before. Enter two numbers on the keyboard, separated by "space" or "enter". When two more numbers are output, the program only takes the first two numbers.
Here, suppose I enter three numbers:
It can be seen that the program only takes the first two numbers.
But! I suddenly wonder if the number 8 that you input will still exist and be automatically read in the next time you use cin? Or after the execution of this sentence cin, the numbers not obtained later will be automatically cleared? So I tried to use cin again, so the program became:
#include<iostream> #include<cstdio> using namespace std; int main() { int a,b,c; cout<<"Hello everyone!"<<endl;//endl stands for line feed cin>>a>>b; cout<<"a+b The sum of is equal to:"<<a+b<<endl; cin>>c; cout<<c; return 0; }
This time I enter three numbers: 6, 9 and 8:
Good guy, it's all executed directly and didn't give me a chance to enter the c value. It seems that the first two of the three numbers entered for the first time are given to a and b, and the last one is saved in the buffer of cin. When using cin next time, the data in the buffer will be automatically read in.
If you want to re-enter, do not enter the left value before. You need to empty the buffer first by adding two sentences before the input:
cin.clear(); cin.sync();
So you can enter the value of c:
More in-depth study of input and output flow, learn it later~~
3, Object oriented – class, object:
The most important thing in object-oriented programming is to understand "class" and "object".
What is a class? A class is a class. Category, classification and similarity... All similar and similar with the same characteristics can be classified into one category, and they can be abstracted into a class - class.
For example, all kinds of dogs can be abstracted into a class, and all kinds of cats can also be abstracted into a class. For the dog, in addition to its age, coat color and other attributes, it also has various actions, such as biting, foraging and so on. These are the properties and methods of the class.
This is true for any kind of things. Attributes represent the current state of the thing, and methods represent the movement and change of the thing.
The structure in C language has the rudiment of object-oriented. Structures can encapsulate various types of data, such as:
struct Student{ char name[10]; int age; float score; struct Student * pStu; };
Note: the definition of "int" and "char" is just a data type, not a data type.
The reason why it is a prototype is that a structure can only "encapsulate" attributes, not "encapsulate" methods (that is, functions). Class can. See the following example:
#include <cstdio> #include <iostream> #include <cstring> using namespace std; //Define a class through the class keyword class Dog{ public: //Properties: char *color; int age; //method: void Yao_Ren(){ printf("I can bite!"); } void eat(){ printf("I'm looking for food!"); } }; int main(){ //create object class Dog dog1; //You can also omit the keyword class //Assign a value to the property of dog1 strcpy(dog1.color,"yellow"); dog1.age = 3; cout<<dog1.age<<endl; cout<<dog1.color<<endl; //Call the method of dog1 dog1.Yao_Ren(); dog1.eat(); return 0; }
Dog class has two attributes: color, age, and two methods: biting and foraging.
Class creates a class Dog, which does not occupy storage space;
In the main function, you need to create an object with a class (similar to creating a variable with a structure), namely dog1. This step is also called instantiation. It is said that the object is an instance of the class, which occupies memory space, and the object has all the properties and methods of the Dog class. Add "." through the object You can access the properties and methods of the object.
Operation results: