类的设计方法和程序

首先我们需要对类进行函数声明:

array.h

#pragma  once

#include <iostream>
using namespace std;
// 进行类的声明
class Array
{
public:
	Array(int length);
	Array(const Array& obj);       // 构造函数
	~Array();                      // 析构函数

public:
	void setData(int index, int valude);
	int getData(int index);
	int length();

private:
	int m_length;
	int *m_space;
};

 

接下来就是类的框架(对类进行定义)

array.cpp

#include "MyArray.h"
// 类的框架

//int m_length;
//char *m_space;
Array::Array(int length)
{
	if (length < 0)
	{
		length = 0; //
	}

	m_length = length;
	m_space = new int[m_length];
}

//Array a2 = a1;
Array::Array(const Array& obj)
{
	this->m_length = obj.m_length;
	this->m_space = new int[this->m_length]; //分配内存空间

	for (int i=0; i<m_length; i++) //数组元素复制
	{
		this->m_space[i] = obj.m_space[i];
	}
}
Array::~Array()
{
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_length = -1;
	}
}

//a1.setData(i, i);
void Array::setData(int index, int valude)
{
	m_space[index] = valude;
}
int Array::getData(int index)
{
	return m_space[index];
}
int Array::length()
{
	return m_length;
}

接下来对类的测试:

array_test.cpp


#include <iostream>
using namespace std;
#include "MyArray.h"

//类的框架设计完毕
//类的测试案例
void main()
{
	
	Array  a1(10); //定义这个类的时候,会调用类的有参构造函数

	for (int i=0; i<a1.length(); i++)
	{
		a1.setData(i, i);
	}

	cout<<"\n打印数组a1: ";
	for (int i=0; i<a1.length(); i++)
	{
		cout<<a1.getData(i)<<" ";
	}
	cout<<endl;

	
	Array a2 = a1;
	cout<<"\n打印数组a2: ";
	for (int i=0; i<a2.length(); i++)
	{
		cout<<a2.getData(i)<<" ";
	}
	cout<<endl;
	

	cout<<"hello..."<<endl;
	system("pause");
	return ;
}

 

MyString类的使用技巧与技术:

1、函数的声明:MyString.h


#include <iostream>
using namespace std;

//c中没有字符串 字符串类(c风格的字符串)
//空串 ""
class  MyString
{
	friend ostream& operator<<(ostream &out, MyString &s);
	friend istream& operator>>(istream &in, MyString &s);
public:
	MyString(int len = 0);
	MyString(const char *p);
	MyString(const MyString& s);
	~MyString();

public: //重载=号操作符
	MyString& operator=(const char *p);
	MyString& operator=(const MyString &s);
	char& operator[] (int index);

public: //重载 == !== 
	bool operator==(const char *p) const;
	bool operator==(const MyString& s) const;
	bool operator!=(const char *p) const;
	bool operator!=(const MyString& s) const;

public:
	int operator<(const char *p);
	int operator>(const char *p);
	int operator<(const MyString& s);
	int operator>(const MyString& s);

	//把类的指针 露出来  
public:
	char *c_str()
	{
		return m_p;
	}
	const char *c_str2()
	{
		return m_p;
	}
	int length()
	{
		return m_len;
	}
private:
	int		m_len;
	char	 *m_p;    //指针

};

 

2、函数的定义:MyString.cpp

#define _CRT_SECURE_NO_WARNINGS

#include "MyString.h"

ostream& operator<<(ostream &out, MyString &s)
{
	out<<s.m_p;
	return out;
}
istream& operator>>(istream &in, MyString &s)
{
	cin>>s.m_p;
	return in;
}

MyString::MyString(int len)       // 构造函数
{
	if (len == 0)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
		m_len = len;
		m_p = new char[m_len + 1];
		memset(m_p, 0, m_len);
	}

}

MyString::MyString(const char *p)    // 有可能传来一个空指针
{
	if (p == NULL)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
}

