项目3-警察和厨师(1)

*Copyright (c) 2014,烟台大学计算机学院

*All right reserved.

*文件名称:test.cpp

*作    者:韩双志

*完成日期:2016年5月7日

*版本号:v1.0

*

*问题描述:完成各个成员函数,增加构造函数,输出相关信息

*输入描述:无

*输出描述:输出相关信息

/*

#include <iostream>

using namespace std;
class Person
{
public:
    Person(int a,string nam):age(a),name(nam){}
    void action();
private:
    int age;
    string name;
};
void Person::action()
{
    cout<<"姓名:"<<name<<endl;
    cout<<"年龄:"<<age<<endl;
}
class Point:public Person
{
public:
    Point(int a,string nam,int b):Person(a,nam),level(b){}
   void arrest(Person);
private:
    int level;
};
void Point::arrest(Person a)
{
    a.action();
    cout<<"级别:"<<level<<endl;
}
class Cook:public Person
{
public:
    Cook(int a,string nam,double m):Person(a,nam),salary(m){}
    string getCake(int);
private:
    double salary;
};
string Cook::getCake(int a)
{
    Person::action();
    cout<<"薪水"<<a<<endl;
}
int main()
{
    Person b(26,"zhangsan");
    Point c(20,"lisi",3);
    Cook a(26,"hahah",6500);
    b.action();
    c.arrest(b);
    a.getCake(6500);
    return 0;

}

*/

运行结果:


知识点结构

   类的继承

学习心得

    大体为了解了类的继承

在 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、付费专栏及课程。

余额充值