Summary of C + + (1)

Source: Internet
Author: User

1. Built-in functions

A program calls a function that requires a certain amount of time and space overhead, which is typically performed as follows:

C + + provides an efficient way to embed the code of the called function directly into the main function at compile time, rather than moving the process out, which avoids the frequent transfer and transfer of function calls, thus saving the time required to "Save the scene" and "Restore the scene" in the process. The following code:

#include <iostream>using namespace Std;inline int max (int,int,int); int main () {int i=1,j=2,k=3;int m=max (i,j,k); cout<< "max=" <<m<<endl;return 0;} inline int max (int a,int b,int c) {if (b>a) a=b;if (c>a) A=c;return A;}

Built-in functions save the running time of the program, but increase the length of the table function, which is also unfavorable, it needs to be used after comprehensive analysis.

2. member functions

The following simple case

#include <iostream> #include <string>using namespace Std;class student{public:void display (); void set (); Private:int num;char sex;string name;}; void Student::d isplay () {cout<< "num:" <<num<<endl;cout<< "Sex:" <<sex<<endl; cout<< "Name:" <<NAME<<ENDL;} void Student::set () {cin>>num;cin>>sex;cin>>name;} int main () {Student stu;stu.set (); Stu.display (); return 0;}

When a member function is defined outside a class, it must precede the function name with the class name, which is qualified, "::" to denote the scope qualifier, and it declares which class the function belongs to.

Note: If the function is defined outside the class, the system does not consider it to be a built-in function with inline functions, the call of these member functions is the same as the normal function call, which facilitates the separation of the interface of the class and the implementation of the class, which facilitates the hiding of information.

3, with parameters of the construction method

In a simple case description:

#include <iostream> #include <string>using namespace std;class box{private:int height;int width;int length; Public:box (int,int,int); int volume ();}; Box::box (int h,int w,int l) {height=h;width=w;length=l;} int Box::volume () {return height*length*width;} int main () {box Box1 (12,25,10);cout<< "The volume of Box1 is" <<box1.volume () <<endl;return 0;}

You can also initialize a data member with a parameter initialization table, overwriting the constructor in the class as follows:

Class Box{private:int Height;int width;int length;public:box (int h,int w,int L): Height (h), Width (w), Length (l) {};int Volume ();};

Note If a data member in a class is an array, it should be assigned a value in the constructor body, but not in the parameter initialization table. The following cases:

Class Student{private:int No;char name[20]; public:student (int n,char nam[]): No (n) {strcpy (Name,nam);};   void display ();}; void Student::d isplay () {cout<<no<< "" <<name<<endl;} int main () {Student stu (1, "Guo Qingxing"); Stu.display (); return 0;}

Or so

Class Student{private:int no;string name; public:student (int n,string nam): No (n), name (NAM) {};   void display ();};
4. Overloading of constructors

As in the following simple code case:

#include <iostream> #include <string>using namespace std;class box{private:int height;int width;int length; Public:box (); Box (int h,int w,int L): Height (h), Width (w), Length (l) {};int volume ();}; Box::box () {height=10;width=10;length=10;} int Box::volume () {return height*length*width;} int main () {box box1;cout<< "The volume of Box1 is" <<box1.volume () <<endl; Box Box2 (12,25,10);cout<< "The volume of Box1 is" <<box2.volume () <<endl;return 0;}

Note: When building an object using the parameterless constructor function, pay attention to the writing of the statement:

Box Box1;

and cannot be written as:

  Box Box1 (); Error

The above Error statement does not define the box object, but rather defines a normal function that is returned as box.

5. Using default parameter constructors

The following cases:

#include <iostream> #include <string>using namespace std;class box{private:int height;int width;int length; Public:box (int h=10,int w=10,int len=10): Height (h), Width (w), Length (len) {};int volume (); void display ();}; int Box::volume () {return height*length*width;} int main () {box box1;cout<< "The volume of Box1 is" <<box1.volume () <<endl; Box Box2 (12,25);cout<< "The volume of Box2 is" <<box2.volume () <<endl; Box Box3 (12,25,30);cout<< "The volume of Box3 is" <<box3.volume () <<endl;return 0;}

Therefore, when writing a construction method with default parameters, there can be no two construction methods, as follows:

Box (int h=10,int w=10,int len=10);

Box ();

This way, if the "box box" is executed, the system cannot establish a constructor based on which construction method to base it.

6. destructor

A destructor is a special member function whose name is preceded by a "~" symbol in front of the class name. In C + + "~" is the negation operator. It can be predicted that it is the opposite of the constructor function. The function of a destructor is to do some cleanup before undoing the memory occupied by the object.

Destructors do not return any values, no function types, and no function arguments. Because there is no function argument, it cannot be overloaded, that is, a class can have only one destructor.

The following cases:

#include <iostream> #include <string>using namespace std;class student{private:int no;string name;char sex; public:student (int n,string nam,char s) {no=n;name=nam;sex=s;cout<< "Constructor" <<no<< "called!" <<endl; } ~student () {cout<< "destructor" <<no<< "called!" <<endl; } void Display () {cout<< "No:" <<no<<endl; cout<< "Name:" <<name<<endl; cout<< "Sex:" <<sex<<endl;}; int main () {Student stu1 (1, "Guo Qingxing", ' m '); Stu1.display (); Student STU2 (2, "Test", ' f '); Stu2.display (); return 0;}

Results:

It is known that for the constructor, the program executes the STU1 constructor first, then executes the Stu2 constructor, and for the destructor, the program executes the STU2 destructor first, then executes the STU1 destructor, which is very similar to the characteristics of the stack. For the constructor of "same level", the destructor is this, then the different "levels", then not the case, where the local variables and global variables to give an example. The following code:

int main () {Student stu1 (1, "Guo Qingxing", ' m '); Stu1.display (); static Student stu2 (2, "Test", ' f '); Stu2.display (); return 0;}

Add a static to the Stu2 object, at which point the STU2 is a static variable that will not be released until the program has finished running. Results such as:

7. Array of objects

Arrays can contain not only simple variables, but also objects made up of classes. The following cases:

#include <iostream> #include <string>using namespace std;class box{private:int height;int width;int length; Public:box (int h=10,int w=10,int len=10): Height (h), Width (w), Length (len) {};int volume (); void display ();}; int Box::volume () {return height*length*width;} int main () {box A[4]={box (), Box (10,30), Box (10,10,40)};cout<< "The volume of Box1 is" <<a[0].volume ( <<endl;cout<< "The volume of Box2 is" <<a[1].volume () <<endl;cout<< "The volume of Box3 are" <<a[2].volume () <<endl;cout<< "The volume of Box4 is" <<a[3].volume () <<endl;return 0;}

Results:

Summary of C + + (1)

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.