设计模式 之美 -- 面向对象(C/C++分别实现)
生活随笔
收集整理的這篇文章主要介紹了
设计模式 之美 -- 面向对象(C/C++分别实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 封裝
- C++實現
- C 實現
- 繼承
- C++ 實現
- C實現
前言
為了保證代碼的可復用性、可擴展性、可維護性,我們提出了面向對象的思想。
面向對象的核心特性有以下幾個
- 封裝特性
信息隱藏或者數據訪問保護。類通過暴露有限的訪問接口,授權外部僅能通過類提供的方式來訪問內部信息或者數據。
封裝用來提升代碼的可擴展性、可維護性 - 繼承特性
繼承是用來表示類之間的 is-a 關系,分為兩種模式:單繼承和多繼承。單繼承表示一個子類只繼承一個父類,多繼承表示一個子類可以繼承多個父類。為了實現繼承這個特性,編程語言需要提供特殊的語法機制來支持。繼承主要是用來解決代碼可復用性的問題 - 多態特性
多態是指子類可以替換父類,在實際的代碼運行過程中,調用子類的方法實現。
多態可以提高代碼的可擴展性和可復用性
C++提供語法來實現以上三個特性,而C語言則可以使用函數指針來實現以上三個特性。
封裝
C++實現
class Person{
private:char *pFName;char *pLName;
public:Person(const char * const str1,const char * const \str2):pFName(str1),pLNname(str2){}~Person();void Person_DisplayInfo();void Person_WriteToFile(const char* pFileName);
}void Person::Person_DisplayInfo()
{cout <<"pFName is " << this->pFName<< endl;cout <<"pLName is " << this -> pLName << endl;
}void Person::Person_WriteToFile(const char* pFileName)
{if(!pFileName) {cout <<"file name is null" << endl;return;}FILE *fp;printf("write ot file name is: %s\n",pFileName);fp = fopen(pFileName, "a+");char *buff = NULL;buff = (char *)malloc(sizeof(DATA_SIZE));if(!buff) {printf("malloc failed\n");return;}int count;strcpy(buff,this->pFName);count = fwrite(buff,sizeof(char),strlen(buff),fp);printf("write to %s's result is %d \n",pFileName,count);fclose(fp);fp = NULL;
}int main()
{Person obj("lili","chuchu");obj.Person_DisplayInfo();obj.Person_WriteToFile("test.txt");return 0;
}
C 實現
person.h
#include <stdio.h>
#include <stdlib.h>typedef struct _Person Person;//declaration of pointers to functions
typedef void (*fptrDisplayInfo)(Person*);
typedef void (*fptrWriteToFile)( Person*, const char*);
typedef void (*fptrDelete)( Person *) ;typedef struct _Person {char* pFName;char* pLName;//interface for functionfptrDisplayInfo Display;fptrWriteToFile WriteToFile;fptrDelete Delete;
}Person;Person* new_Person(const char* const pFirstName, const char* const pLastName); //constructorvoid delete_Person(Person* const pPersonObj); //destructorvoid Person_DisplayInfo(Person* const pPersonObj);
void Person_WriteToFile(Person* const pPersonObj, const char* pFileName);
person.c
#include <stdio.h>
#include <stdlib.h>
#include "person.h"#define DATA_SIZE 1024 Person* new_person(const char* const pFirstName, const char* const pLastName)
{Person *obj = NULL;obj = (Person *)malloc(sizeof(Person));if(!obj){return NULL;}obj -> pFName = malloc(sizeof(char)*(strlen(pFirstName)+1));if(!obj -> pFName) {return NULL;}strcpy(obj -> pFName, pFirstName);obj -> pLName = malloc(sizeof(char) * (strlen(pLastName) + 1));if(!obj -> pLName) {return NULL;}strcpy(obj -> pLName, pLastName);/*construct the function pointer*/obj->Delete = delete_Person;obj->Display = Person_DisplayInfo;obj->WriteToFile = Person_WriteToFile;printf("finish constructor Person\n");return obj;
}void Person_WriteToFile(Person* const pPersonObj, const char* pFileName)
{if(!pPersonObj || !pFileName) {return;}FILE *fp;printf("write ot file name is: %s\n",pFileName);fp = fopen(pFileName, "a+");char *buff = NULL;buff = (char *)malloc(sizeof(DATA_SIZE));if(!buff) {printf("malloc failed\n");return;}int count;strcpy(buff,pPersonObj->pFName);count = fwrite(buff,sizeof(char),strlen(buff),fp);printf("write to %s's result is %d \n",pFileName,count);fclose(fp);fp = NULL;
}void delete_Person(Person* const pPersonObj)
{if(!pPersonObj) {return;}if(pPersonObj -> pFName) {free(pPersonObj->pFName);pPersonObj -> pFName = NULL;}if(pPersonObj -> pLName) {free(pPersonObj -> pLName);pPersonObj -> pLName = NULL;}}void Person_DisplayInfo(Person* const pPersonObj) {if(!pPersonObj) {printf("obj is not exists, please construct\n");}printf("pFName is %s\n",pPersonObj -> pFName);printf("pLName is %s\n",pPersonObj -> pLName);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "person.h"
//#include "employee.h"int main(int argc, char *argv[]) {Person *obj;obj = new_person("pfirst_name","plast_name");obj -> Display(obj);obj -> WriteToFile(obj,"F:\\test_write.txt");obj -> Delete(obj);return 0;
}
繼承
C++ 實現
class Person{
private:char *pFName;char *pLName;
public:Person(const char * const str1,const char * const \str2):pFName(str1),pLNname(str2){}~Person(){}void Person_DisplayInfo();void Person_WriteToFile(const char* pFileName);
}class Empolyee:public Person{
private:char* pDepartment;char* pCompany;int nSalary;
public:Empolyee(const char * const str1,const char * const\str2,int num):Person(str1,str2),pDepartment(str1),pCompany(str2),nSalary(num){}~Empolyee(){}void Employee_DisplayInfo();
}
void Empolyee::Employee_DisplayInfo()
{Person_DisplayInfo();//可以訪問基類的公有成員cout << "pDepartment is " << pDepartment << endl;cout << "pCompany is " << pCompany<< endl;cout << "nSalary is " << nSalary << endl;
}
C實現
person.c如上,person.h增加萬能指針void *
person.h
#include <stdio.h>
#include <stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */typedef struct _Person Person;//declaration of pointers to functions
typedef void (*fptrDisplayInfo)(Person*);
typedef void (*fptrWriteToFile)( Person*, const char*);
typedef void (*fptrDelete)( Person *) ;typedef struct _Person {void *pDerivedObj; char* pFName;char* pLName;//interface for functionfptrDisplayInfo Display;fptrWriteToFile WriteToFile;fptrDelete Delete;
}Person;Person* new_Person(const char* const pFirstName, const char* const pLastName); //constructorvoid delete_Person(Person* const pPersonObj); //destructorvoid Person_DisplayInfo(Person* const pPersonObj);
void Person_WriteToFile(Person* const pPersonObj, const char* pFileName);
employee.h
#include "Person.h"typedef struct _Employee Employee;typedef struct _Employee
{Person* pBaseObj;char* pDepartment;char* pCompany;int nSalary;//If there is any employee specific functions; add interface here.}Employee;Person* new_Employee(const char* const pFirstName, const char* const pLastName,const char* const pDepartment, const char* const pCompany, int nSalary); //constructor
void delete_Employee(Person* const pPersonObj); //destructorvoid Employee_DisplayInfo(Person* const pPersonObj);
employee.c
#include <stdio.h>
#include <stdlib.h>#include "employee.h"Person* new_Employee(const char* const pFirstName, const char* const pLastName,const char* const pDepartment, const char* const pCompany, int nSalary)
{Employee* pEmpObj;//calling base class construtorPerson* pObj = new_person(pFirstName, pLastName);//allocating memorypEmpObj = malloc(sizeof(Employee));if (pEmpObj == NULL){pObj->Delete(pObj);return NULL;}pObj->pDerivedObj = pEmpObj; //pointing to derived object//initialising derived class memberspEmpObj->pDepartment = malloc(sizeof(char)*(strlen(pDepartment)+1));if(pEmpObj->pDepartment == NULL){return NULL;}strcpy(pEmpObj->pDepartment, pDepartment);pEmpObj->pCompany = malloc(sizeof(char)*(strlen(pCompany)+1));if(pEmpObj->pCompany== NULL){return NULL;}strcpy(pEmpObj->pCompany, pCompany);pEmpObj->nSalary = nSalary;//Changing base class interface to access derived class functions//virtual destructor//person destructor pointing to destrutor of employeepObj->Delete = delete_Employee;pObj->Display = Employee_DisplayInfo;//pObj->WriteToFile = Employee_WriteToFile;printf("finish construct employee\n");return pObj;
}void delete_Employee(Person* const pPersonObj)
{if(!pPersonObj){return;}Employee* pEmpObj;pEmpObj = pPersonObj->pDerivedObj;if(pEmpObj->pDepartment) {free(pEmpObj->pDepartment);pEmpObj->pDepartment = NULL;}if(pEmpObj->pCompany) {free(pEmpObj->pCompany);pEmpObj->pCompany = NULL;}pPersonObj->Delete(pPersonObj);
}void Employee_DisplayInfo(Person* const pPersonObj) {if(!pPersonObj) {printf("pPersonObj is null\n");return;}Employee* pEmpObj;pEmpObj = pPersonObj->pDerivedObj;printf("pEmpObj's pDepartment is %s\n",pEmpObj->pDepartment);printf("pEmpObj's pCompany is %s\n",pEmpObj->pCompany);printf("pEmpObj's nSalary is %d\n",pEmpObj->nSalary);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "person.h"
//#include "employee.h"int main(int argc, char *argv[]) {Person *obj;obj = new_person("pfirst_name","plast_name");obj -> Display(obj);obj -> WriteToFile(obj,"F:\\test_write.txt");obj -> Delete(obj);Person *pobj;pobj = new_Employee("Gauri", "Jaiswal","HR", "TCS", 40000);pobj -> Display(pobj);pobj -> Delete(pobj);return 0;
}
總結
以上是生活随笔為你收集整理的设计模式 之美 -- 面向对象(C/C++分别实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美宜佳加盟费的话大概需要多少钱啊?
- 下一篇: 设计模式 之美 -- 单例模式