C++封装与抽象

目录

一 封装与抽象简介

二 代码示例


一 封装与抽象简介

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

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

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

二 代码示例

基类

//
// 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地址: [email protected]:XiaoWang_csdn/cpp_13_abstructandhide.git

猜你喜欢

转载自blog.csdn.net/XiaoWang_csdn/article/details/130818764