C++学习笔记:MyString类的重载 练习
2016-02-23 19:19
363 查看
首先测试.cpp
MyString.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; }
运行结果:
相关文章推荐
- 结构体指针和结构体嵌套
- 学习C语言基本思路与参考书籍
- C++构造函数调用规则
- 设计模式之命令模式
- c++中new与delete
- 在MDK中使用C++源格式做printf串口输出时跳死 调试时发现卡在启动代码中
- C++对象模型浅析
- C++基础::便捷函数
- 深入分析C++中声明与定义的区别
- C++基础::shared_ptr 编程细节(二)
- C++及C中的 string char指针及char数组
- C语言的原码,反码和补码
- C语言中的位运算
- C语言字符串与字符数组
- C语言 变量的作用域和生命周期
- 【转】嵌入式软件:C语言编码规范
- C++ string类的实现
- C++,软件结构中.Cpp和.h的关系
- C语言的枚举类型
- C语言的联合体的使用总结