C++常用函数(二)(纯虚函数,全虚函数,模拟 java 中接口回调等)

174 阅读1分钟

C++常用函数二

纯虚函数

头文件:

class BaseActivity {
public:
    void onCreate();
    //TODO 虚函数
//    virtual string getLayoutID();

    //TODO 纯虚函数
    virtual string getLayoutID() = 0;

    virtual void initView() = 0;
};

class MainActivity : public BaseActivity {
public:
    //子类必须实现,因为是纯虚函数
    string getLayoutID();

    //子类必须实现,因为是纯虚函数
    void initView();
};

实现文件

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

    this->initView();
    std::cout << "布局文件为: " << this->getLayoutID() << endl;
}

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

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

main 文件

int main() {
	MainActivity mainActivity;
	
	mainActivity.onCreate();
	return 0;
}

运行结果为:

finviewById(xxx)
布局文件为: R.layout.main_layout

纯虚函数的特点:

  • 用virtual标识 = 0 的叫纯虚函数
  • 类似于 java 中的抽象方法,只需要定义行为,具体实现交给子类

全虚函数

头文件:

//一个类中函数都是纯虚函数叫全虚函数
class Persion {
    virtual string getName() = 0;

    virtual int getAge() = 0;

    virtual string getSex() = 0;
};

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

    int getAge();

    string getSex();

    void showInfo();

};

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

    int getAge();

    string getSex();

    void showInfo();
};

实现文件:


//纯虚函数
string Student1::getName() {
    return "Student1";
}

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

string Student1::getSex() {
    return "男";
}

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

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

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

string Student2::getSex() {
    return "女";
}

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

main 文件

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

    Student2 student2;
    student2.showInfo();
	return 0;
}

运行结果:

Student1	14	男	
Student2	17

全纯虚函数特点:

  • 一个类中函数都是纯虚函数叫全虚函数
  • 类似于 java 中的接口,只需要定义行为,具体实现交给子类

模拟 java 接口回调

头文件:


class Bean {
public:
    string name;
    string password;

    Bean(string, string);
};

//用户实现
class User {
public:
    //成功返回 纯虚函数
    virtual void success(int code, string message, Bean bean) = 0;

    //失败返回  纯虚函数 
    virtual void error(int code, string message) = 0;
};

//用户实现 
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);

实现函数:


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;
}


//设置账号密码
string setting(string name, string password, User &user) {
    if (name.empty() || name.empty()) {
        return "请检查账号密码是否输入!";
    }

    if (name == "shizhenjiang" && password == "123456") {
        user.success(200, "恭喜您,登录成功", Bean(name, password));
        return "恭喜您,登录成功!";
    } else {
        user.error(404, "登录失败");
        return "恭喜您,登录失败!";
    }
}

Bean::Bean(string name, string password) : name(name), password(password) {
}

main 文件

int main(){
	//TODO 模拟 java 接口回调
    string name;
    string password;
    cout << "请输入账号密码: "<<name << endl;
    std::cin >> name;
    std::cin >> password;
    cout << "您输入的账号为: "<<name << endl;
    cout << "您输入的密码为: "<<password << endl;

    UserImpl userimpl;

    setting(name,password,userimpl);
	return 0;
}

运行结果:

请输入账号密码: 
1234
shizhenjiang
您输入的账号为: 1234
您输入的密码为: shizhenjiang
code:404,  message:登录失败

疑惑:
这里为什么要使用UserImpl继承自 User?

在类中,只要有纯虚函数,那么就获取不到该类的引用,所以只能通过子类继承父类的方式,实现效果

在这里插入图片描述

上一章:C++常用函数(一)(析构函数,拷贝函数,友元函数,构造函数,静态函数等)

原创不易,您的点赞就是对我最大的支持~