C++基础之面向对象编程思想(一)(C++入门必学:面向对象编程思想详解(一))

原创
ithorizon 6个月前 (10-20) 阅读数 23 #后端开发

C++基础之面向对象编程思想(一)

一、面向对象编程概述

面向对象编程(Object-Oriented Programming,简称OOP)是一种编程范式,它强调将数据(属性)和操作数据的方法(函数)封装在一起,形成一个个具有自主功能的对象。C++作为一种赞成多范式的编程语言,既赞成面向过程编程,也赞成面向对象编程。本文将详细介绍C++面向对象编程的基础知识。

二、面向对象编程的核心概念

面向对象编程首要包括以下几个核心概念:

  • 封装(Encapsulation)
  • 继承(Inheritance)
  • 多态(Polymorphism)

三、封装

封装是指将数据(属性)和操作数据的方法(函数)组合在一起,形成一个类(Class)。类是面向对象编程的基本单位,用于描述具有相同属性和方法的对象。

以下是一个易懂的封装示例:

class Person {

private:

string name;

int age;

public:

Person(string name, int age) {

this->name = name;

this->age = age;

}

void setName(string name) {

this->name = name;

}

string getName() {

return name;

}

void setAge(int age) {

this->age = age;

}

int getAge() {

return age;

}

};

四、继承

继承是面向对象编程的一个重要特性,允许我们从一个现有的类(父类)创建一个新的类(子类)。子类继承了父类的所有属性和方法,同时还可以添加新的属性和方法或覆盖父类的方法。

以下是一个易懂的继承示例:

class Animal {

public:

void eat() {

cout << "Animal is eating." << endl;

}

};

class Dog : public Animal {

public:

void bark() {

cout << "Dog is barking." << endl;

}

};

五、多态

多态是指允许不同类的对象对同一消息做出响应。在C++中,多态首要通过虚函数(Virtual Function)实现。

以下是一个易懂的多态示例:

class Animal {

public:

virtual void makeSound() {

cout << "Animal is making a sound." << endl;

}

};

class Dog : public Animal {

public:

void makeSound() override {

cout << "Dog is barking." << endl;

}

};

class Cat : public Animal {

public:

void makeSound() override {

cout << "Cat is meowing." << endl;

}

};

Animal *animal1 = new Dog();

Animal *animal2 = new Cat();

animal1->makeSound(); // 输出:Dog is barking.

animal2->makeSound(); // 输出:Cat is meowing.

delete animal1;

delete animal2;

六、构造函数和析构函数

构造函数和析构函数是类的两个特殊成员函数,分别用于对象的初始化和清理。

构造函数在创建对象时自动调用,用于初始化对象的成员变量。析构函数在对象销毁时自动调用,用于执行一些清理工作,如释放资源。

以下是一个包含构造函数和析构函数的示例:

class Person {

private:

string name;

int age;

public:

Person(string name, int age) {

this->name = name;

this->age = age;

cout << "Person created." << endl;

}

~Person() {

cout << "Person destroyed." << endl;

}

// 省略其他成员函数...

};

七、静态成员

静态成员是类的成员,但不是类的对象的成员。静态成员在所有对象之间共享,可以通过类名直接访问。

以下是一个包含静态成员的示例:

class Person {

private:

static int count;

public:

Person() {

count++;

}

~Person() {

count--;

}

static int getCount() {

return count;

}

};

int Person::count = 0;

int main() {

cout << "Number of persons: " << Person::getCount() << endl; // 输出:0

Person p1;

cout << "Number of persons: " << Person::getCount() << endl; // 输出:1

Person p2;

cout << "Number of persons: " << Person::getCount() << endl; // 输出:2

return 0;

}

八、友元函数

友元函数是定义在类外部的函数,但它可以访问类的私有和保护成员。友元函数不是类的成员,但可以通过类名直接调用。

以下是一个包含友元函数的示例:

class Box {

private:

double width;

public:

Box(double w) : width(w) {}

double getWidth() {

return width;

}

friend void printWidth(const Box& b);

};

void printWidth(const Box& b) {

cout << "Width of box: " << b.getWidth() << endl;

}

int main() {

Box box(10.0);

printWidth(box); // 输出:Width of box: 10

return 0;

}

九、总结

本文介绍了C++面向对象编程的基础知识,包括封装、继承、多态、构造函数、析构函数、静态成员和友元函数等概念。通过这些核心概念,我们可以创建更加模块化、可重用和易于维护的代码。在后续文章中,我们将继续深入探讨C++面向对象编程的其他高级特性。


本文由IT视界版权所有,禁止未经同意的情况下转发

文章标签: 后端开发


热门