C ++ object-oriented
This article is only used as a review of the C ++ postgraduate course.
Object-oriented Constructor
It is automatically called when an object is created.
Copy constructor
A parameter is a reference to a class object. It uses an existing object to initialize a new object of the same type.
The replication constructor is called in the following three cases.
When one object of the class is used to initialize another object of the class.
Point a (1, 2); // both methods call the copy constructor, but they are written differently. Point B (a); // use object a to initialize bPoint c = a; // use object a to initialize B
If the parameter of a function is a class object, when the function is called, the parameters and real parameters are combined.
void foo(Point p){ cout<<p.getX()<<endl;}int main(){ Point a(1,2); foo(a); return 0;}
Note: The copy constructor is called when the value is passed, but not when the reference is passed. Therefore, when a large object is passed, the transfer of reference is more efficient.
If the return value of a function is a class object, when the function is executed, it returns the caller.
Point foo(){ Point a(1,2); return a ;}int main(){ Point b; b= foo(); return 0;}
Note: When a leaves function foo (), object a will die. At this time, it will call the copy constructor to create an unknown temporary object that exists in expression B = foo.
Destructor
Destructor are used to clear objects before they are deleted. They are automatically called when the object's lifetime ends.
The invocation sequence of the Destructor is the opposite to that of the constructor.
Class combination
- When you create a class object, other objects in the class and member variables of the basic data type are initialized first. Constructors are called in the following order:
- Call the constructor of the embedded object. The sequence is defined by embedded objects in the composite class. Note that the sequence of embedded objects in the constructor initialization list is irrelevant to the calling sequence of embedded object constructor.
- The function body that executes the constructor of this class.
- The invocation sequence of the Destructor is the opposite to that of the constructor. After the Destructor is executed, the built-in object destructor are executed one by one. The Calling sequence of the built-in object's destructor is the opposite of the order in the class definitions in the combination class.
Write the copy constructor of the class. Parameters must be passed for the copy constructor of the embedded member object.
class Point{public:Point(Point& p);};class Line{private:Point p1,p2;public:Line(Point& p1,Point& p2);};Line::Line(Point& xp1,Point& xp2):p1(xp1),p2(xp2){//to-do something}
Forward DeclarationClass B; // declare class A {public: void foo (B B) ;}; class B {public: void bar (A );};
When a class has a circular dependency, you must use the Forward Declaration. However, even if a Forward Declaration exists, the class objects cannot be directly defined.
Static data member of a class
Static modification. A class attribute is a data item that describes the common features of all objects of a class. Its Attribute values are the same for any instance.
class Point{public: Point(){ count++; } void show_count(){ cout<<count<<endl; }private: static int count;};int Point::count = 0;
Static data members are initialized independently outside the class and need to allocate space for them.
Static function Member
Static member functions can directly access the static data and function members of the class. The access to non-static members must pass through the object name.
Class A {public: staticvoid foo (A a); private: int x ;}; void A: foo (A) {cout <x; // an error occurred when referencing x. Cout <a. x; // correct .}
Youyuan
The membership relationship provides a mechanism for data sharing between member functions of different classes or objects and between member functions of classes and general functions.
Features of youyuan
- The relationship between friends and friends cannot be passed.
- The relationship between friends and friends is one-way.
- A friend relationship cannot be inherited.
Youyuan Function
Friend functions are non-member functions modified with the keyword "friend" in the class. It can be a common function or a member function of other classes. You can use the object name to protect the private and member of the member class.
class Point{ Point(int x,int y); int getX(); int getY(); friendfloat dist (Point &p1,Point &p2);private: int x,y;};float dist(Point &p1,Point &p2){ double x = p1.x - p2.x; double y = p1.y - p2.y; return static_cast<float>(sqrt(x * x + y * y));}int main(){ Point p1(1,1),p2(4,5); cout<<dist(p1,p2); return 0;}
Youyuan class
Like a friend meta function, one class can declare another class as a friend Meta class. If Class A is A Class B, all the members of Class A are membership functions of class B and can access the private and protected members of Class B.
Class A {public: int getX (); friend class B; // B is A's friend class private: int x ;}; class B {public: void set (int I); private: A a;}; void B: set (int I) {. x = I; // because B is A member of A, you can access the private member of A class object in B's member functions .};
Shared data protection common objects
The data member value does not change during the entire lifecycle of the object. A common object must be initialized and cannot be updated.
const A a(3,4);
Class member modified with const
- Common member functions
Type specifier function name (parameter list) const
Eg:void foo() const;
- The const keyword must be included in the definition and declaration.
- A common object can only call a common member function.
Whether a function is called by a common object or not, the target object is regarded as a common object during the function call. Therefore, a common member function cannot update the data member of the target object, you cannot call a non-const-modified function for the target object.
Class A {public: void foo () const {cout <"foo const" <endl; this-> bar (); // error C2662: ":: bar: the "this" pointer cannot be converted from "const A" to "A & this-> x = 1; // error C3490: because the constant object is accessing "x", it cannot be modified} void bar () {cout <"bar" <endl;} private: int x ;};
Const can distinguish between function overloading. For example, declare the following functions.
void print();
void print() const;
In this case, the second object is often called, and the first object is called nearby.
Regular data member
A regular data member can only specify a specific value through the initialization list of the constructor.
Class A {public: A (int I): a (I) {// a = I; // error} private: const int a; static const int B; // static const int B = 10; // The static constant of the correct int or enum can be directly specified in the class .}; Const int A: B = 10;
- Regular reference
The referenced object cannot be updated.
Note that:
- Non-const references can only be bound to common objects, but common references can be bound to common objects.
- An object that is often referenced. When accessed through this reference, the object can only be treated as a constant object. (You cannot modify data members or call non-const member functions)
This article permanently updates the link address: