Learn C++ classes and objects from scratch

C++ is an object-oriented programming language, which provides a powerful class mechanism to allow programmers to combine data and operations, and to manage and operate data conveniently. In C++, we can use classes to define our own data types, which can encapsulate data and operations on data, and can be used multiple times in a program.

1. What are classes and objects

In C++, a class is a user-defined data type that can contain data members and member functions. Among them, the data member represents the data attribute in the class, and the member function represents the operation method in the class. An object is an instance of a class, which usually contains all the data members defined in the class, and can call the member functions defined in the class.

The relationship between classes and objects is like the relationship between blueprints and houses. Blueprints are used to describe the construction specifications of houses, and houses are concrete instances created by blueprints. Similarly, a class is used to describe the properties and methods of an object, and an object is a concrete instance created according to the description of the class.

2. Class declaration and definition

In C++, we can declare a new class through the class keyword. A class declaration usually includes the class name, the data members in the class, and the definition of member functions. For example, the following is a simple class declaration:

class Rectangle {
public:
    int width;
    int height;
    int area();
};

In this class declaration, we define a class Rectanglecalled that has three data members, including width, heightand area. Among them, widthand represent the width and height of the rectangle heightrespectively , and arearepresent the membership function to calculate the area of ​​the rectangle.

It should be noted that the member functions in the class can be accessed through .the operator , for example, for the instance object of the above class rect, we can use rect.area()to call areathe member function to calculate the area of ​​the rectangle.

Of course, class definitions usually need to be placed in header files to be shared among multiple source files. For example, the following is an example of a simple class definition:

// Rectangle.h 头文件
#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {
public:
    int width;
    int height;
    int area();
};

#endif  // RECTANGLE_H
// Rectangle.cpp 源文件
#include "Rectangle.h"

int Rectangle::area() {
    return width * height;
}

In this example, we first define the class Rectangle.hin Rectangle, and define the implementation of its member functions Rectangle.cppin area(). In this way, when we need to use Rectanglethe class , we only need to include the header file Rectangle.h.

3. Object creation and use

In C++, we usually use the new keyword to create objects of classes. For example, here's a simple example of creating Rectanglea object :

Rectangle* rect = new Rectangle;
rect->width = 2;
rect->height = 3;
int a = rect->area();

In this example, we first created rectan Rectangleobject named with the new keyword, and set the widthand heightproperties of the object. Then, we calculate the area of ​​the rectangle by calling rect->area()the method and assign the result to an integer variable a.

Note that after use, we need to explicitly release the memory space through the delete keyword to avoid memory leaks. For example:

delete rect;

Four. Summary

In C++, classes and objects are the foundation of object-oriented programming. By using classes and objects, programmers can easily encapsulate data and behavior, and realize code reuse and modularization. When defining a class, we need to consider the data members and member functions in the class, and in the actual use process, we need to pay attention to the reasonable allocation and release of memory space.

5. Give an example

We assume that we want to create a book class, which needs to contain data members such as title, author, and publication date, and needs to provide some operation methods, such as getting the title of the book, getting the name of the author, getting the date of publication, etc. In C++, we can define this book class with the following code:

class Book {
private:
    string title;
    string author;
    string date;

public:
    void setTitle(string t) {
        title = t;
    }

    string getTitle() {
        return title;
    }

    void setAuthor(string a) {
        author = a;
    }

    string getAuthor() {
        return author;
    }

    void setDate(string d) {
        date = d;
    }

    string getDate() {
        return date;
    }
};

In this example, we use the class keyword to define a class Booknamed , which contains private data members title, authorand date, and public member functions setTitle, getTitle, setAuthor, getAuthor, setDateand getDate.

Among them, setTitlethe , setAuthorand setDatemember functions are used to set the title, author and publication date information of the book, while getTitlethe , getAuthorand getDatemember functions are used to obtain the title, author and publication date information of the book.

Next, we can create an Bookobject :

Book book;
book.setTitle("C++ Primer Plus");
book.setAuthor("Stephen Prata");
book.setDate("2011-08-25");
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Date: " << book.getDate() << endl;

In this example, we first create bookan Bookobject called and use the setTitle, setAuthorand setDatemember functions to set the book's title, author, and publication date information.

Then, we use the getTitle, getAuthorand getDatemember functions to obtain the three information of the book, and coutprint them to the screen through the statement. In this way, we have successfully created a Bookobject , and manipulated and viewed it.

Guess you like

Origin blog.csdn.net/m0_62338174/article/details/130300827