C++ common function 2

  • Pure virtual function
  • All virtual functions
  • Simulate a Java interface callback

Pure virtual function

The header file:

class BaseActivity {
public:
    void onCreate(a);
    / / TODO virtual functions
// virtual string getLayoutID();

    //TODO pure virtual function
    virtual string getLayoutID(a) = 0;

    virtual void initView(a) = 0;
};

class MainActivity : public BaseActivity {
public:
    // Subclasses must be implemented because they are pure virtual functions
    string getLayoutID(a);

    // Subclasses must be implemented because they are pure virtual functions
    void initView(a);
};
Copy the code

Implementation file

void BaseActivity::onCreate(a) {
    this->getLayoutID();

    this->initView();
    std: :cout << "Layout file is:" << this->getLayoutID() << endl;
}

string MainActivity::getLayoutID(a) {
    return "R.layout.main_layout";
}

void MainActivity::initView(a) {
    std: :cout << "finviewById(xxx)" << endl;
}
Copy the code

The main file

int main(a) {
	MainActivity mainActivity;
	
	mainActivity.onCreate();
	return 0;
}
Copy the code

The running results are as follows:

The layout file of finviewById(XXX) is R.layout.main_layoutCopy the code

Features of pure virtual functions:

  • A function marked with virtual = 0 is a pure virtual function
  • Similar to the abstract method in Java, only need to define the behavior, the concrete implementation of the subclass

All virtual functions

The header file:

// A class whose functions are purely virtual is called a virtual
class Persion {
    virtual string getName(a) = 0;

    virtual int getAge(a) = 0;

    virtual string getSex(a) = 0;
};

class Student1 : public Persion {
public:
    string getName(a);

    int getAge(a);

    string getSex(a);

    void showInfo(a);

};

class Student2 : public Persion {
public:
    string getName(a);

    int getAge(a);

    string getSex(a);

    void showInfo(a);
};
Copy the code

Implementation file:


// A pure virtual function
string Student1::getName(a) {
    return "Student1";
}

int Student1::getAge(a) {
    return 14;
}

string Student1::getSex(a) {
    return "Male";
}

void Student1::showInfo(a) {
    std: :cout << this->getName() << "\t"
              << this->getAge() << "\t"
              << this->getSex() << "\t" << endl;
}

string Student2::getName(a) {
    return "Student2";
}

int Student2::getAge(a) {
    return 17;
}

string Student2::getSex(a) {
    return "Female";
}

void Student2::showInfo(a) {
    std: :cout << this->getName() << "\t"
              << this->getAge() << "\t"
              << this->getSex() << "\t" << endl;
}
Copy the code

The main file

int main(a){
 	Student1 student1;
    student1.showInfo();

    Student2 student2;
    student2.showInfo();
	return 0;
}
Copy the code

Running results:

Student1	14Male Student217Copy the code

Holomorphic virtual function features:

  • A class whose functions are purely virtual is called a total virtual
  • Similar to the interface in Java, only need to define the behavior, the concrete implementation of the subclass

Simulate a Java interface callback

The header file:


class Bean {
public:
    string name;
    string password;

    Bean(string.string);
};

// User implementation
class User {
public:
    // Returns a pure virtual function on success
    virtual void success(int code, string message, Bean bean) = 0;

    // Returns a pure virtual function on failure
    virtual void error(int code, string message) = 0;
};

// User implementation
class UserImpl : public User {
public:
    void success(int code, string message, Bean bean);

    void error(int code, string message);
};


string setting(string name, string password, User &user);
Copy the code

Implementation function:


void UserImpl::success(int code, string message, Bean bean) {
    std: :cout << "code:" << code << ", message:" << message << endl;
}

void UserImpl::error(int code, string message) {
    std: :cout << "code:" << code << ", message:" << message << endl;
}


// Set the account password
string setting(string name, string password, User &user) {
    if (name.empty() || name.empty()) {
        return "Please check whether the account password is entered!";
    }

    if (name == "shizhenjiang" && password == "123456") {
        user.success(200."Congratulations, you logged in.", Bean(name, password));
        return "Congratulations, login success!";
    } else {
        user.error(404."Login failed");
        return "Congratulations, login failed!";
    }
}

Bean::Bean(string name, string password) : name(name), password(password) {
}
Copy the code

The main file

int main(a){
	//TODO simulates Java interface callbacks
    string name;
    string password;
    cout << "Please enter your account password:"<<name << endl;
    std: :cin >> name;
    std: :cin >> password;
    cout << "The account number you entered is:"<<name << endl;
    cout << "Your password is:"<<password << endl;

    UserImpl userimpl;

    setting(name,password,userimpl);
	return 0;
}
Copy the code

Running results:

Please enter your account password:1234Shizhenjiang The account you entered is:1234Your password is: shizhenjiang code:404, message: login failedCopy the code

Confused: why use UserImpl inherited from User here?

In a class, as long as there is a pure virtual function, there is no reference to that class, so the effect can only be achieved by subclass inheriting from parent class

(Destructor, copy function, friend function, Constructor, static function, etc.)

Original is not easy, your praise is my biggest support ~