C++-构造函数-接口

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Ms{
	private:
		char *p;
		int len;
	public:
		Ms(const char* str);
		Ms();
		~Ms();
		void copy(const Ms& r);
		void append(const Ms& r);
		void Show();
		bool compare(const Ms& r);
		void swap(Ms& r);
};

Ms::Ms(const char* str)
{
//	cout << "Ms单参构造函数" << endl;
	len=strlen(str);
	p=(char*)calloc(1,len+1);
	strcpy(p,str);
}

Ms::Ms()
{
//	cout << "Ms无参构造函数" << endl;
	p=nullptr;
	len=0;
}

Ms::~Ms()
{
	if(p!=NULL)
	{
		free(p);
	}
}

void Ms::copy(const Ms& r)//复制
{
	if(p!=NULL)
	{
		free(p);
	}
	len=r.len;
	p=(char*)calloc(1,len+1);
	strcpy(p,r.p);
}

void Ms::append(const Ms& r)//拼接
{
	len=len+r.len;
	char* backup=p;
	p=(char*)calloc(1,len+1);
	strcpy(p,backup);
	strcat(p,r.p);
	free(backup);
}

void Ms::Show()//输出
{
	cout << p << endl;
}

bool Ms::compare(const Ms& r)//比较
{
	return strcmp(p,r.p)==0;
}

void Ms::swap(Ms& r)//交换
{
	char* temp = p;
	p = r.p;
	r.p = temp;
}

int main(int argc,const char** argv){
	Ms str="dio";
	Ms ptr;
	
	ptr.copy("stand");
	ptr.Show();

	ptr.append("power");
	ptr.Show();

	if(ptr.compare(str)==1)
	{
		cout << "str和ptr一样" << endl;
	}else{
		cout << "str和ptr不一样" << endl;
	}

	ptr.swap(str);
	str.Show();
	ptr.Show();

}
cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <fcntl.h>

using namespace std;

class File{
private:
	FILE* fp;
public:
	File(const char* filename);//构造函数
	void Write(const string str);//写入
	string Read(int size);//读取
	~File();//析构函数
};

File::File(const char* filename)
{
	fp=fopen(filename,"w+");
	if(fp==NULL)
	{
		cout << "文件打开失败" << endl;
		return;
	}
}

void File::Write(const string str)
{
	const char* ptr =str.data();
	if(fwrite(ptr,strlen(ptr),1,fp)==0)
	{
		cout << "数据写入失败" << endl;
	}
}

string File::Read(int size)
{
	char ch[size+1]="";
	fseek(fp,0,SEEK_SET);
	if(fread(ch,size,1,fp)==0)
	{
		cout << "数据读取失败" << endl;
	}
	string ptr =ch;
	return ptr;
}

File::~File()
{
	if(fp!=NULL)
	{
		fclose(fp);
		fp=NULL;
	}
}

int main(int argc,const char** argv){
	File f="./1.txt";
	f.Write("WORLD");
	string buf=f.Read(3);
	cout << buf << endl;

}
cpp 复制代码
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
 
enum kinds_mutex
{
	MUTEX_FAST,
	MUTEX_CHECK,
	MUTEX_RECURSION
};
 
class Mutex
{
	private:
		pthread_mutex_t mutex;
		enum kinds_mutex kinds;
	public:
		Mutex();
		void setMutex(enum kinds_mutex kinds);
		~Mutex();
		void lock();
		void unlock();
};
 
 
Mutex::Mutex()
{
	pthread_mutex_init(&mutex,NULL);
}
 
 
void Mutex::setMutex(enum kinds_mutex kinds)
{
	switch(kinds)
	{
		case MUTEX_FAST:
			{
				pthread_mutex_init(&mutex,NULL);
				break;
			}
		case MUTEX_CHECK:
			{
				pthread_mutexattr_t attr;
				pthread_mutexattr_init(&attr);
				pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
				pthread_mutex_init(&mutex,&attr);
				break;
			}
		case MUTEX_RECURSION:
			{
				pthread_mutexattr_t attr;
				pthread_mutexattr_init(&attr);
				pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE_NP);
				pthread_mutex_init(&mutex,&attr);
				break;
			}
 
	}
}
 
 
Mutex::~Mutex()
{
	pthread_mutex_destroy(&mutex);
}
 
 
void Mutex::lock()
{
	pthread_mutex_lock(&mutex);
}
 
 
void Mutex::unlock()
{
	pthread_mutex_unlock(&mutex);
}
 
 
Mutex m1,m2;
 
 
void* pthread_main(void* arg)
{
	while(1)
	{
		m2.lock();
		cout << "线程2"<<endl;
		sleep(1);
		m1.unlock();
	}
}
 
 
int main()
{
	pthread_t id;
	m1.setMutex(MUTEX_FAST);
	m2.setMutex(MUTEX_FAST);
	m2.lock();
	pthread_create(&id,0,pthread_main,NULL);
	pthread_detach(id);
	while(1)
	{
		m1.lock();
		cout<<"线程1"<<endl;
		sleep(1);
		m2.unlock();
	}
 
	return 0;
}
相关推荐
mqiqe1 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim1 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情1 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学1 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484812 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
liangshanbo12154 小时前
虚拟列表深度面试题整理
java·开发语言·前端
ZISHU_9874 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
略略略咯咯4 小时前
C#Unity
开发语言·unity·c#
gugucoding4 小时前
59. 【Java】Spring Boot 入门:第一个 Web 应用
java·开发语言·spring boot
ShineWinsu4 小时前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20