在本章中,我们将深入探讨C++中的类与对象。类是面向对象编程的核心概念之一,它允许我们创建自定义数据类型,这些数据类型可以封装数据和行为。
类的定义
定义一个简单的类
在C++中,类的定义通常包含数据成员(也称为属性或字段)和成员函数(也称为方法)。下面是一个简单的类定义示例:
class Rectangle {
private:
int width, height;
public:
void set_values(int, int);
int area() { return width * height; }
};成员访问权限
在上述示例中,width 和 height 被声明为私有(private),这意味着它们只能从类的内部访问。而成员函数 set_values 和 area 则被声明为公有(public),这样它们可以从类的外部访问。
创建对象
使用类创建对象
定义了一个类之后,我们可以使用该类来创建对象。创建对象的过程称为实例化。下面是如何创建 Rectangle 类的对象并调用其成员函数的示例:
Rectangle rect; rect.set_values(3, 4); int area = rect.area();
在这个例子中,我们首先创建了一个名为 rect 的 Rectangle 类型的对象,然后通过这个对象调用了 set_values 函数,并计算了矩形的面积。
构造函数
定义构造函数
构造函数是一种特殊的成员函数,它在创建新对象时自动调用。构造函数通常用于初始化对象的状态。构造函数的名字必须与类名相同,并且没有返回类型。
// www.javascriptcn.com code example
class Rectangle {
private:
int width, height;
public:
// 构造函数
Rectangle(int x, int y) {
width = x;
height = y;
}
int area() { return width * height; }
};使用构造函数创建对象
构造函数使对象的创建更加简洁明了:
Rectangle rect(3, 4); // 使用构造函数创建对象 int area = rect.area(); // 计算面积
析构函数
定义析构函数
析构函数也是一种特殊的成员函数,它在对象生命周期结束时被调用。析构函数通常用于释放资源。析构函数的名字必须与类名相同,并且前面加上波浪线 ~,并且没有参数和返回类型。
// www.javascriptcn.com code example
class Rectangle {
private:
int width, height;
public:
Rectangle(int x, int y) {
width = x;
height = y;
}
~Rectangle() {
std::cout << "Rectangle destroyed\n";
}
int area() { return width * height; }
};析构函数的作用
析构函数的主要用途是在对象销毁时执行一些清理操作,例如释放分配的内存或其他资源。
成员函数
成员函数的定义
成员函数是类的一部分,它们可以直接访问类的数据成员。成员函数可以在类定义体内定义,也可以在类定义体外定义。
// www.javascriptcn.com code example
// 在类定义体内定义成员函数
class Rectangle {
private:
int width, height;
public:
Rectangle(int x, int y) {
width = x;
height = y;
}
int area() { return width * height; } // 成员函数定义
};
// 在类定义体外定义成员函数
int Rectangle::area() {
return width * height;
}静态成员函数
静态成员函数是属于类而不是对象的函数。它们可以通过类名直接调用,而不需要创建类的实例。
// www.javascriptcn.com code example
class Rectangle {
private:
static int count;
public:
Rectangle() {
++count;
}
static int get_count() {
return count;
}
};
int Rectangle::count = 0;静态成员变量
静态成员变量是所有对象共享的变量。它们只在类中定义一次,并且在类的所有对象之间共享。
// www.javascriptcn.com code example
class Rectangle {
private:
static int count;
public:
Rectangle() {
++count;
}
static int get_count() {
return count;
}
};
int Rectangle::count = 0;友元函数
定义友元函数
友元函数是一种非成员函数,它可以访问类的私有和保护成员。友元函数需要通过在类定义中声明来指定。
// www.javascriptcn.com code example
class Rectangle {
private:
int width, height;
public:
Rectangle(int x, int y) {
width = x;
height = y;
}
friend int get_area(Rectangle);
};
int get_area(Rectangle rect) {
return rect.width * rect.height;
}友元类
友元类也是一种特殊的关系,它允许一个类访问另一个类的私有和保护成员。
// www.javascriptcn.com code example
class Rectangle {
private:
int width, height;
public:
Rectangle(int x, int y) {
width = x;
height = y;
}
friend class Square; // 声明Square类为友元类
};
class Square {
public:
int side;
void set_side(int s) {
side = s;
}
int area() {
return side * side;
}
};继承
定义基类和派生类
继承允许我们创建一个新的类,这个新的类继承了现有类的特性。这种关系被称为“is-a”关系。继承的基本语法如下:
// www.javascriptcn.com code example
class Base {
protected:
int value;
public:
Base(int val) : value(val) {}
virtual ~Base() {}
virtual int get_value() const { return value; }
};
class Derived : public Base {
public:
Derived(int val) : Base(val) {}
~Derived() {}
int get_value() const override { return value * 2; }
};多态性
多态性是指同一个接口可以有不同的实现方式。C++通过虚函数支持多态性。虚函数允许我们在基类中定义一个函数,并在派生类中重写这个函数。
// www.javascriptcn.com code example
class Shape {
public:
virtual double area() const = 0; // 纯虚函数
virtual ~Shape() {}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override { return width * height; }
};抽象类
抽象类的概念
抽象类是一种不能实例化的类,它通常包含一个或多个纯虚函数。纯虚函数是没有具体实现的虚函数,它们的存在使得类成为抽象类。
class Shape {
public:
virtual double area() const = 0; // 纯虚函数
virtual ~Shape() {}
};抽象类的使用
抽象类主要用于定义接口,子类必须提供纯虚函数的具体实现。
// www.javascriptcn.com code example
class Shape {
public:
virtual double area() const = 0; // 纯虚函数
virtual ~Shape() {}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override { return width * height; }
};以上是关于C++类与对象的详细教程。希望这些内容能帮助你更好地理解和掌握C++面向对象编程的基本概念和技巧。