1.类的定义
在C++中,类(class)是面向对象编程的基本构建块。它用于定义一种数据类型,该数据类型可以包含数据成员(属性)和成员函数(方法)。下面是一个C++类的基本定义示例:
#include
#include
class Person {
public:
// 数据成员
std::string name;
int age;
// 默认构造函数
Person() : name("Unknown"), age(0) {}
// 带参数的构造函数
Person(std::string n, int a) : name(n), age(a) {}
// 成员函数
void introduce() const {
std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl;
}
};
int main() {
// 创建对象
Person person1; // 使用默认构造函数
person1.introduce();
Person person2("Alice", 30); // 使用带参数的构造函数
person2.introduce();
return 0;
}
class Person { ... };
定义了一个名为 Person
的类。public
:表示公有的,可以被类外部访问。name
和 age
,分别是字符串和整数类型。Person()
初始化 name
为 "Unknown" 和 age
为 0。Person(std::string n, int a)
用于初始化成员变量。introduce()
用于打印对象的介绍信息。main
函数中,实例化了 Person
类的对象 person1
和 person2
,并调用了 introduce()
方法。1.1类域
在C++中,类域(或称为成员域)通常是指类里面定义的成员变量和成员函数。类域主要用来封装数据和提供可以操作这些数据的方法。类域可以分为以下两种主要组成部分:
#include
#include
class Rectangle {
private:
// 数据成员(私有)
double width;
double height;
public:
// 构造函数
Rectangle(double w, double h) : width(w), height(h) {}
// 成员函数(公有)
double area() const {
return width * height; // 计算矩形面积
}
void setWidth(double w) {
width = w; // 设置宽度
}
void setHeight(double h) {
height = h; // 设置高度
}
double getWidth() const {
return width; // 获取宽度
}
double getHeight() const {
return height; // 获取高度
}
};
int main() {
Rectangle rect(10.0, 5.0); // 创建对象
std::cout << "Area: " << rect.area() << std::endl;
// 修改矩形的宽度和高度
rect.setWidth(12.0);
rect.setHeight(6.0);
std::cout << "New Area: " << rect.area() << std::endl;
return 0;
}
2.实例化
在C++中,实例化是指创建一个特定类的对象的过程。当一个类被实例化时,内存为这个对象分配空间,并且通过构造函数初始化对象的状态。每个对象都是类的一个实例,可以拥有自己的数据成员属性值。
#include
#include
class Car {
public:
// 数据成员
std::string brand;
int year;
// 构造函数
Car(std::string b, int y) : brand(b), year(y) {}
// 成员函数
void displayInfo() const {
std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
}
};
int main() {
// 实例化 Car 类的对象
Car myCar("Toyota", 2020); // 创建对象并初始化
// 调用成员函数
myCar.displayInfo(); // 输出:Brand: Toyota, Year: 2020
return 0;
}
3.this指针
在C++中,this
指针是一个隐式指针,每一个非静态成员函数都有一个隐藏的指针,指向调用该成员函数的对象。它用于在类的成员函数内部引用调用该函数的对象。this
指针提供了访问当前对象的能力,尤其在以下场景中很有用:
this
指针来明确区分两者。*this
可以允许连续调用相同对象的多个成员函数。this
可以帮助实现特定的操作,例如判断当前对象是否与另一个对象相同。 #include
#include
class Box {
private:
double width;
public:
// 构造函数
Box(double w) : width(w) {}
// 修改宽度的成员函数
void setWidth(double width) {
this->width = width; // 使用 this 指针区分
}
// 获取宽度的成员函数
double getWidth() const {
return this->width; // 也可以使用 this 指针,虽然可选
}
// 返回当前对象的引用
Box& compareWidth(const Box& b) {
if (this->width > b.width) {
std::cout << "Current box is wider." << std::endl;
} else if (this->width < b.width) {
std::cout << "Current box is narrower." << std::endl;
} else {
std::cout << "Both boxes have the same width." << std::endl;
}
return *this; // 返回当前对象的引用
}
};
int main() {
Box box1(10.5);
Box box2(8.0);
// 修改 box1 的宽度
box1.setWidth(9.5);
std::cout << "Box 1 width: " << box1.getWidth() << std::endl; // 输出:Box 1 width: 9.5
// 比较宽度
box1.compareWidth(box2);
return 0;
}
Box(double w)
初始化了一个 width
数据成员。setWidth
:
this->width = width;
明确区分了成员变量 width
和参数 width
。在这种情况下,this
指针帮助我们访问对象的成员变量。getWidth
:
this->width
是可选的,因为成员变量和 this
关键字的隐式含义已经指明。compareWidth
:
this
) 的宽度与另一个 Box
对象的宽度,并返回当前对象的引用,以允许链式调用。在C++和C语言中实现栈(Stack)数据结构的方式有一些不同。这主要是由于C++支持面向对象编程,而C语言则是过程导向的。下面是对两种语言中栈的实现进行比较的示例。
在C语言中,栈通常使用结构体和函数来管理。以下是一个基本栈的实现:
#include
#include
#include
#define MAX 100
typedef struct {
int top;
int items[MAX];
} Stack;
// 初始化栈
void initStack(Stack* s) {
s->top = -1;
}
// 检查栈是否为空
bool isEmpty(Stack* s) {
return s->top == -1;
}
// 检查栈是否为满
bool isFull(Stack* s) {
return s->top == MAX - 1;
}
// 入栈
bool push(Stack* s, int value) {
if (isFull(s)) {
printf("Stack is full.\n");
return false;
}
s->items[++s->top] = value;
return true;
}
// 出栈
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return -1; // 返回-1表示错误
}
return s->items[s->top--];
}
// 获取栈顶元素
int peek(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return -1; // 返回-1表示错误
}
return s->items[s->top];
}
int main() {
Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
printf("Top element: %d\n", peek(&stack));
printf("Popped: %d\n", pop(&stack));
printf("Top element: %d\n", peek(&stack));
return 0;
}
在C++中,栈的实现通常使用类,强调封装和数据隐藏。以下是一个基本栈的实现:
#include
#include
class Stack {
private:
std::vector items;
public:
// 入栈
void push(int value) {
items.push_back(value);
}
// 出栈
int pop() {
if (isEmpty()) {
std::cout << "Stack is empty." << std::endl;
return -1; // 返回-1表示错误
}
int value = items.back();
items.pop_back();
return value;
}
// 获取栈顶元素
int peek() const {
if (isEmpty()) {
std::cout << "Stack is empty." << std::endl;
return -1; // 返回-1表示错误
}
return items.back();
}
// 检查栈是否为空
bool isEmpty() const {
return items.empty();
}
// 检查栈是否为满(在这里不实现满的概念,因为使用vector)
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
std::cout << "Top element: " << stack.peek() << std::endl;
std::cout << "Popped: " << stack.pop() << std::endl;
std::cout << "Top element: " << stack.peek() << std::endl;
return 0;
}
特性 | C语言实现 | C++语言实现 |
---|---|---|
数据结构 | 使用结构体 | 使用类(class) |
封装 | 通过函数实现部分封装 | 采用类的概念,封装数据和方法 |
内存管理 | 使用固定大小的数组 | 使用动态数组(如 std::vector ) |
入栈/出栈 | 需要手动管理栈顶索引 | 通过类的方法管理,无需手动管理 |
错误处理 | 通过返回值和打印错误消息 | 通过函数返回值和异常处理 |
内存和性能 | 结构简单,性能高(但有限制) | 使用 STL 容器,灵活性好但可能稍有开销 |
在C语言中,栈的实现通常更加底层,使用结构体和函数,通过手动管理内存和索引来实现。而在C++中,栈的实现则更为优雅,利用类和容器的特性简化了代码逻辑,同时加强了数据的封装性和可维护性。C++的实现通常更易于理解、使用和扩展。