laitimes

Object Oriented(1)

author:Abin Casual Recording

Briefly describe what object orientation is

1, object-oriented is a programming idea, everything as an object. Package the property variables owned by these classes and the functions that manipulate them into a class.

2. Process-oriented and object-oriented differences

Process-oriented: Write code from top to bottom based on business logic

Object-oriented: Encapsulating data and functions allows for rapid development and reduces repetitive code rewrites.

Briefly describe the three characteristics of object orientation

Encapsulation, inheritance, polymorphism

1. Packaging

Combine data and manipulation data methods, hide the properties and implementation details of the object, and expose only the interface to interact with the object. The essence of encapsulation is management.

2. Inheritance

You can use all the functionality of an existing class and extend it without rewriting the principle class.

Inheritance method:

Inheritance mode private inheritance protected inheritance public inheritance
A private member of the base class Not visible Not visible Not visible
A protected member of a base class Become a private member Still a protected member Still a protected member
A public member of the base class Become a private member Become a protected member Remains a public member

3. Polymorphism

The same operation acts on different objects, can have different interpretations, and produce different execution results, which is polymorphism. That is, to point to the object of the subclass with a reference to the base class.

Polymorphic implementation: rewrite, overload

Briefly describe the overloading and rewriting of C++, and their differences

1. Rewrite

Assigns a redefined function that exists in the raw class. Its function name, parameter list, return value type, all must be consistent with the function being overridden in the base class, and the overridden function of the derived class will be called when the derived class object is called, and the overridden function will not be called.

Functions that are overridden in the rewritten base class must have virtual decorations.

example:

#include<bits/stdc++.h>  using namespace std;  class A { public:  virtual void fun()  {   cout << "A";  } }; class B :public A { public:  virtual void fun()  {   cout << "B";  } }; int main(void) {  A* a = new B();  a->fun();//输出B,A类中的fun在B类中重写 }           

2. Overloading

We use several functions in our usual code writing but their implementations are the same, but some of the details are different. In C++, it was proposed to define multiple functions by a single function name, which is called function overloading.

Function overloading refers to several functions of the same name that are declared in the same accessible area with different parameter columns (type, number, order of parameters), according to the parameter list to determine which function to call, overload does not care about the function return type.

#include<bits/stdc++.h>  using namespace std;  class A {  void fun() {};  void fun(int i) {};  void fun(int i, int j) {};     void fun1(int i,int j){}; };           

How C++ overloading and rewriting are implemented

1. C++ uses the name mangling technique to change the name of the function and distinguish between functions of the same name with different parameters. The naming tilt is done during the compilation phase.

2. Add the virtual keyword before the function of the base class, override the function in the derived class, and the runtime will call the corresponding function according to the actual type of the object.

The object type is a derived class, which calls the function of the derived class; if the object type is a base class, it calls the function of the base class.

  • Functions that are declared with the virtual keyword are called virtual functions, and virtual functions are certainly member functions of the class.
  • Classes with virtual functions have a one-dimensional table of virtual functions called virtual tables, and objects of the class have a virtual pointer to the beginning of the virtual table. A virtual table corresponds to a class, and a virtual table pointer corresponds to an object.
  • Polymorphism is an interface multiple implementation that is at the core of object orientation, divided into class polymorphism and function polymorphism.
  • Overrides are implemented using virtual functions, combined with dynamic binding.
  • Pure virtual functions are virtual functions plus = 0.
  • An abstract class is a class that includes at least one pure virtual function.

    Pure virtual function: virtual void fun()=0. That is, the abstract class must implement this function in the subclass, that is, it has a name first, no content, and implements the content in the derived class.

How the C language implements overloads in the C++ language

Functions with the same name are not allowed in C because function naming is the same at compile time, unlike C++, which adds parameter types and return types as the compiled names of the functions, thus implementing overloading. If you want to visualize function overloads in C, you can do so in the following ways:

1, the use of function pointers to achieve, overloaded functions can not use the same name, but similar implementation of function overloading function.

2. Overloaded functions use variable parameters, such as opening a file open function.

3, gcc has built-in functions, the program uses compiled functions to achieve function overloading

example:

#include<stdio.h>   void func_int(void * a) {     printf("%d\n",*(int*)a);  //输出int类型,注意 void * 转化为int }   void func_double(void * b) {     printf("%.2f\n",*(double*)b); }   typedef void (*ptr)(void *);  //typedef申明一个函数指针   void c_func(ptr p,void *param) {      p(param);                //调用对应函数 }   int main() {     int a = 23;     double b = 23.23;     c_func(func_int,&a);     c_func(func_double,&b);     return 0; }           

There are several types of constructors, each of which does what

Constructors in C++ can be divided into 4 categories: default constructors, initialization constructors, copy constructors, and move constructors.

1. Default construction and initialization structure. Initialization is done when the object of the class is defined.

example:

class Student { public:  //默认构造函数  Student()  {     num=1001;        age=18;      }  //初始化构造函数  Student(int n,int a):num(n),age(a){} private:  int num;  int age; }; int main() {  //用默认构造函数初始化对象S1  Student s1;  //用初始化构造函数初始化对象S2  Student s2(1002,18);  return 0; }           

There are parameter constructs, and the compiler no longer provides a default constructor.

2. Copy structure

#include "stdafx.h" #include "iostream.h"  class Test {     int i;     int *p; public:     Test(int ai,int value)     {         i = ai;         p = new int(value);     }     ~Test()     {         delete p;     }     Test(const Test& t)     {         this->i = t.i;         this->p = new int(*t.p);     } }; //复制构造函数用于复制本类的对象 int main(int argc, char* argv[]) {     Test t1(1,2);     Test t2(t1);//将对象t1复制给t2。注意复制和赋值的概念不同     return 0; }           
  • The assignment constructor implements a copy of the value (shallow copy) by default.
  • 3. Move constructor. Used to implicitly convert variables of other types to objects of this class. The following conversion constructor converts r of type int to an object of type Student, with the age of the object r and num of 1004.

    Student(int r) {  int num=1004;  int age= r; }           

    Defines an empty class, which functions are generated by default

    Parameterless constructors, copy constructors, assignment operators, destructors (non-virtual).

    The initialization order of C++ class objects, the order in case of multiple inheritance

    1. Creates an object of the derived class, and the constructor of the base class is called first (also over the member classes in the derived class)
    2. If there is a member class in the class, the constructor of the member class takes precedence over the constructor of the class itself; (also takes precedence over the constructor of the class itself)
    3. Base class constructors If there are multiple base classes, the constructor is called in the order in which a class appears in the class derivation table, not in the order in which they appear in the member initialization table
    4. Member Class Object Constructors If there are multiple member class objects, the order in which the constructors are called is the order in which the objects are declared in the class, not the order in which they appear in the member initialization table
    5. Derived class constructors, as a general rule derived class constructors should not be able to assign values directly to a base class data member but pass values to the appropriate base class constructors, otherwise the implementation of the two classes becomes tightly coupled and will be more difficult to correctly modify or extend the base class implementation. (The responsibility of the base class designer is to provide an appropriate set of base class constructors)

    summary:

    Parent Class Constructor – > member class object constructor – > its own constructor

    The initialization of the member variables in the middle is related to the order in which they are declared, and the order in which the constructor is called is the order in which the class is derived

    The order of destruction and the order of construction are reversed