当前位置: 首页 > news >正文

C++ 面向对象编程

面向对象编程(Object-Oriented Programming, OOP)是C++语言的一个重要特性,它允许开发者以更直观和模块化的方式来设计和构建程序。OOP的四个主要原则是:封装(Encapsulation)、继承(Inheritance)、多态(Polymorphism)和抽象(Abstraction)。

1、封装

封装是将数据和操作数据的方法绑定在一起,形成一个不可分割的整体(即对象)。通过封装,可以隐藏对象的内部实现细节,只对外暴露必要的接口。

C++ 的类一般定义在头文件里

Person.h

#pragma once
#include <string>
#include <iostream>
using namespace std;
class Person
{
protected:int number;string name;
public:void setNumber(int newNumber) {number = newNumber;}int getNumber() {return number;}void setName(string newName) {name = newName;}string getName() {return name;}void say() {cout << "hello";}
};

#pragma once 程序编译一次

private 修饰的变量为成员属性,成员属性只能通过get、set方法调用 

对象创建和方法调用

#include <string>
#include <iostream>
#include "Person.h"
using namespace std;void main() {Person stu;stu.setNumber(10);cout << stu.getNumber() << endl;stu.say();
}

2、继承

继承允许新的类(子类或派生类)继承现有类(父类或基类)的属性和方法。这有助于代码复用和扩展。

#pragma once
#include "Person.h"
class Student :public Person
{
private:double score;public:void setScore(double newScore) {score = newScore;}double getScore() {return score;}string toString() {return "[" + to_string(getNumber()) + ","+ getName() + "," + to_string(getScore())+"]";}};

Student类继承了Person基类

3、多态

多态允许将父类对象视为子类对象来处理,从而实现接口的动态绑定。C++中的多态通常通过虚函数(virtual function)来实现。

多态实现条件:

1)子类继承父类   

2)子类重写父类方法

3)子类向上转型 

父类Animal

class Animal
{
public:virtual void makeSound() {cout << "animal sound" << endl;}
};

子类 Dog ,并且实现父类方法

class Dog : public Animal
{
public:void makeSound() override{cout << "dog sound" << endl;}};

子类 Cat ,并且实现父类方法

class Cat : public Animal
{
public:void makeSound() override {cout << "cat sound" << endl;}
};

子类向上转型 

void playSound(Animal& animal) {animal.makeSound();
}int main() {Dog dog;Cat cat;playSound(dog); //输出 dog soundplaySound(cat); //输出 cat soundreturn 0;
}

makeSound函数在Animal类中声明为虚函数,并在DogCat类中重写。这使得playSound函数可以接收不同类型的Animal对象,并根据对象的实际类型调用相应的makeSound方法。

 4、抽象

抽象是指将复杂系统的实现细节隐藏起来,只暴露其接口或公共行为。在C++中,抽象通常通过抽象基类(包含纯虚函数的类)来实现。

class Shape
{
public:virtual void draw() = 0;//纯虚函数
};
class Circle : public Shape
{
public:void draw() override {cout << "drawing circle" << endl;}
};
class Square : public Shape
{
public:void draw() override {cout << "drawing square" << endl;}
};
int main() {Shape* shape1 = new Circle();Shape* shape2 = new Square();shape1->draw();shape2->draw();return 0;
}

Shape类是一个抽象基类,包含一个纯虚函数drawCircleSquare类继承自Shape类,并实现了draw方法。

http://www.lryc.cn/news/509827.html

相关文章:

  • 我的Serverless实战——引领云计算的下一个十年,附答案
  • 有哪些其他方法可以实现数据一致性验证?
  • vue 基础学习
  • HarmonyOS NEXT 实战之元服务:静态案例效果---查看国际航班服务
  • PetaLinux 内核输出信息的获取方式
  • Android使用辅助服务AccessibilityService实现自动化任务
  • 工业大数据分析算法实战-day15
  • C语言实现顺序表详解
  • 【ES6复习笔记】对象方法扩展(17)
  • 【视觉惯性SLAM:相机成像模型】
  • 学习笔记(C#基础书籍)-- C#基础篇
  • 操作系统(26)数据一致性控制
  • ubuntu24.04使用opencv4
  • 【项目构建】Gradle入门
  • Electron -- Electron应用主要核心(二)
  • 【前端开发】HTML+CSS+JavaScript前端三剑客的基础知识体系了解
  • git命令恢复/还原某个文件、删除远程仓库中的文件
  • 二十一、Ingress 进阶实践
  • ES学习Promise对象(九)
  • 寻找适合小户型的开源知识库open source knowledge base之路
  • Linux高级--2.6 网络面试问题
  • 在 CentOS 7 上安装 Node.js 20 并升级 GCC、make 和 glibc
  • 音视频入门基础:MPEG2-TS专题(20)——ES流简介
  • 五子棋小游戏设计(Matlab)
  • 基于Pycharm与数据库的新闻管理系统(3)MongoDB
  • WebRtc webrtc-streamer部署
  • CVPR-2024 | 具身导航模型大一统!NaviLLM:学习迈向具身导航的通用模型
  • CAN201 Introduction to Networking(计算机网络)Pt.2 传输层
  • git仓库多人协作新建分支 合并到主分支流程详解
  • Visual Studio 使用 GitHub Copilot 与 IntelliCode 辅助编码 【AI辅助开发系列】