In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about what classes and objects are in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Classes and objects
object-oriented
It has always been process-oriented programming such as C language, until the 1970s, process-oriented programming showed deficiencies in the development of large-scale programs. The computer industry put forward the idea of object-oriented (Object Oriented Programming), in which the core concepts are classes and objects, and the three major characteristics of object-oriented are encapsulation, inheritance and polymorphism.
Process-oriented and object-oriented are only two ideas with different emphasis in computer programming. Process-oriented is the most practical way of thinking, of which the most important is the idea of modularization. Process-oriented pays more attention to the steps of process, action or event. Even object-oriented contains the idea of process-oriented. compared with process-oriented, object-oriented method is mainly to objectify things, thinking that things can be transformed into a series of objects and the relationship between them. more in line with the way people perceive things.
With the takeout system as an example, the process-oriented idea will modularize the steps of ordering, picking up, delivering, taking orders and so on, which are reflected in the program as functions. Object-oriented thinking will reduce the whole process to the relationship between objects and objects, that is, the relationship between merchants, riders and users and them, which is reflected in the program is the design of classes.
Object-oriented is a broad and profound thought, which can not be understood thoroughly in a long time, but needs to be realized slowly in study and work.
Unlike Java, C++ is a pure object-oriented language. C++ is based on object-oriented but compatible with C, so it can also be process-oriented.
1. Definition of the class / / Cstruct Student {char name [20]; int age; int id;}; struct Student age; int strcpy (s.name, "yyo"); s.age = 18 positions.id = 11 name name Student {/ / member variable char _ class [20]; class _ age; int _ id / / member method void Init (const char* name, int age, int id) {strcpy (_ name, name); _ age = age; _ id = id;} void Print () {cout = (const Date& d); / / d._year) {return true } else if (_ year = = d._year & & _ month > d._month) {return true;} else if (_ year = = d._year & & _ month = = d._month & & _ day > d._day) {return true;} return false } / / operator overloading > = bool Date::operator > = (const Date & d) {return (* this > d) | | (* this = = d);} / / operator overloading GetMonthDay () {_ day-= GetMonthDay (); / / maximum value of days subtraction-first decrement, then carry _ month++ / / month carry / / check whether the number of months is legal if (_ month = = 13) {_ month = 1; _ year + = 1 Bing Bang / year carry}} return * this;}
Such an implementation method will change the value of the object, so it is better to directly implement it as + = and return a reference to the object to avoid calling the copy construct.
Implement + overload and then demultiplex + =.
/ / operator overloading + Date Date::operator+ (int day) {/ / temporary variables are destroyed and cannot be referenced by Date ret (* this); ret + = day; / / ret.operator+= (day); return ret;}
Create a temporary variable and initialize it with * this, then use the temporary variable for the + = operation and return the temporary variable. Note that the temporary variable is destroyed with the stack frame and its reference cannot be returned.
Overloading of subtraction operator
/ / operator overload-= / / date-days = date Date& Date::operator-= (int day) {/ / prevent days from being negative if (_ day < 0) {return * this + =-day;} _ day-= day; / / check whether the number of days is legal while (_ day)
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.