C++ encapsulation and abstraction

Table of contents

1 Introduction to encapsulation and abstraction

Two code examples


An  Introduction to Encapsulation and Abstraction

In object-oriented programming, the concept of class is very important, and the concepts of inheritance, polymorphism, and encapsulation related to classes are even more core. Next, briefly introduce the concepts of encapsulation and abstraction.

Encapsulation: Expose the interface to the outside, hide the specific implementation details, and provide necessary protection for key data.

Abstraction: It is actually based on encapsulation, which is to abstract the characteristics and behaviors of the real world, organize and encode them in the form of classes, and finally achieve reuse.

Two code examples

base class

//
// 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) {

}

Derived class 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) {

}

Derived class 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) {

}

main function:

#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;
}

Complete code clone address: [email protected]:XiaoWang_csdn/cpp_13_abstructandhide.git

Guess you like

Origin blog.csdn.net/XiaoWang_csdn/article/details/130818764