The content of this article is based on the record of the course “Object-oriented Programming” given by Professor Pan Rong of Xidian University


Software development and object orientation

The characteristics of good software

Reusable, extensible (easy to create new subsystems on top of existing ones), maintainability (adaptable to changing requirements)

Develop ideas and methods

Process oriented

  • Divide the software structure according to the function — determine the input and output of the whole system, and then divide the intermediate process into small subsystems, each subsystem also has input and output, and connect these parts together to form the whole system.

  • Function as the main body, the whole program coupling degree is high.

object-oriented

  • Software system as a collection of objects;
  • The system structure is relatively stable. “Objects” change less than “functions/procedures/functions”;
  • An object encapsulates the definition of a function;
  • Improve software reusability, maintainability and expansibility.

abstract

The key point of abstraction is to extract the essence of an entity, that is, to extract and simplify the essence of complex things.

The features abstracted by the abstraction process should all be related to the system. The simpler an abstraction, the more general it is.

There are many possible ways an entity can be abstracted. Attention needs to be paid to specific abstractions based on actual requirements.

The properties and behaviors that an abstraction holds should not exceed the purpose for which it was originally defined.

Class abstraction and definition

An object is an abstraction of something concrete.

An object is a software structure or module that bundles state (data/attributes) and behavior (actions/operations/functions) together. In other words, an object is an instance of a class with defined property values.

Class is a further abstraction of objects with the same characteristics.

A class is an abstraction that describes the common characteristics of a set of similar objects. Classes can control the visibility of their variables and functions externally — that is, you can set certain variables/functions to be accessible only from within the class object — which improves the security of your program.

The process of creating an object from a class is called “instantiation.”

Different objects instantiated from a class have the same structure, but can have different data values.

UML representation of the class

+ : public – : private # : protected

Access control

Public, private, and protected can be used multiple times. In general, the first two are written before the class.

Outside of a class, only public members can be accessed.

Private members can only be accessed by functions and friends of the class and are not visible outside the class.

Protected members are the same as public for derived classes (that is, subclasses that inherit from the class) and private for other classes.

Members of the

  • Data members cannot be explicitly initialized inside the class body, such as int number = 1. Class is initialized by a constructor (constructor).
  • Member functions are declared the same as before. They should be the only way to manipulate encapsulated (private) data. Member functions can be defined inside or outside a class. Functions defined within a class automatically become inline functions.

Definitions of multifile classes (constructors, destructors see bottom of page)

//time.h
class Time {
public:
    Time(int newH, int newM, int newS = 10);// The constructor can be overloaded
    										// Default values need to be added at declaration time
    ~Time(a);// Destructors have no arguments and cannot be overloaded.
	void setTime(int newH, int newM, int newS);
	void showTime(a);
private:
	int hour, min, second;
};


// time.cpp
#include "time.h"	// The file that needs to contain the declaration

// Use the function body to initialize
Time::Time(int newH, int newM, int newS) {
    hour = newH;
    min = newM;
    second = newS;
}
// Use the initializer list for initialization
Time::Time(int newH, int newM, int newS): hour(newH), min(newM), second:(newS) {}
Time::~Time(...) {
    ...
}

void Time::setTime(int newH, int newM, int newS) {
    hour = newH;
    min = newM;
    second = newS;
}
void Time::showTime(a) {... }// Instantiate the object using a no-argument constructor:
Time myTime1;

// Instantiate an object with and only an argument constructor:
Time myTime(...).;
Copy the code

Create an object

< class name > < object name table >;

Storage is allocated at creation time, and a class is simply a type definition.

A list of object names that can be declared as Pointers, reference names, and arrays of objects.

Object of the UML

Class member access

  • Public members can be accessed directly from objects;
  • You can access all members of a class using their names directly;
  • Object and member name access: example: time.showtime ();
  • Access via object pointer variables: example: TimePtr->showTime();
  • Access by reference.

There are only data members in each object space. There is only one copy of a member function, shared by all objects.

Constructor, destructor

Constructor, destructor

The constructor is called automatically at instantiation time and initialized automatically according to the procedure we defined; Destructors are called automatically at the end of the object’s life cycle to perform cleanup according to the procedure we defined. When a variable is not initialized, its value is random.

The constructor, which is automatically called by the compiled system when an object is defined, creates and initializes the object.

For classes with no constructor, the compilation system prepares a default, no-argument constructor that does nothing.

For classes that explicitly define an argument constructor, the compiler does not automatically add the default no-argument constructor. An error is reported if this notation is not called.

  • Destructors do some cleanup at the end of the life cycle and do not reclaim space. This space should be released by system administration. Destructors cannot take arguments and therefore cannot be overloaded;
  • In general, constructors are called in the same order as objects are declared, and destructors are called in the opposite order.
Construct, destructor call timing

The global object’s constructor is called before all functions (including main) are executed, and the destructor is called when main ends or exit is called.

Local automatic objects: constructed at instantiation, destructor called at the end of function call, object release;

A static local object in a function: the constructor constructor is called the first time its function is called, and the destructor is not released until main is finished or exit is called.

Copy (copy) constructor

The copy constructor name is the same as the class name, does not specify a type, takes one argument and is a reference to an object of the same class.

  • Objects can be assigned and copied

    • Assignment: Two existing objects of the same kind that can be assigned. This assignment is done holistically, regardless of access rights.

      < object 1> = < object 2>;Copy the code
    • Copy: Create a new object and make it the same as the original object

      < class name > < object 2>(< object 1>); < class name > < object 2> = < object 1>;Copy the code

      If the copy constructor is not defined, this is “initialization by member by default.”

      < class name > (const < class name >& < object name >); < class name >::< class name > (const < class name >& < object name >) {// Implement... }Copy the code

      Use cases:

      TAdd(const TAdd& p) {
      	x = p.x;		// You can define your own copy logic here
      	y = p.y;
      	cout<<"Copy constructor"<<endl;
      }
      Copy the code

      The default copy constructor does a simple one-to-one copy.

  • The case with the copy constructor

    // 1.
    // 2. The function takes an object as an argument and needs to pass the argument in its entirety when called
    void func(Box b) {... }int main(a) {...Box box1(1.3.5);
        func(box1);	// The system copies from box1(argument) to b(parameter), where the copy constructor is called automatically. }// 3. The return value of a function is an object, and the return value is returned at the end of the function call
    Box func2(a) {
        Box box1(1.2.3);
        return box1;
    }
    
    int main(a) {
        Box box2;
        box2=func2(a);/* Box1 is copied into a temporary object and then assigned to box2. All that is called here is the copy constructor. * /
        return 0;
    }
    Copy the code