Chapter 6: Classes and Objects

A class is a C++ tool for converting abstractions into user-defined types, combining data representation and methods for manipulating data into a compact package

C++ is an abstract and unified description of a group of objects with the same properties and behavior. is a user-defined data type

Format: ";" must be added at the end of the class

   Implementation of each member function;

class class name

{

   public:

           public data members and member functions;

   protected:

           Protect data members and member functions; class members can be data types or functions;

   private:

           private data members and member functions;

};

Since hiding data is one of the main goals of opp, data items are usually placed in the private part, and the member functions that make up the class interface are placed in the public part;

It is not necessary to use the keyword privat in the class declaration, as this is the default access control for class objects;

The only difference between public and private is that the default access type for structs is public, while classes are prite;

Implement class member functions:

Return value type class name::member function name (parameter list)
{
          function body
}

access class members

Access to object members includes:
       ●Dot access form: object name. public member
       ●Pointer access form

Object pointer variable name -> public member

Function overloading: a set of functions with the same function name but different parameters (different types or different numbers).

Same number of parameters, different parameter types

When defining a member function, use the scope resolution operator (::) to indicate the class to which the function belongs;

Class methods can access the private components of the class;

例如:void stock::update(double price)

The full name of a class method includes the class name.

Functions whose definitions are located in the class declaration will automatically become inline functions;

class constructor and destructor

What the constructor does is:

 Allocate space for objects; assign initial values ​​to data members; request other resources

C++ provides a special member function - class constructor, which is specially used to construct new objects and assign values ​​to their data members

The constructor has no return value, in fact the constructor does not declare a type;

The parameter of the constructor identifies not the class member, but the value assigned to the class member, so the parameter name cannot be the same as the class member;

A destructor is a member function used to cancel an object

When an object's scope ends, the system automatically calls the destructor

The role of the destructor is to clean up when the object dies

When there is no user-defined destructor, the system provides the default version of the destructor
The destructor name is: ~classname
A destructor has no parameters and no return type

C++ provides two ways to initialize objects using constructors:

The first way: call the destructor explicitly For example: stock food=stock("world cabbage", 250, 1.25);

The second way: call the destructor implicitly For example: stock garment("furry mason", 50, 2.5);

Default constructor:

A default constructor is a constructor used to create an object when no explicit initial value is provided.

Format:

Format:
funname (parameter list): initializer list
{ function body, can be empty function body }
The form of the initialization list:
member name 1 (parameter name 1), member name 2 (parameter name 2), member name n (parameter name n)

When the object lifetime ends, it needs to do cleanup work, such as: release the storage space occupied by members (pointers)

The destructor does the above work.

Destructor is automatically called (implicitly called)

A destructor has no return value, cannot have parameters, and cannot be overloaded

classname::~classname()
{
       function statement
}

The definition format is as follows (implemented outside the class): 


this pointer

In general, all class methods set the this pointer to the address of the object on which it was called;

When the parameter is the same as the member variable name, such as this->x = x, it cannot be written as x = x.

Copy constructor:

The copy constructor uses an existing object of the same kind to create a new object for data initialization

C++ provides default version of copy constructor for classes

Programmers can define user versions of copy constructors

 grammatical form

  classname::classname(const classname & referencename, …);

Shallow copy:

When an object is initialized with another object, only the data members are copied, but the resources are not copied, so that the copying method in which the two objects point to the same resource at the same time is called shallow copying.

That is: for data members of complex types, only the storage address is copied without copying the storage content

What the default copy constructor does is a simple data copy, that is, a shallow copy 

deep copy:

When an object is initialized by another object, not only data members but also resources are copied in a way called deep copying.

The copy made by the custom copy constructor is a shallow copy. 

1. The deep copy constructor must be explicitly defined
2. Features of deep copy constructor
①Definition: class name::class name([const] class name & object name);
② Handling of member variables: For member variables of complex types, use the new operator to apply for space, and then perform related copy operations
Regular members:

If the const keyword is added after the dust function of the class, the member function becomes a constant member function 

type specifier function name (parameter list) const;

Add const in front of the data member to become a constant data member

static member function

In addition to static data members, a class can also have static member functions.

Static functions can only access static members,

Either a static member function or a static data member.

Like static data members, static member functions are static members of a class, and they are not object members. Therefore, references to static members do not require object names.

Static member functions do not have this pointer and can only operate on static data

The format for defining a static member function is as follows:

    static return type static member function name (parameter list);

    Similar to static data members, the general formats for calling public static member functions are as follows:

   class name::static member function name (argument list)

   object. static member function name (argument list)

   Object pointer -> static member function name (argument list)

friend function

If a function (function B) is defined somewhere other than this class (class A)
This function can be a non-member function that does not belong to any class,
It can also be a member function of other classes,
Declare it (function B) with friend in the class body, and this function is called the friend function of this class (class A).
Friend function (function B) can access private members in this class (class A)
Inclusion of the class

Constructor (formal parameter list): object member 1 (formal parameter list), … , object member n (formal parameter list);

define class
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class student //class name
{
    string name;
    int no;
    int score[3]; //Data member
    float average;
    int order;
    public:
    student(int id,string na,int x,int y,int z):name(na),no(id)
    {
    score[0]=x;
    score[1]=y;
    score[2]=z;
    order=1,average=(score[0]+score[1]+score[2])/3;
    }
    student()
    {
        score[0]=score[1]=score[2]=0;
        order=1,average=0;
    }
    int getNo(){return no;}
    float getAverage(){return average;}
    void setAverage(int avg){average=avg;} //Member function
    void setOrder(int x){order=x;}
    int getOrder(){return order;}
    string getName(){return name;}
    void setName(string name){this->name=name;}
    void display();
}; //Must add a semicolon

summary:

The first step in specifying the design of a class is to provide a class declaration. Class declarations Liss structure declarations, which can include data members and function members. A declaration has a private part, in which the members declared can only be accessed through member functions; a declaration also has a public part, in which the members declared can be directly accessed by programs that use the class object. Typically, data routines are placed in the private section, and member functions are placed in the public section;

The content of the public part constitutes the abstract part of the design - the public interface, and encapsulating the data into the private part can protect the integrity of the data, which is called data hiding. Therefore, C++ makes it easy to implement opp features such as abstraction, data hiding, and encapsulation through classes;

C++ tries to make user-defined types as similar as possible to standard types, so objects can be declared;

If you want a member function to operate on multiple objects, you can pass additional objects to it as parameters. If a method needs to explicitly reference the object on which it is called, it can use the this pointer. Since the this pointer is set to the address of the calling object, *this is an alias for that object.






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823029&siteId=291194637