Class class

Classes are object-oriented features. There is no concept of a class in C. CPP has abstract classes, but no interface. CPP supports multiple inheritance.

A common class:

class Fruit
{
    private:
        
    public:
        Fruit();
        ~Fruit();
};

Fruit::Fruit()
{
}

Fruit::~Fruit()
{
}
Copy the code

Constructors and destructors:

Where Fruit() represents the constructor and ~Fruit() represents the destructor. The constructor is used to create objects and set initialization parameters. The destructor is executed when the object is destroyed.

Decorator:

  • Private:Represents a private member that cannot be accessed externally, only by its own class and friend functions.
  • public:Represents a public member that can be accessed externally.
  • protected:Represents protected members. Protected members are similar to private members, but subclasses can access protected members.

Class member functions:

When we create a function in a class, we can initialize it directly, or implement it outside the class:

class Fruit { private: int count; public: Fruit(); ~Fruit(); void add(int i); Int getCount(){return count; }}; Fruit::Fruit() { cout << "create fruit" << endl; } Fruit::~Fruit() { cout <<"fruit deleted"<<endl; Void Fruit::add(int I){count = count + I; }Copy the code

Friend functions:

A friend function can be defined in a class, but it is not a member function of the class and must be implemented outside the class. It has access to the private and protected members of the definition class.

Friend class: All functions in a friend class are friends of that class.


#include <iostream>
using namespace std;

class Fruit
{
    private:
        int count = 0;
    public:
        Fruit();
        ~Fruit();

        // friend void printF(Fruit ft);
        friend void printF(Fruit &ft);
};

// void printF(Fruit ft){
//     cout << ft.count <<endl;
// }

void printF(Fruit &ft){
    cout << ft.count <<endl;
}

int main(){

    Fruit fruit;
    
    printF(fruit);

    return 0;
}
Copy the code

This pointer: Any member function of a class can use this pointer to access a member of the class. This pointer is const and cannot be modified.

Virtual functions, pure virtual functions, abstract classes:

Virtual functions: Decorated with the keyword virtual

        virtual void fuck2(){
            cout <<"fuck 2"<<endl;
        }
Copy the code

A virtual function means that when overridden by a subclass, the subclass’s function is called instead of the parent class’s function

Pure virtual function: an empty virtual function

virtual void fuck() = 0;
Copy the code

Abstract class: A class is abstract if it has pure virtual functions

Abstract classes are materialized and must be created by subclasses.

class Fruit { private: public: Fruit(); ~Fruit(); virtual void fuck() = 0; void fuck1(){ cout <<"fuck 1"<<endl; } virtual void fuck2(){ cout <<"fuck 2"<<endl; }};Copy the code

Inheritance: Note that inheritance uses the public modifier,

class Apple: public Fruit
{
    private:
        
    public:
        Apple();
        ~Apple();

        void fuck(){
            cout << "fuck apple"<<endl;
        };

        void fuck2(){
            cout << "fuck apple 22"<<endl;
        };

};
Copy the code

Call to see the output:

    Apple apple;
    apple.fuck();
    apple.fuck1();
    apple.fuck2();
Copy the code

Output:

fuck apple
fuck 1
fuck apple 22
Copy the code
  • Fuck () is a pure virtual function that calls a subclass
  • Fuck1 () calls the parent class’s methods
  • Fuck2 () is a virtual function overridden by subclasses, so it calls subclasses. If the subclass does not override the virtual function, the parent class will still be called.

Multiple inheritance is as follows:

class Apple: public Fruit, public Orange{
    
}
Copy the code

Structure struct

CPP can use structs to create custom data structures, equivalent to bean classes in Java

Create a basic structure:

struct Book
{
    string name;
    int id;
    long int ISBN;
};
Copy the code

}; Create one or more struct objects between:

struct Book
{
    string name;
    int id;
    long int ISBN;
}book1,book2;
Copy the code

Data office initialization:

<! Book1. name = "C++ programing"; <! --> Book book3{"Android++", 1, 21321231}; cout<<book3.id<<endl; cout<<book3.name<<endl; cout<<book3.ISBN<<endl;Copy the code

Using type aliases:

typedef struct{
    int id;
    string name;
}Ebook;

Copy the code

I feel it is useless grammar sugar.

Structure pointer:

Ebook Ebook {2," ah-ha ha ha "}; Ebook *ptr_book; ptr_book = &ebook; cout<< ptr_book->id <<endl; cout<< ptr_book->name <<endl;Copy the code

It’s no different than a regular pointer

What’s the difference between a structure and a class?

  • In general, structs are better thought of as an implementation of a data structure and classes are better thought of as an implementation of an object.
  • Default inherited access: struct is public by default, class is private by default.

What is the difference between structs in C and CPP?

  • C can only be used as a structure of data and cannot have functions, while structs in CPP can have functions
  • C has no modifiers. CPP structures can have public, protected, and private modifiers
  • C: struct prefix (struct);struct Book bookIt’s incredible
  • There is no concept of inheritance in C, structs in CPP can be inherited

Presentation:

struct TBook : public Book
{
    private:
        int ids = 1232342;
    public:
        string names;
        long int TTT;
        void printBook(TBook *book);
}tbook;

void TBook::printBook(TBook *book){
    cout<<book->ids<<endl;
    cout<<book->names<<endl;
    cout<<book->TTT<<endl;

    cout<<book->name<<endl;
}

Copy the code

Perform:

    tbook.name = "C++";
    tbook.names = "Android++";
    tbook.TTT = 1213;

    tbook.printBook(&tbook);
Copy the code

Output result:

1232342
Android++
1213
C++
Copy the code

The appropriate union

A Commons is similar to a structure, but only one data member exists at the same time. When one member is assigned, the other member values are lost.

union ONE { int i; float j; double ids[10]; }; int main(){ ONE one; one.i = 123; cout << one.i <<endl; One. J = 1.0 f; cout << one.i <<endl; //one.i will lose cout << one.j <<endl; return 0; }Copy the code

Common features:

  • The default access control character is public
  • Can contain constructors, destructors
  • Cannot contain a member of a reference type
  • Cannot be inherited from another class or used as a base class
  • Cannot contain virtual functions
  • Anonymous unions have direct access to union members in the scope where they are defined
  • Anonymous unions cannot contain protected or private members
  • The global anonymous union must be static

Use with structure:

struct PEOPLE
{
    string name;
    union TEST
    {
        int id;
        float id_f;
    } test;
    
};
Copy the code