一、封装性
封装是面向对象编程的一个主要特点,把数据隐藏,通过函数把数据绑定起来,避免受到外界的干扰和误用从而确保了安全,仅向用户暴露接口而把具体的实现细节隐藏起来的机制。、
下面写开始写小案例:
1、 创建一个numberSource.h头文件,声明类、数据、函数:
#ifndef __NUMBERSOURCE_H__
#define __NUMBERSOURCE_H__
#include
#include
using namespace std;
class NumberSource
{
// 数据私有化
private:
string doctorCode;
string doctorName;
string deptCode;
string deptName;
int num;
string noon;
string appDate;
string startTime;
string endTime;
// 通过行为函数操作数据
public:
NumberSource();
NumberSource(NumberSource &obj);
~NumberSource();
void setDoctorCode(string);
void setDoctorName(string);
void setDeptCode(string);
void setDeptName(string);
void setNum(int);
void setNoon(string);
void setAppDate(string);
void setStartTime(string);
void setEndTime(string);
string getDoctorCode();
string getDoctorName();
string getDeptCode();
string getDeptName();
int getNum();
string getNoon();
string getAppDate();
string getStartTime();
string getEndTime();
string printAttr();
virtual void sendMsg()
{
cout << "unknow source"<< endl;
}
};
#endif
2、创建numberSource.cpp源文件实现numberSource.h的类定义:
#include "numberSource.h"
#include
#include
using namespace std;
NumberSource::NumberSource()
{
cout << " no args structure function " << endl;
}
NumberSource::~NumberSource()
{
cout << " destruction function " << endl;
}
// 拷贝函数
NumberSource::NumberSource(NumberSource &obj)
{
cout << " copy structure function " << endl;
}
void NumberSource::setDoctorCode(string doctorCode)
{
this->doctorCode = doctorCode;
}
void NumberSource::setDoctorName(string doctorName)
{
this->doctorName = doctorName;
}
void NumberSource::setDeptCode(string deptCode)
{
this->deptCode = deptCode;
}
void NumberSource::setDeptName(string deptName)
{
this->deptName = deptName;
}
void NumberSource::setNum(int num)
{
this->num = num;
}
void NumberSource::setNoon(string noon)
{
this->noon = noon;
}
void NumberSource::setAppDate(string appDate)
{
this->appDate = appDate;
}
void NumberSource::setStartTime(string startTime)
{
this->startTime = startTime;
}
void NumberSource::setEndTime(string endTime)
{
this->endTime = endTime;
}
string NumberSource::getDoctorCode()
{
return this->doctorCode;
}
string NumberSource::getDoctorName()
{
return this->doctorName;
}
string NumberSource::getDeptCode()
{
return this->deptCode;
}
string NumberSource::getDeptName()
{
return this->deptName;
}
int NumberSource::getNum()
{
return this->num;
}
string NumberSource::getNoon()
{
return this->noon;
}
string NumberSource::getAppDate()
{
return this->appDate;
}
string NumberSource::getStartTime()
{
return this->startTime;
}
string NumberSource::getEndTime()
{
return this->endTime;
}
string NumberSource::printAttr()
{
string numStr = to_string(this->num);
return "doctorCode=" + this->doctorCode + "\n doctorName=" + this->doctorCode + "\n deptCode=" + this->deptCode +
"\n deptName=" + this->deptName + "\n num=" + numStr + "\n noon=" + this->noon+ "\n appDate=" + this->appDate+
"\n startTime=" + this->startTime + "\n endTime=" + this->endTime ;
}
3、 创建app.cpp主函数调用:
#include "numberSource.h"
#include
#include
#include
using namespace std;
int main()
{
cout << "HELLO WOERD" << endl;
NumberSource number;
number.setDoctorCode("00155");
number.setDoctorName("Li Xiao Li");
number.setDeptCode("ZB01022025");
number.setDeptName("shen nei yi men zhen");
number.setNoon("AM");
number.setNum(20);
number.setAppDate("2023-08-25");
number.setStartTime("09:00");
number.setEndTime("12:00");
cout << number.printAttr() <
4、 编写CMakeLists.txt构建文件:
cmake_minimum_required(VERSION 3.10)
project(test20230824)
set(CMAKE_CXX_STANDARD 17)
add_executable(app app.cpp numberSource.cpp numberSource.h)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
二、继承性
继承是依据另一个类来定义一个类,有利于重用代码功能和提高执行时间的效果。
1、在numberSource.h头文件中声明NetNumberSource继承NumberSource
class NetNumberSource: public NumberSource
{
private:
// 子类特有数据
string netName;
public:
NetNumberSource();
NetNumberSource(NetNumberSource &obj);
~NetNumberSource();
void setNetName(string);
string getNetName();
string printAttr();
void sendMsg() override
{
cout << " send net source" << endl;
}
};
2、在numberSource.app中实现NetNumberSource
NetNumberSource:: NetNumberSource()
{
cout << "NetNumberSource no args structure function " << endl;
}
NetNumberSource::NetNumberSource(NetNumberSource &obj)
{
cout << "NetNumberSource copy structure function " << endl;
}
NetNumberSource::~NetNumberSource()
{
cout << "NetNumberSource copy structure function " << endl;
}
void NetNumberSource::setNetName(string netName)
{
this->netName=netName;
}
string NetNumberSource::getNetName()
{
return this->netName;
}
string NetNumberSource::printAttr()
{
// 子类重写父类函数
string superStr=NumberSource::printAttr();
return superStr+"\n netName="+this->netName;
}
3、子类实例化测试
NetNumberSource number;
/// 子类继承了父类的 setDoctorCode() setDoctorName()等函数
number.setDoctorCode("00155");
number.setDoctorName("Li Xiao Li");
number.setDeptCode("ZB01022025");
number.setDeptName("shen nei yi men zhen");
number.setNoon("AM");
number.setNum(20);
number.setAppDate("2023-08-25");
number.setStartTime("09:00");
number.setEndTime("12:00");
number.setNetName(" NET SOURCE");
cout << number.printAttr() <
三、多态性
多态是类的多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态,调用成员函数时会根据调用函数的对象的类型来执行不同的函数。
1、在numberSource.h头文件中声明ComNumberSource继承NumberSource
class ComNumberSource: public NumberSource
{
private:
string comName;
public:
ComNumberSource();
~ComNumberSource();
string printAttr();
void sendMsg() override
{
cout << " send com source" << endl;
}
};
2、在numberSource.app中实现ComNumberSource
ComNumberSource::ComNumberSource()
{
}
ComNumberSource::~ComNumberSource()
{
}
string ComNumberSource::printAttr()
{
string s = NumberSource::printAttr();
return s+"\n comName="+this->comName;
}
父子关系图如下:
3、多态测试
NumberSource* number;
NetNumberSource netSource;
number= &netSource;
number->sendMsg();
cout << "\n--------------------\n" <sendMsg();