面向对象编程(OOP)是一种程序设计范式,它通过将程序结构化为对象来促进代码的重用性、扩展性和维护性。在 C++ 中,面向对象编程的核心特性包括 类(Class) 、对象(Object) 、封装(Encapsulation) 、继承(Inheritance) 、多态(Polymorphism) 和 抽象(Abstraction) 。
1. 类和对象
类是创建对象的模板,对象是类的实例。类定义了对象的属性(成员变量)和行为(成员函数)。
例子:
#include <iostream>
using namespace std;
// 定义一个类
class Car {
public:
// 成员变量
string brand;
int year;
// 成员函数
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// 创建对象
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2020;
// 调用成员函数
myCar.displayInfo();
return 0;
}
2. 构造函数和析构函数
- 构造函数:在创建对象时自动调用,用于初始化对象。
- 析构函数:在对象销毁时自动调用,用于释放资源。
例子:
#include <iostream>
using namespace std;
class Person {
public:
// 构造函数
Person(string n, int a) {
name = n;
age = a;
cout << "Constructor called!" << endl;
}
// 析构函数
~Person() {
cout << "Destructor called!" << endl;
}
void displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
private:
string name;
int age;
};
int main() {
// 创建对象,自动调用构造函数
Person p("John", 30);
p.displayInfo();
// 自动调用析构函数
return 0;
}
3. 封装(Encapsulation)
封装是指将对象的属性(数据)和行为(方法)捆绑在一起,并通过访问控制来限制外界直接访问对象的内部数据。通过设置访问权限(public、private 和 protected),我们可以控制数据的可见性和可修改性。
例子:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // 私有成员,外部不能直接访问
public:
// 构造函数
BankAccount(double initialBalance) {
balance = initialBalance;
}
// 公有方法,允许外部获取余额
double getBalance() {
return balance;
}
// 公有方法,允许存款
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
// 公有方法,允许取款
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
};
int main() {
BankAccount account(1000);
account.deposit(500);
account.withdraw(200);
cout << "Current Balance: " << account.getBalance() << endl;
return 0;
}
4. 继承(Inheritance)
继承是面向对象编程的一个重要特性,允许一个类继承另一个类的属性和方法,从而实现代码的重用和扩展。
例子:
#include <iostream>
using namespace std;
// 基类
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// 派生类
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks!" << endl;
}
};
int main() {
Dog dog;
dog.eat(); // 从基类继承的方法
dog.bark(); // 派生类自己的方法
return 0;
}
5. 多态(Polymorphism)
多态允许对象以不同的方式响应相同的消息。C++ 中的多态有两种形式:编译时多态(函数重载和运算符重载)和运行时多态(虚函数和动态绑定) 。
例子:运行时多态(虚函数)
#include <iostream>
using namespace std;
// 基类
class Animal {
public:
virtual void sound() { // 虚函数
cout << "Some animal makes a sound." << endl;
}
};
// 派生类
class Dog : public Animal {
public:
void sound() override { // 重写基类的虚函数
cout << "The dog barks!" << endl;
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "The cat meows!" << endl;
}
};
int main() {
Animal* animal;
Dog dog;
animal = &dog;
animal->sound(); // 输出 "The dog barks!"
Cat cat;
animal = &cat;
animal->sound(); // 输出 "The cat meows!"
return 0;
}
6. 抽象(Abstraction)
抽象是隐藏复杂性,只暴露必要的功能。在 C++ 中,抽象类是不能直接实例化的类,它至少包含一个纯虚函数。
例子:
#include <iostream>
using namespace std;
// 抽象类
class Shape {
public:
// 纯虚函数
virtual void draw() = 0;
};
// 派生类
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle." << endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a rectangle." << endl;
}
};
int main() {
Shape* shape;
Circle circle;
shape = &circle;
shape->draw(); // 输出 "Drawing a circle."
Rectangle rectangle;
shape = &rectangle;
shape->draw(); // 输出 "Drawing a rectangle."
return 0;
}
7. 运算符重载(Operator Overloading)
运算符重载允许你自定义 C++ 运算符的行为,使其适应自定义的类类型。
例子:
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r, double i) : real(r), imag(i) {}
// 运算符重载
Complex operator + (const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3.0, 4.0);
Complex c2(1.0, 2.0);
Complex c3 = c1 + c2; // 使用重载的加法运算符
c3.display(); // 输出 "4 + 6i"
return 0;
}
总结
面向对象编程的基本特性可以通过 C++ 中的类、对象、继承、多态、封装和抽象等概念实现。以上是一些基础用法.