C ++ acquaintance

Know C ++

Compile and run the step
editor (.cpp .h) compiler (form .obj) link (.exe) execution (loader into the memory) Debugging

Function prototype int area(int,int); 等价于 int area(int a,intb); similar java interface
namespace using namespace std;
preprocessor 符号 "#"开头

References
keywords & as Type &a=object;a practical point to objects

  1. When you create must be initialized
  2. Object references can not be changed after initialization
  3. It can not be null

const qualifier
int * const p=&a; pointer to the memory address can not be changed, the address value can change
const int * const p=&a;the address values can not be changed, and

Dynamic memory allocation
new keyword as Type *p=new int// no initial value Type *p=new int(10);// initial value int *p1=new int[10];// initialize the array to the left pointer equation

Release memory
delete p; // array with releasedelete[] p

* P void ; // pointer P represents the type of uncertainty

String object

string str1="i am lili!";           //类与java不一样 , 类不需要大写
string str2("who are you!);
string str3=str1.substr(2,2);    //截取第2个字符
int i=str1.find("am",0)  //第0个开始查找 字符串在主串的位置

Return value
int & index(int i); // function returns a reference
类型标识符 * 函数名(参数列表);// return a pointer
string input(const int m)// return object

inline keyword
does not have a real function call when the C compiler to execute the function key modified, but instead call expression with the body of the function.

The default function of the parameters
in the prototype as defined void f(int a,int b=1,int c=2 )// default parameters written on the right (not left to write, because the match is from left to right)
If the call is given arguments, with the argument, if not given, then use pre-declared The default parameter values.

int add(int x=5,int y=6){}

Create a new life cycle of a controlled object Point * pt1 = new Point;

Copy constructor
class name :: class name (const reference object class name & name)

  1. It is also a constructor function return types can not be specified.
  2. Only one parameter is a reference to an object of the same kind.
  3. No class has a copy constructor
Destructor
Point();
  1. A class has only one, there is no argument, can not be overloaded
  2. Revoking the object, call the destructor.
  3. Do not specify a return type

Function template template <class T> 或 template <typename T>

template <class T> T max(T m1,T m2){ 
    return (m1>m2)?m1:m2;
}

By value (Copies)

void sendValue(char ch){
      cout << ch << endl;
      cout << (ch=='a') << endl;
}

Biography address (modify the original address)

void sendAddress(int * px,int * py){
        cout << px<< " " << py <<endl;
}

Pass by reference

void sendAddress(int &a,int &b){
        cout << a<<" "<< b<<endl;
        a=100;
}

Classes and Objects

Class declaration

class 类名{
          函数和数据    // 不说明访问权限,默认是私有的 
    private:
         私有数据和函数    //只可在本类中访问
         int a,b;              //类体中不允许对数据成员初始化  如 int a(2),b(3); 是错误的  产生具体对象时才有意义
    public:
         公有函数和数据      //通过该访问权限修饰的函数来访问私有
         void Display();       //函数原型(声明函数)
         void Setab(int m,int n){           //类体内定义  默认为inline 
              
         }
    protected:
         保护数据和函数       
}

返回类型 类名::成员函数名(参数列表){    //在类体外定义成员函数 需要类名和作用域运算符  ::
      函数体  //内部实现
}

inline 返回类型 类名::成员函数名(参数列表){    //在类体外定义成员函数(内联)
      函数体  //内部实现
}

Objects using classes

Point p1,p2;
Point *p =&p1;
Point &p3=p2;
p1.Setab(5,10);  //对象和引用   调用函数
p->Display();  //指针访问
p3.Setab(1,2);   //引用   调用函数

Static member
Static members must be initialized
data type class name :: static data member name = initial value;

Class vivo Initialization:
static int S;
class initialization vitro:
int Point :: COUNT = 0;

Friend function
friend function is independent and external functions of the current class,
all members of the class can access it

Friend can be a class or function

class Point{
private:

public:
Friend Double Distances (& Point, & Point); // friend function declarations
}

double distances (xxxx) {} // define friend function

const object
constant member (normal data members, often quoted (constructor initializes only), a static data member often ((in vitro class initialization))
const
member functions -> member functions normally
objects -> // objects can often call he's often a member function, you can not call other member functions

Inheritance and derived

Single inheritance
class derived class Name: Access Control base class {
P
P
P
}

Constructor execution order:
. Constructor initializes the base class member 1. Call base class (base class from left to right)
2. Add members initialized to the lower
body of the constructor of the derived class 3. Run

Class protected members of
protected
derived class to access protected members of the base class with public access to the same members of the
same derived class object to access protected members of the base class and access private members

Base class member function of the derived class to access the base class object base class (derived class objects) external function
private inaccessible inaccessible inaccessible
protected protected inaccessible inaccessible
public public accessible accessible

Private derived: sub-class private class derived grandfather, grandfather class method is not called directly grandson class

Derived protection: child protection class derived class grandfather, the grandfather is a protected class member properties in the subclass or protection. (Private non-inherited)

Ambiguity and the rules governing

Application of scopes defined operators and member name, derived base class of the same name of the function dominator
obj.Base1 :: fun () obj.Base2 :: fun ()

virtual function

Released seven original articles · won praise 0 · Views 111

Guess you like

Origin blog.csdn.net/weixin_37979099/article/details/103882647