//拷贝构造函数
//MyString s3 = s2;
MyString::MyString(const MyString& s)
{
	m_len = s.m_len;    // 根据 s 分配内存 
	m_p = new char[m_len + 1];    // 根据内存大小分配内存的大小 

	strcpy(m_p, s.m_p);
}

MyString::~MyString()
{
	if (m_p != NULL)        // 如果内存空间不是null的话
	{
		delete [] m_p;
		m_p = NULL;
		m_len = 0;
	}
}

 s4 = "s2222";
MyString& MyString::operator=(const char *p)
{
	//1 旧内存释放掉
	if (m_p != NULL)
	{
		delete [] m_p;
		m_len = 0;
	}
	//2 根据p分配内存
	if (p == NULL)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
	return *this;
}

// s4 = s2;
MyString& MyString::operator=(const MyString &s)
{
	//1 旧内存释放掉
	if (m_p != NULL)
	{
		delete [] m_p;
		m_len = 0;
	}
	//2 根据s分配内存
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);

	return *this;
}

char& MyString::operator[] (int index)
{
	return m_p[index];
}

//if (s2 == "s222222")
bool MyString::operator==(const char *p) const
{
	if (p == NULL)
	{
		if (m_len == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if (m_len == strlen(p))
		{
			return !strcmp(m_p, p);
		}
		else
		{
			return false;
		}
	}
}

bool MyString::operator!=(const char *p) const
{
	return !(*this == p);
}


bool MyString::operator==(const MyString& s)  const
{
	if (m_len != s.m_len)
	{
		return false;
	}
	return !strcmp(m_p, s.m_p);
}

bool MyString::operator!=(const MyString& s) const
{
	return !(*this == s);
}


//if (s3 < "bbbb")
int MyString::operator<(const char *p)
{
	return strcmp(this->m_p , p);
}

int MyString::operator>(const char *p)
{
	return strcmp(p, this->m_p);
}

int MyString::operator<(const MyString& s)
{
	return strcmp(this->m_p , s.m_p);
}

int MyString::operator>(const MyString& s)
{
	return  strcmp(s.m_p, m_p);
}



 

3、主函数的调用:MyString_Test.cpp

 
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
using namespace std;
#include "MyString.h"

void main01()
{
	MyString s1;
	MyString s2("s2");
	MyString s2_2 = NULL;
	MyString s3 = s2;
	MyString s4 = "s4444444444";

	//测试运算符重载 和 重载[]
	//=

	s4 = s2;
	s4 = "s2222";
	s4[1] = '4';
	printf("%c", s4[1]); 

	cout<<s4 <<endl;
	//ostream& operator<<(ostream &out, MyString &s)
	
	//char& operator[] (int index)
	//MyString& operator=(const char *p);
	//MyString& operator=(const MyString &s);

	cout<<"hello..."<<endl;
	system("pause");
	return ;
}

void main02()
{
	MyString s1;
	MyString s2("s2");     // 这个时候传来的是一个字符串

	MyString s3 = s2;

	if (s2 == "aa")
	{
		printf("相等");
	}
	else
	{
		printf("不相等");
	}

	if (s3 == s2)
	{
		printf("相等");
	}
	else
	{
		printf("不相等");
	}
	
}
void main03()
{
	MyString s1;
	MyString s2("s2");

	MyString s3 = s2;
	s3 = "aaa";

	int tag = (s3 < "bbbb");
	if (tag < 0 )
	{
		printf("s3 小于 bbbb");
	}
	else
	{
		printf("s3 大于 bbbb");
	}

	MyString s4 = "aaaaffff";
	strcpy(s4.c_str(), "aa111"); //MFC
	cout<<s4<<endl;
}

void main011()
{
	MyString s1(128);
	cout<<"\n请输入字符串(回车结束)";
	cin>>s1;

	cout<<s1;
	system("pause");
	
}

void main()
{
	MyString s1(128);
	cout<<"\n请输入字符串(回车结束)";
	cin>>s1;

	cout<<s1<<endl;
	
	system("pause");

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值