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 Rectangle
called that has three data members, including width
, height
and area
. Among them, width
and represent the width and height of the rectangle height
respectively , and area
represent 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 area
the 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.h
in Rectangle
, and define the implementation of its member functions Rectangle.cpp
in area()
. In this way, when we need to use Rectangle
the 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 Rectangle
a object :
Rectangle* rect = new Rectangle;
rect->width = 2;
rect->height = 3;
int a = rect->area();
In this example, we first created rect
an Rectangle
object named with the new keyword, and set the width
and height
properties 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 Book
named , which contains private data members title
, author
and date
, and public member functions setTitle
, getTitle
, setAuthor
, getAuthor
, setDate
and getDate
.
Among them, setTitle
the , setAuthor
and setDate
member functions are used to set the title, author and publication date information of the book, while getTitle
the , getAuthor
and getDate
member functions are used to obtain the title, author and publication date information of the book.
Next, we can create an Book
object :
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 book
an Book
object called and use the setTitle
, setAuthor
and setDate
member functions to set the book's title, author, and publication date information.
Then, we use the getTitle
, getAuthor
and getDate
member functions to obtain the three information of the book, and cout
print them to the screen through the statement. In this way, we have successfully created a Book
object , and manipulated and viewed it.