In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to write oriented objects in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to write oriented objects in C language.
The following I will use a factory class to achieve the production of specific cars, Mercedes-Benz, BMW, Audi cars will be produced through the factory class, by the parent pointer to the specific car instance:
Header file:
/ / Car.h # ifndef CAR_H_ # define CAR_H_ typedef enum CarType_E {CAR_TYPE_BENZE = 0, CAR_TYPE_BMW, CAR_TYPE_AUDI, CAR_TYPE_NONE,} CarType_E; class BaseCar {public: BaseCar (CarType_E CarType); virtual ~ BaseCar (); virtual void CarSpeaker (); CarType_E _ CarType;} Class BenzeCar: public BaseCar {public: BenzeCar (CarType_E CarType); ~ BenzeCar (); public: void CarSpeaker ();}; class BMWCar: public BaseCar {public: BMWCar (CarType_E CarType); ~ BMWCar (); void CarSpeaker ();}; class AudiCar: public BaseCar {public: AudiCar (CarType_E CarType); ~ AudiCar (); void CarSpeaker ();} Class CarFactory {public: BaseCar* createNewCar (CarType_E CarType);}; # endif / * CAR_H_ * /
Source code:
/ / Car.cpp # include "Car.h" # include using namespace std; BaseCar::BaseCar (CarType_E CarType): _ CarType (CarType) {printf ("BaseCar create\ n");} BaseCar::~BaseCar () {printf ("BaseCar delete\ n");} void BaseCar::CarSpeaker () {std::cout createNewCar (CAR_TYPE_BMW); BaseCar* newAudiCar = carFactory- > createNewCar (CAR_TYPE_AUDI); newBenzeCar- > CarSpeaker () NewBMWCar- > CarSpeaker (); newAudiCar- > CarSpeaker (); delete newBenzeCar; newBenzeCar = NULL; delete newBMWCar; newBMWCar = NULL; delete newAudiCar; newAudiCar = NULL; delete carFactory; carFactory = NULL; return 0;}
Compiled output:
The above is a source example of a simple factory pattern. Now, let's talk about why we can also implement these three features of object-oriented thinking in C:
The first is encapsulation: the encapsulation of C++ encapsulates the functions and properties of abstract classes and is not open to the public. External use of these properties and methods must be accessed through a concrete instance object. We know that once the header file is included in the C language, the functions and variables in the header file can be used. In fact, C language can also use a method to achieve this effect. That is to use structure + function pointer + static. Properties and function pointers are defined in the structure. Static limits the use of methods to this module and accesses them externally through pointer functions. In this way, object-oriented encapsulation can be achieved.
For inheritance: C++ object-oriented inheritance can inherit the properties and methods of the parent class, and there is the memory of the parent object in the memory of the subclass object, then, if we use C language to write, we can completely define a parent class variable in the parent class structure, and construct the parent class while using the subclass, then we can achieve the property of inheritance.
For polymorphism: C++ allows a parent pointer to a subclass entity. When using this pointer, if the method is a virtual function, the execution action will be executed in the specific subclass function. The essential way to achieve this is through a virtual function pointer, because we use C language to write object-oriented is to use function pointers to encapsulate functions. Then we can make the function pointer of the variable of the parent class of the structure body point to the function of the subclass to achieve the characteristic of polymorphism.
All right, after playing tricks in front of you, let's start with the specific code implementation:
Header file:
# ifndef CAR_H_ # define CAR_H_ # include # include typedef enum CarType {CAR_BENZE = 0, CAR_BMW, CAR_AUDI, CAR_NONE,} CarType; typedef struct Base_Car {CarType car_type; void (* speaker) (struct Base_Car* car); void* parent_car; / / point to parent,if no any parent,then make it NULL} Base_Car Typedef struct Benze_Car {Base_Car* car; void (* speaker) (struct Base_Car* car);} Benze_Car; typedef struct BMW_Car {Base_Car* car; void (* speaker) (struct Base_Car* car);} BMW_Car; typedef struct Audi_Car {Base_Car* car; void (* speaker) (struct Base_Car* car);} Audi_Car Typedef struct Car_Factory {Base_Car* (* create_new_car) (CarType car_type);} Car_Factory; Car_Factory* new_car_factory (); void delete_car_factory (Car_Factory* car_factory); Base_Car* new_Base_Car (); Benze_Car* new_benze_Car (); BMW_Car* new_bmw_Car (); Audi_Car* new_audi_Car () Void delete_Base_Car (struct Base_Car* car); void delete_Benze_Car (struct Benze_Car* car); void delete_BMW_Car (struct BMW_Car* car); void delete_Audi_Car (struct Audi_Car* car); # endif / * CAR_H_ * /
Source file:
# include "Car.h" static void Car_speaker (struct Base_Car* car) {printf ("this is a car\ n");} static void Benze_speaker (struct Base_Car* car) {printf ("this is Benze Car, car type is:% d\ n", car- > car_type);} static void BMW_speaker (struct Base_Car* car) {printf ("this is BMW Car, car type is:% d\ n", car- > car_type) } static void Audi_speaker (struct Base_Car* car) {printf ("this is Audi Car, car type is:% d\ n", car- > car_type);} Benze_Car* new_benze_Car () {Benze_Car* real_car = (Benze_Car*) malloc (sizeof (Benze_Car)); Base_Car* base_car = new_Base_Car (); printf ("Benze_Car create\ n") Real_car- > car = base_car; real_car- > speaker = Benze_speaker; base_car- > car_type = CAR_BENZE; base_car- > parent_car = (void*) real_car; base_car- > speaker = real_car- > speaker; return real_car;} BMW_Car* new_bmw_Car () {BMW_Car* real_car = (BMW_Car*) malloc (sizeof (BMW_Car)) Base_Car* base_car = new_Base_Car (); printf ("BMW_Car create\ n"); base_car- > car_type = CAR_BMW; real_car- > car = base_car; real_car- > speaker = BMW_speaker; base_car- > car_type = CAR_BMW; base_car- > parent_car = (void*) real_car; base_car- > speaker = real_car- > speaker; return real_car } Audi_Car* new_audi_Car () {Audi_Car* real_car = (Audi_Car*) malloc (sizeof (Audi_Car)); Base_Car* base_car = new_Base_Car (); printf ("Audi_Car create\ n"); base_car- > car_type = CAR_AUDI; real_car- > car = base_car; real_car- > speaker = Audi_speaker; base_car- > car_type = CAR_AUDI Base_car- > parent_car = (void*) real_car; base_car- > speaker = real_car- > speaker; return real_car;} Base_Car* new_Base_Car () {Base_Car* base_car = (Base_Car*) malloc (sizeof (Base_Car)); printf ("BaseCar create\ n"); base_car- > car_type = CAR_NONE; base_car- > parent_car = NULL Base_car- > speaker = Car_speaker; return base_car;} Base_Car* create_new_Car (CarType car_type) {Base_Car* base_car = NULL; switch (car_type) {case CAR_BENZE: {Benze_Car* real_car = new_benze_Car (); base_car = real_car- > car; break } case CAR_BMW: {BMW_Car* real_car = new_bmw_Car (); base_car = real_car- > car; break;} case CAR_AUDI: {Audi_Car* real_car = new_audi_Car (); base_car = real_car- > car; break } default: break;} return base_car;} void delete_Benze_Car (struct Benze_Car* car) {free (car- > car); car- > car = NULL; free (car); printf ("Benze_Car delete\ n");} void delete_BMW_Car (struct BMW_Car* car) {free (car- > car); car- > car = NULL; free (car) Printf ("BMW_Car delete\ n");} void delete_Audi_Car (struct Audi_Car* car) {free (car- > car); car- > car = NULL; free (car); printf ("Audi_Car delete\ n") } void delete_Base_Car (struct Base_Car* car) {if (NULL! = car- > parent_car) {switch (car- > car_type) {case CAR_BENZE: {delete_Benze_Car ((Benze_Car*) car- > parent_car); car = NULL; / / base car will be delete in child free function break } case CAR_BMW: {delete_BMW_Car ((BMW_Car*) car- > parent_car); car = NULL; break;} case CAR_AUDI: {delete_Audi_Car ((Audi_Car*) car- > parent_car); car = NULL; break } default: break;}} if (NULL! = car) {free (car); car = NULL;} printf ("Base_Car delete\ n");} Car_Factory* new_car_factory () {Car_Factory* car_factory = (Car_Factory*) malloc (sizeof (Car_Factory)); car_factory- > create_new_car = create_new_Car Return car_factory;} void delete_car_factory (Car_Factory* car_factory) {free (car_factory); car_factory = NULL;}
Test file main.cpp
# include # include "Car.h" int main () {Car_Factory* car_factory = new_car_factory (); Base_Car* benzeCar = car_factory- > create_new_car (CAR_BENZE); Base_Car* bmwCar = car_factory- > create_new_car (CAR_BMW); Base_Car* audiCar = car_factory- > create_new_car (CAR_AUDI); benzeCar- > speaker (benzeCar); bmwCar- > speaker (bmwCar) AudiCar- > speaker (audiCar); delete_Base_Car (benzeCar); benzeCar = NULL; delete_Base_Car (bmwCar); bmwCar = NULL; delete_Base_Car (audiCar); audiCar = NULL; delete_car_factory (car_factory); car_factory = NULL; return 0;}
Execute after compilation:
Thank you for your reading. the above is the content of "how to write object-oriented objects in C language". After the study of this article, I believe you have a deeper understanding of how to write object-oriented objects in C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.