Road C ++ learning --3

First, the design of the abstract class
class class name
{
public public authority
setting member property
set member functions
}
using the class to create an object instance of the object
class name of the object name
to set the properties of an object by calling the member function
relationship of classes and objects? Is an abstract object class, object is an instance of the class.
II. Inline functions
to the compiler a suggestion, add keywords, the compiler does not necessarily follow inline processing, without keywords, perhaps the compiler will steal touch plus inline, member functions by default with the keyword
III. the default parameter function
parameters can have default values, if there is a position with the default values, then starting from this position, from left to right must have a default value, function declarations and implementations can have a default value of
four or function parameter placeholders
void func (int) must provide Invoking the parameter placeholder
placeholder parameter has a default value may be
five, function overloading
in C ++ function name can be repeated under the same scope must then, the same function name, a function of the parameters different number or different types or a different order. letter number return Times value Do not can With Make for weight Load of article Matter \ Color {red} {function returns a value as not overloaded condition}

Six, extren analysis
to solve the C ++ language code call c

#ifdef __cplusplus
extern "C"{
#endif 
.........
#ifdef __cplusplus
}
#endif 

Seven, encapsulation
package strict type conversion in c ++ detection, so that the properties and behaviors bind together

  • Properties and behavior as a whole to represent things in life
  • Control permissions public private rights protected public rights private protection rights

Eight, constructor and destructor
object initialization and cleanup, the system calls the default constructor and destructor, empty implementation

class Person
{
public: //必须写在public下
	//构造函数写法
	// 与类名相同,没有返回值,不写void ,可以发生重载(可以有参数)
	//构造函数有编译器自动调用,而不是手动,而且只会调用一次
	Person()
	{
		cout << "构造函数" << endl;
	}
	Person(int a)
	{
		cout << "构造函数" << endl;
	}
	//析构函数写法
	//与类名相同, 类名前面加一个符号 “~”,也没有返回值,不写void,不可以有参数。
	//自动调用,而且只会调用一次
	~Person()
	{
		cout << "析构函数" << endl;
	}
};

Nine, classification and call the constructor

  • According to the classification parameters
    • No argument constructor (default)
    • There constructor parameters,
  • Type classification
    • Copy constructor
    • General constructor
class Person
{
public: 
	Person()
	{
		cout << "构造函数" << endl;
	}
	Person(int a)
	{
		cout << "构造函数" << endl;
	}
	// 拷贝构造函数
	Person(const Person & p)  //不加&为值传递,开辟一个新的数据,调用拷贝构造,进入死循环。
	{
		cout << "拷贝构造函数" << endl;
	}
};
//括号法调用
Person p1(1); //有参
Person p2(p1); //拷贝
Person p3; //默认构造函数不要加(), Person p3();编译器会认为这行是函数的声明;
//显示法调用
Person p4 = Person(100);
Person p5 = Person(p4);
Person(100) //叫匿名对象,如果编译器发现对象是匿名的,那么在这行代码结束时就释放
//不能直接用拷贝构造函数,初始化匿名对象
Person(p5); //编译器会认为写成了Person p5;对象的生命
  • Implicit type conversion

  • Copy constructor call time
    1, with already created objects to initialize a new object
    2 to the value passed to the function parameters by value embodiment
    3, a local object returned by value
    4, there Optimization release

  • Constructor calls Rule
    1. default for a class provides three functions: the default constructor, copy constructor, destructor
    2, when we have a constructor parameter, the system is not in a default constructor of us, but the system will also provide a default copy constructor
    3, when we provide a copy constructor, the system will not provide other constructors

Ten, deep and shallow copy copy

  • Default copy constructor will provide a simple copy of the value
  • If the property where there is data pointing to the heap space area, so simple shallow copy of the release will result in an exception duplicate memory
  • Appeal to solve the problem, we need to provide your own copy constructor deep copies.

XI initialization list

  • After the constructor: the attribute (parameter), attributes (parameters)
class Person{
	Person() : m_a(10),m_b(10),m_c(10)
	{}
	Person(int a, int b, int c) : m_a(a),m_b(b),m_c(c)
	{}	
	int m_a;
	int m_b;
	int m_c;
}

XII class object as a member

  • First class objects eleven construction order structure, and then construct their own
  • Destructor order to their destruction, in contrast destructor class objects, and construction order

Thirteen, explicit keywords

  • Prevent the implicit type conversion Mystring str3 = 10;

Fourteen, new operator

	//Person p1; //栈区开辟
	Person* p2 = new Person; //堆区开辟 所有new出来的对象都会返回该类型的指针

	delete p2;//配合new用
  • If you use the new expression [], must be used in the corresponding delete expression [], if not applicable in the new expression [], must not use [] in the corresponding delete expression.

Guess you like

Origin blog.csdn.net/weixin_43615373/article/details/90301268