项目3-警察和厨师1

#include <iostream>
using namespace std;
class Person
{
public:
    Person(int a,string n):age(a),name(n){}
    void action();
    string getname(){return name;}
    int getage(){return age;}

private:
    int age;
    string name;
};
class Polic:public Person
{
public:
    Polic(int a,string n,int l,int a1,string n1):Person(a,n),level(l),leader(a1,n1){cout<<"警察登场"<<endl;}
    void action();
    void arrest(Person &);
private:
    int level;
    Person leader;
};
class Cook:public Person
{
public:
    Cook(int a,string n,double s,Polic p):Person(a,n),salary(s),protector(p){}
    void getcake(int );
private:
    double salary;
    Polic protector;
};
int main()
{

    return 0;
}

void Person::action()
{
    cout<<" name "<<name<<" age "<<age;
}
void Polic::action()
{
    cout<<"警察"<<getname()<<"的年龄为"<<getage()<<","<<"等级为"<<level<<",上司是"<<leader.getname()<<endl;
}
void Polic::arrest(Person &p)
{   p.action();
    cout<<"被逮捕"<<endl;

}
void Cook::getcake(int n)
{

    cout<<"cook "<<getname()<<" give me "<<n<<" cske"<<endl;

}

在 Linux 系统中,动态链接库(共享库)可以定义自己的初始化和清理函数,它们会在动态链接库加载和卸载时自动调用。 初始化函数的名称为 `_init`,其原型为: ```c void _init(void); ``` 清理函数的名称为 `_fini`,其原型为: ```c void _fini(void); ``` 当动态链接库加载时,动态链接器会自动调用 `_init` 函数;当动态链接库卸载时,动态链接器会自动调用 `_fini` 函数。 下面是一个简单的示例,演示如何在动态链接库中定义初始化和清理函数: test.c 文件: ```c #include <stdio.h> __attribute__((constructor)) void init_func() { printf("test.so initialized\n"); } __attribute__((destructor)) void uninit_func() { printf("test.so uninitialized\n"); } void test_func() { printf("test_func called\n"); } ``` 在上述代码中,我们使用 GCC 的 `__attribute__((constructor))` 和 `__attribute__((destructor))` 属性,分别将 `init_func` 和 `uninit_func` 函数定义为初始化函数和清理函数。在 `init_func` 函数中输出初始化信息,在 `uninit_func` 函数中输出清理信息。同时,我们定义了一个 `test_func` 函数,用于在主程序中调用。 编译命令: ``` gcc -shared -fPIC -o libtest.so test.c ``` 在主程序中加载动态链接库,并调用其中的函数: ```c #include <dlfcn.h> int main() { void *handle = dlopen("./libtest.so", RTLD_NOW); if (handle == NULL) { fprintf(stderr, "dlopen failed: %s\n", dlerror()); return 1; } void (*test_func)(); test_func = (void (*)())dlsym(handle, "test_func"); if (test_func == NULL) { fprintf(stderr, "dlsym failed: %s\n", dlerror()); dlclose(handle); return 1; } test_func(); dlclose(handle); return 0; } ``` 在上述代码中,我们使用 dlopen() 函数加载动态链接库,并使用 dlsym() 函数获取其中的函数指针。然后,我们调用动态链接库中的函数,完成相应的操作。在程序结束时,动态链接器会自动调用 `_fini` 函数来清理动态链接库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值