11.19c++面向对象+单例模式

编写如下类: class File{ FILE* fp }; 1:构造函数,打开一个指定的文件 2:write函数 向文件中写入数据 3:read函数,从文件中读取数据,以string类型返回

代码实现:

cpp 复制代码
#include <iostream>

using namespace std;

class myfile{
    FILE *fp;
public:
    myfile(const char *path,const char *type);//通过fp绑定一个构造函数
    ~myfile();//通过fp绑定一个析构函数
    int write(char *buf,int signal_size,int terms);//申明一个write函数,向文件中写入数据
    int read(char *buf,int signal_size,int terms);//申请一个read函数,从文件中读取数据,返回string类型
};
myfile::myfile(const char *path,const char *type){
    fp=fopen(path,type);
    if(fp==NULL){
        perror("fopen");
    }
}
myfile::~myfile(){
    fclose(fp);
}
int myfile::write(char *buf,int signal_size,int terms){
    int res=fwrite((char *)buf,signal_size,terms,fp);
    return res;
}
int myfile::read(char *buf,int signal_size, int terms){
    int res=fread((char *)buf,signal_size,terms,fp);
    return res;
}
int main()
{
    myfile file_src={"C:\\Users\\pzctj\\Documents\\81_C++\\day2\\myfile\\main.cpp","r"};
    myfile file_dest={"myfile.txt","w"};
    char str[1024]={0};
   // string buf=str;
    while(1){
        //buf.clear();
       int res=file_src.read((char *)str,1,1024);
        if(res==0){
            cout << "文件读取结束" << endl;
            break;
        }
        file_dest.write((char *)str,1,res);
    }
    return 0;
}

单例模式实现:

懒汉模式:
cpp 复制代码
#include <iostream>

using namespace std;

class signalTon{
private:
    class Mutex{
         pthread_mutex_t mutex;
    public:
        Mutex(){
            pthread_mutex_init(&mutex,NULL);
            cout << "无参互斥锁构造函数" << endl;
        }
        void lock(){
            pthread_mutex_lock(&mutex);
        }
        void unlock(){
            pthread_mutex_unlock(&mutex);
        }
    };
    signalTon(){
        cout << "无参signalTon构造函数" << endl;
    }
    static Mutex mutex;
public:
    signalTon(const signalTon& r)=delete ;//删除自动分配的拷贝构造函数
    //signalTon()=default;//确保必须分配构造函数
    //公开接口,用来调用构造函数,构建唯一对象或者返回唯一对象
    static signalTon* getInstance(){
        mutex.lock();
        static signalTon* instance=NULL;
        if(instance==NULL){
            sleep(1);//延时1秒,对于多线程懒汉模式可能if判断就不起作用,因此引入互斥锁
            instance=new signalTon;//堆区开辟空间并调用构造函数为对象初始化
        }
        mutex.unlock();
        return instance;
    }
};
signalTon::Mutex signalTon::mutex;//静态成员属性mutex开辟空间
void* task(void *arg){
    signalTon* s1=signalTon::getInstance();
    cout << s1 << endl;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,NULL,task,NULL);
    pthread_detach(tid);
    signalTon* s1=signalTon::getInstance();
    cout << s1 << endl;
    return 0;
}
饿汉模式:
cpp 复制代码
#include <iostream>

using namespace std;

class signalTon{
private:
    signalTon(){
        cout << "无参signalTon构造函数" << endl;
    }
    static signalTon* instance;
public:
    signalTon(const signalTon& r)=delete ;//删除自动分配的拷贝构造函数
    //signalTon()=default;//确保必须分配构造函数
    //公开接口,用来调用构造函数,构建唯一对象或者返回唯一对象
    static signalTon* getInstance(){
        return instance;
    }
};
signalTon* signalTon::instance=new signalTon;
void* task(void *arg){
    signalTon* s1=signalTon::getInstance();
    cout << s1 << endl;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,NULL,task,NULL);
    pthread_detach(tid);
    signalTon* s1=signalTon::getInstance();
    cout << s1 << endl;
    return 0;
}
相关推荐
橙小花10 分钟前
C语言:指针、变量指针与指针变量、数组指针与指针数组
c语言·开发语言
Cyanto29 分钟前
MyBatis-Plus高效开发实战
java·开发语言·数据库
艾莉丝努力练剑1 小时前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
wjs20241 小时前
XML 语法详解
开发语言
Jackilina_Stone2 小时前
【faiss】用于高效相似性搜索和聚类的C++库 | 源码详解与编译安装
android·linux·c++·编译·faiss
双叶8362 小时前
(Python)文件储存的认识,文件路径(文件储存基础教程)(Windows系统文件路径)(基础教程)
开发语言·windows·python
喜欢吃燃面2 小时前
C++:list(1)list的使用
开发语言·c++·学习
枫昕柚2 小时前
python
开发语言·python
Dxy12393102162 小时前
Python Requests-HTML库详解:从入门到实战
开发语言·python·html
啊阿狸不会拉杆3 小时前
《Java 程序设计》第 7 章 - 继承与多态
java·开发语言·jvm·算法·intellij-idea