C + + features: encapsulation, inheritance, polymorphism

Posted by estero2002 on Mon, 03 Jan 2022 01:04:13 +0100

1, Class and data abstraction

The idea of object-oriented is to regard anything as an object. The object has its own attributes and behavior. Collecting these attributes is the task of data abstraction.

What is included in an empty class:

class empty
{
public:
    empty();  // non-parameter constructor 
    empty(const empty &rh); // copy constructor 
    ~empty(); // Destructor
    empty & operator=(const empty &rg); // Assignment operator function
};


empty e1;       // e1: "I'm empty!" non-parameter constructor 
empty e2(e1); // e2: "I am as empty as you!" copy constructor 
empty e3 = e1; // e3: "me too! Me too!" Assignment operator
  1. Data abstraction with classes

    public: members can be accessed outside the class.

    private: members can only be accessed within a class.

    protected: can be accessed within a class and its subclasses.

  2. Constructor: a function with the same name as the class. Its function is to initialize member data when the object is generated.

  3. Destructor: ~ function: clean up the member data of the object at the end of the object and release the dynamic memory allocation.

Const object and const member function:

//const modifier variable. Declare the variable as read-only and protect the variable from change. Must initialize.
const int i = 5;
i = 10;//error
//const decorated array 
const int a[9] = {1,2,3 ,4 5,5,9};
//const decorated pointer 1. The value of the restricted pointer to the space cannot be changed. 2. The restricted pointer cannot be changed
int i = 5;
int j = 6;
int k = 7;
const int *p = &i; //Definition 1


Function: declare constant variables. Modifying const data will cause compilation errors.

  1. Friend object and friend class

    Friend friend. The friend function of the class can access the private member of the class. The defined friend function needs to be described in the class definition.

    The parameter referenced by the friend function is the address of the class. The members in the class can be accessed through the address of the class.

    friend  void good(Buddy *b);
    good(&b);
        
    friend  void good(Buddy &b);
    good(b);
    
    friend class class2;//Class, as a friend, can access members in class1.
    
  2. this pointer

    A pointer to itself in the class definition. this pointer can only be defined in member functions. this pointer cannot be used in static functions

  3. Dynamic memory allocation (heap)

    C language: malloc() first applies for a continuous memory heap space and returns a pointer to this memory space. You need to judge whether the return value of the function is NULL.

    malloc allocates a memory size of at least the number of bytes specified by the size parameter.

    int *p = malloc(sizeof(int));
    if(P!= NULL){
        
    }
    free(p);
    

    C++: new

    The new keyword applies for dynamic memory, and the delete keyword releases memory. Dynamic memory is allocated from memory = = heap = =.

    Type name * Pointer = new Type name;
    
    int * p = new int(size);
    delete p;
    
    int * foo;
    foo = new int [5];  //Returns a pointer to the first element of the sequence. The initialization array size is 1.
    foo = new int (1);  // Dynamically allocate an int and initialize it to 1
    
    //new keyword initialization
    int* pi = new int(1);
    float* pf = new float(2.0f);
    char* pc = new char('c');
    

Difference between New and malloc:

The new keyword is part of c + +malloc is a function provided by the c standard library
new allocates memory in units of specific typesmalloc allocates memory in bytes
new can be initialized when applying for a single type variablemalloc does not have the feature of memory initialization
new returns a pointer of typemalloc returns an untyped pointer

2, C + + features: encapsulation, inheritance, polymorphism

Encapsulation: modularization of code

Access type of encapsulated class: public private protected (members can only be accessed by member functions of this class or derived classes)

Inheritance: implementing code extensions

Polymorphism: virtual function, function overload

Virtual functions are accessed through object pointers or references to base classes

The difference between function overloading and function Rewriting:

Function overload:
//Occurs in the same class, with the same function name and different parameter lists
 Function override:
//Function overrides must have inheritance relationships
//Function rewriting requirements: the function of the parent class cannot meet the requirements of the child class
//The rewritten function and the rewritten function, function name and function parameters must be exactly the same
//The return value of the rewritten function is the same as that of the rewritten function
  1. inherit

    Use one class to generate objects of another class. A subclass gets all the members of the parent class, as well as its own members.

    class father{
    
    };
    class son :public father {
    
    };
    
  2. Virtual functions and polymorphism

    Virtual function: defined in the parent class but not implemented, implemented in the child class. The virtual function must be added with virtual to call the member function of the actual subclass through the pointer of the parent class.

    It is mainly to implement an interface and standardize inheritance. (access functions defined by derived classes through base classes)

    **Pure virtual function: * * virtual void funtion1()=0. Function prototype equals 0,

Difference between virtual function and pure virtual function:

virtual functionPure virtual function
Virtual functions must be implementedOnly declaration, no definition
Virtual functions can only achieve polymorphism by means of pointers or referencesA derived class is simply an interface to an inherited function
Virtual functions can be used directlyThis function can only be used if it is implemented in a subclass.

When the base class pointer points to a subclass object and calls the member function of the subclass and the base class with the same name through this pointer, if the base class is declared as a virtual function, the function of the subclass will be called if the subclass is not written, and the function of the base class will be called if it is not declared.

Polymorphism: it is realized through virtual functions, that is, through the virtual functions of the same parent class, subclasses realize different functions.

Polymorphism is embodied in that the parent class reference variable can point to the child class object.

The implementation of polymorphism depends on inheritance. First declare the instance of a parent class and give different child class instances respectively.

Polymorphism is the ability of the same behavior to have multiple different forms or forms.

  1. Template

With templates, you can not only specify all related function overloads. You can also specify all classes

//inherit
#include <iostream>
#include <string>
using namespace std;
class  Father {
public:
	void set_value(int a, int b) {
		height = a;
		width = b;
	}
protected:
	int height;
	int width;
};
class Son : public Father {
public:
	int area() {
		return height * width;
	}
};
int main() {
	Son s1;
	Father F;
	s1.set_value(4,5);
	cout<<s1.area();
	return 0;
}
//Virtual function polymorphism
#include <iostream>
#include <string>
using namespace std;

class  Father {
public:
	 void set_value(int a, int b) {
		height = a;
		width = b;
	}
	 virtual int area() {
		return height * width / 2;
	}
protected:
	int height;
	int width;
};

class Son : public Father {
public:
	 int area() {
		return height * width;
	}
};
class Son1 : public Father {
public:
	int area() {
		return height * width;
	}
};

int main() {
	Son1 s1;
	Father *F = new Son1;
	F->set_value(4,5);
	cout<<F->area();
	return 0;

}

Topics: C++