C ++ programming | fourth chapter Classes and Objects

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43145926/article/details/96716072

The basic features of object-oriented programming

  • Abstract: specific problems (objects) are summarized, the public nature of a class of objects out of the process and be described.
  • Package: abstract and behavioral data obtained (or function) combine to form an organic whole, is a function of the code data and operation data organically combined to form a "class"
  • Inheritance: allowing the characteristics of the original class, more specifically, a more detailed description.
  • Polymorphism: refers to the ability of a program capable of processing a plurality of types of objects. This polymorphism can be in C ++ polymorphism by forced, heavy polymorphic, polymorphic parameters of the type, comprising 4 kinds of polymorphic forms.
  • The scope of public, private, protected, and when the difference is not written
    Answer: The default is default when not write
    Here Insert Picture Description
  • Inline member functions
    implicitly declared:
class Clock {
public:
	void show(){xxxxxxxxx}
private:
	int hour,minute;
};

Display statement:

class Clock {
public:
	void show();
private:
	int hour,minute;
};
inline void Clock::show(){
xxxxxxxxx;
}

Constructor

  • The same function names and class names of the constructor, no return value, typically declared public

  • The constructor is automatically called when the object is created
    the copy constructor

  • Its parameter is a reference object of this class. Its role is to use an object that already exists to initialize a new object of the same kind.

class Point{
public:
	Point(int xx=0,int yy=0){//构造函数
	x=xx;
	y=yy;
}
	Point(Point &p);//复制构造函数
	int getx(){return x};
	int gety(){return y};
private:
	int x,y;
};
Point::Point(Point &p)
{
	x=p.x;
	y=p.y;
}

The constructor is called when the object is created, and the copy constructor is called in the following three cases

  • When an object with another object class to initialize the class
int main()
{
	Point a(1,2);
	Point b(a);//初始化b.c时复制构造函数被调用
	Point c=a;
}
  • If the object is a class parameter of the function, the function is called, and arguments for parameter binding.
void f(Point p)
{xxx;}
int main()
{
	Point a(1,2);
	f(a);//函数的形参为类的对象,调用函数时,复制构造函数被调用
	return 0;
}
  • If the function return value is an object class, the function returns to the caller when the execution is completed
Point g()
{
	Point a(1,2);
	return a; //返回值是对象,调用复制构造函数
}

Destructor

  • To do some clean-up work before the object is deleted
  • Destructor is called automatically at the time of the lifetime of the object of coming to an end.
  • Destructor no arguments

Embedded object constructor call, call order they appear in the order defined according to a combination of the embedded object class. Note that the order has nothing to do call the embedded object appears in the constructor initializer list in the embedded object constructor

Forward Reference Statement

class Fred;//前向引用声明
class Bar
{
	Fred x;//错误,类Fred的定义尚不完整
};
class Fred
{
	Bar y;
};

UML graphic logo Unified Modeling Language
Reference This Blog: https://blog.csdn.net/qq_27175513/article/details/79744694

Structure

  • Structure and only difference is that class, and class structures have different default access control attributes: in the class, is not specified for the access attribute control member, which is private property, and the structure for the public.
  • If the structure of all members of the public, there is no user-defined constructors, useless base class virtual function and, in this way it can be assigned:
    struct Student {}; int main() { Student stu={97001,"lll",'F',19}; }

Commonwealth

  • The default access control properties for the public
  • All data consortium members share the same set of memory cells
  • While members of the Commonwealth of variables is at most only a meaningful
  • Members of the object can not have a custom consortium constructors, destructors and copy assignment operator overloading
  • Commonwealth can not be inherited, and thus does not include support polymorphism.
union Mark
{
	char grade;//等级制的成绩
	bool pass; //只记是否通过的成绩
	int percent;// 百分制成绩
};

Guess you like

Origin blog.csdn.net/qq_43145926/article/details/96716072