C++封装与抽象

文章介绍了面向对象编程中的封装和抽象概念,通过基类`Person`及其派生类`PersonA`和`PersonB`的代码示例,展示了如何定义接口、隐藏实现细节以及使用纯虚函数实现多态。在主函数中,展示了基类指针调用子类方法的用法,体现了继承和多态的特性。
摘要由CSDN通过智能技术生成

目录

一 封装与抽象简介

二 代码示例


一 封装与抽象简介

在面向对象的程序设计中,类的概念非常重要,那么与类相关的继承、多态、封装概念更是核心。接下来简单介绍一下封装与抽象的概念。

封装: 对外暴露接口,隐藏具体的实现细节,并且对关键数据进行必要的保护。

抽象:实际上是基于封装的,就是把现实世界的特征和行为抽象出来,以类的形式进行组织、编码,最后实现复用。

二 代码示例

基类

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_13_ABSTRUCTANDHIDE_PERSON_H
#define CPP_13_ABSTRUCTANDHIDE_PERSON_H

#include "iostream"
#include "string"

using namespace std;


class Person {
public:
    string occupation; //职业
    string name; //姓名
    int age; //年龄
    Person();
    Person(string oc,string n,int age);

    //纯虚函数,由子类实现
    virtual void job() = 0;  //工作
    virtual void family() = 0; //家庭

    //私有成员的get与set方法
    int getIncomeSum() const;
    void setIncomeSum(int incomeSum);

private:
    int incomeSum; //总收入

};


#endif //CPP_13_ABSTRUCTANDHIDE_PERSON_H





//
// Created by 11010 on 2023/4/9.
//

#include "Person.h"

int Person::getIncomeSum() const {
    return incomeSum;
}

void Person::setIncomeSum(int incomeSum) {
    Person::incomeSum = incomeSum;
}


Person::Person() {

}

Person::Person(string oc, string n, int age):occupation(oc),name(n),age(age) {

}

派生类A:

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_13_ABSTRUCTANDHIDE_PERSONA_H
#define CPP_13_ABSTRUCTANDHIDE_PERSONA_H


#include "Person.h"

//子类A
class PersonA : public Person{
public:
  void job() override;
  void family() override;

  PersonA();
  PersonA(string oc,string name,int age);
};


#endif //CPP_13_ABSTRUCTANDHIDE_PERSONA_H





//
// Created by 11010 on 2023/4/9.
//

#include "PersonA.h"

void PersonA::job() {
 cout<<name<<"的工作是:"<<occupation<<",年龄为:"<<age<<endl;
}

void PersonA::family() {
 cout<<name<<"家庭情况为:"<<"年收入"<<getIncomeSum()<<"万"<<endl;
}

PersonA::PersonA():Person() {

}

PersonA::PersonA(string oc, string name, int age) : Person(oc, name, age) {

}

派生类B:

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_13_ABSTRUCTANDHIDE_PERSONB_H
#define CPP_13_ABSTRUCTANDHIDE_PERSONB_H


#include "Person.h"

//子类B
class PersonB : public Person{
public:
    void job() override;
    void family() override;

    PersonB();
    PersonB(string oc,string name,int age);
};


#endif //CPP_13_ABSTRUCTANDHIDE_PERSONB_H





//
// Created by 11010 on 2023/4/9.
//

#include "PersonB.h"


void PersonB::job() {
    cout<<name<<"的工作是:"<<occupation<<",年龄为:"<<age<<endl;
}

void PersonB::family() {
    cout<<name<<"家庭情况为:"<<"年收入"<<getIncomeSum()<<"万"<<endl;
}

PersonB::PersonB():Person() {

}

PersonB::PersonB(string oc, string name, int age) : Person(oc, name, age) {

}

主函数:

#include <iostream>
#include "Person.h"
#include "PersonA.h"
#include "PersonB.h"

int main() {

    //创建基类对象: 因为基类是抽象类,所以不能有实例,但是它可以指向子类的实例
    Person *person= nullptr;

    //创建子类A的对象
    PersonA *personA=new PersonA("电器工程师","李兴",37);
    //创建子类B的对象
    PersonB *personB=new PersonB("C++开发工程师","大衡",30);


    //继承父类set的方法,先设置属性值
    personA->setIncomeSum(30);
    //调用子类各自的实现的虚函数方法
    personA->job();
    personA->family();

    personB->setIncomeSum(50);
    personB->job();
    personB->family();


    //使用父类指针指向子类对象,并使用父类指针调用子类方法
    person=personA;
    person->job();
    person->family();

    person=personB;
    person->job();
    person->family();

    return 0;
}

完整代码clone地址: git@gitcode.net:XiaoWang_csdn/cpp_13_abstructandhide.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值