This article introduces the basic use of C + +, with a program description of the basic application, of course, C + + pointers and operators of the overloaded these features are not involved, just the most basic programming things to tell once,
With the "Python" version of the Python development environment configuration, Helloworld,python Chinese issues, input and output, conditions, loops, arrays, classes (click to open the link) is a sister article, it is said that the pile of things out of the book can write a lot of pages, It's really drunk to be in class for most of the semester.
The following program code:
#include <iostream> #include <string>//to use C + + strings, you must introduce stringusing namespace Std;class util_impl{// The abstract class of util is a class declared only by methods Protected:void SayHello (); int Max ();};/ /Different from Java, declare a class need to use a semicolon class util:public util_impl{//new definition Util classes, inherit Util_impl this class private:string hello;public:void SayHello (string name) {//Output method This->sethello ("Hello,"); Cout<<this->gethello () <<name<<endl;} int max (int n, int *arr) {//Max method, explains how to pass an array int max=arr[0];for (int i=0;i<n;i++) {if (Max<arr[i]) {max=arr[i in a function/method ];}} return Max;} The getter of Hello with Setterstring Gethello () {return this->hello;} void Sethello (string Hello) {this->hello=hello;}}; int main () {Util util;//declares a class, not the same as Java, followed by the new Util () string name;cout<< "May I ask your name?" <<endl;cin>>name;util.sayhello (name);//Call the SayHello method in util directly to int arr[]={1,3,40,22,20,10};// Defines an array of int arr_length=sizeof (arr)/sizeof (arr[0]);//Find out the length of this array, there is no arr.length such usage in C + + int Arr_max=util.max (arr_length , arr);cout<< "Arr maximum value is:" <<arr_max<<endl;return0;}
The results of the operation are as follows:
The above-mentioned procedure first defines a Util class, which inherits from a Util_impl called "abstract class", which is filled with the method declaration, and the method declaration in Util_impl is defined as protected, which is used only by its descendants and cannot be called directly.
The Util class then implements these method declarations, an output, and a maximum value. The Util class intentionally does a static member string Hello like Java, and implements its getter and setter, stating the use of the This keyword.
Second, defining an array arr[] requires itself to find its array length by means of an int arr_length=sizeof (arr)/sizeof (arr[0]), which is not the same as other languages. Length is so cool, after all, compared to the bottom and the old.
Then, if the function/method in C + + takes an array as an argument, it is necessary to pass the array length together with the array name, which is essentially the pointer to the array, or the sentence, after all, C + + bottom, ancient. This way, the function/method can finally manipulate the array.
Finally return the output of the return value of arr, and don't think of C + + as Java, you can use the + connection string and shaping, and then automatically trigger the shaping of the ToString () method, or honestly using << to connect strings and shaping it!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Input and output, loops, conditions, strings, arrays, classes, inherited usages of "C + +" C + +