您的位置:首页 > 编程语言 > C语言/C++

C++学习笔记:MyString类的重载 练习

2016-02-23 19:19 363 查看
首先测试.cpp

// MyString类案例.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"MyString.h"
#pragma warning(disable:4996)

int _tmain(int argc, _TCHAR* argv[])
{
MyString str1("lifeng");//MyString str1(const char* p);
MyString str2(str1);//MyString str2(MyString &str1);
MyString str3 = str1;
MyString str4 = "nihao";
str4[0] = 'w';//char& operator[](int index);
if (str1 == str4){//bool operator==(MyString &str4);
cout << "两者相等" << endl;
}
else if(str1!=str4){//bool operator!=(MySring &str);
cout << "两者不等" << endl;
}
if (str1 < str4){//bool operator<(MyString &str);
cout << "小于" << endl;
}
else if (str1>str4){//bool operator>(MyString &str);
cout << "大于" << endl;
}
MyString s5 = "abcd";
//strcpy((LPCTSTR)s5.m_p, "ad");//MFC
strcpy((char*)s5.c_str(), "adc");
MyString s(12);
cin >> s;//isstream &operator>>(istream &cin,MyString &s);
cout << s << endl;
return 0;
}
头文件,MyString.h

#include"stdafx.h"
#include <iostream>

using namespace std;
class MyString{
public:
MyString(int len=0);
MyString(const char*p);
MyString(MyString &str);
char& operator[](int index);
bool operator==(MyString &str);
bool operator!=(MyString &str);
int operator<(MyString &str) const;
int operator<(const char*p) const;
int operator>(MyString &str) const;
int operator>(const char *p) const;
//把类的指针属性 露出来
const char *c_str();
friend istream &operator>>(istream &in, MyString &s);//全局函数
friend ostream &operator<<(ostream &out, MyString &s);
private:
int m_len;
char *m_p;
};


MyString.cpp

#include"stdafx.h"
#include"MyString.h"
#pragma warning(disable:4996)

istream &operator>>(istream &in, MyString &s){
cin >> s.m_p;
return in;
}
ostream &operator<<(ostream &out, MyString &s){
cout<<s.m_p;
return out;
}
MyString::MyString(int len)
{
if (len == 0){
m_len = 0;
m_p = new char[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 (m_p != NULL){
delete[]m_p;
m_p = NULL;
m_len = 0;
}
m_len = strlen(p);
if (m_len == 0){
m_p = new char[1];
strcpy(m_p, "");
}
else{
m_p = new char[m_len + 1];
strcpy(m_p, p);
}
}
MyString::MyString(MyString &str){
if (m_p != NULL){
delete[]m_p;
m_p = NULL;
m_len = 0;
}
m_len = str.m_len;
m_p = new char[m_len + 1];
strcpy(m_p,str.m_p);
}
char& MyString::operator[](int index){
return m_p[index];
}
bool MyString::operator==(MyString &str){
if (str.m_len==m_len){
for (int i = 0; i < m_len; i++){
if (str[i] != m_p[i]){
return false;
}
}
return true;
}
else{
return false;
}
}
bool MyString::operator!=(MyString &str){
return !(*this == str);
}
int MyString::operator<(MyString &str) const{
return strcmp(m_p, str.m_p);
}
int MyString::operator<(const char*p) const{
return strcmp(this->m_p,p);
}
int MyString::operator>(MyString &str) const{
return strcmp(this->m_p,str.m_p);
}
int MyString::operator>(const char *p) const{
return strcmp(this->m_p, p);
}
const char *MyString::c_str(){
return m_p;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: