As a professional in the surveying industry, merely using surveying instruments for measurement and software for drawing is far from sufficient. For instance, to process a batch of surveying data, one needs to understand programming to utilize computers for batch processing.Additionally, developing an internal data processing software also requires programming skills. Furthermore, modern three-dimensional urban construction, navigation technology, remote sensing image processing and analysis, etc., are all closely related to computers. To effectively utilize computers, one must understand a programming language.Programming can enhance our work efficiency, allowing computers to truly serve our industry. To become a versatile talent, one must learn a programming language and apply it in practice. This serves as a record of my daily learning and progress in programming. Please feel free to point out any mistakes.
C++Object-Oriented Programming
C++ is a high-level programming language designed for object-oriented programming. C++ introduces new object-oriented mechanisms such as classes, inheritance, polymorphism, and overloading based on the traditional C language.
In object-oriented programming, the term ‘object’ typically refers to an abstraction that simulates and represents real-world entities, which generally includes both the attributes and behaviors of the object.C++ abstracts the attributes and behaviors of an object into data members and member functions, encapsulating them within a class. An instance of a class is referred to as an object of that class.
Each object of a class can typically only access the public member functions and public data members of that class. Private member functions and data members within the class can only be accessed by the member functions of that class. Classes interact with the outside world through external interfaces, and objects communicate with each other through messages.
A class is an abstraction of objects that share common attributes (data members) and behavioral characteristics (member functions). Member functions respond to messages sent to the object, and the invocation of member functions is achieved through message passing.A constructor is a special type of class member function that ensures each object is correctly initialized. The constructor provides a method for initializing the private data members of the class. If a class has a constructor, the constructor of that class is automatically called when an object is created.A destructor serves the opposite purpose of a constructor; when an object’s lifetime ends, the program automatically calls the object’s destructor, which is generally used to handle cleanup tasks after the object is released, such as freeing the memory occupied by the object.Both constructors and destructors share the same name as their class but cannot have any return type.When a program creates a new object, it first allocates appropriate memory for the object based on the class definition, then automatically calls the constructor of that class to initialize that memory area, thus completing the initialization of the object.The this pointer, in C++, each object has a pointer to itself called the this pointer. By using the this pointer, an object can determine its own address and implicitly reference its data members and member functions.The this pointer can also be used as a return value for functions.Static members can be divided into static data members and static member functions. Static member functions are shared among all objects of the class; regardless of how many objects of the class exist, there is only one instance of the class’s static members, which reside in a common memory area. Static data members must be initialized within file scope. Static member functions are associated only with a specific class, not with a specific class object.A friend function is a function defined outside of a class that is not a member function but can access all members of that class, including private and protected members. The scope of a friend function begins at its declaration and ends simultaneously with the scope of the class that declares the friend function. When calling a friend function, the object it is accessing must be specified in its parameter list, rather than the class’s members. Friend functions can be declared anywhere within the class, and the friendship relationship is neither commutative nor transitive.Inheritance allows for the creation of a new class based on an existing class, where the new class inherits the properties and behaviors of the existing class. To give the new class its own functionality, it must also modify these behaviors and properties.A derived class is a new class created from an existing class, referred to as a derived class or subclass, while the existing class is called the parent class or base class. A derived class can also serve as a base class for future derived classes.
To be continued~