c++运算符重载总结
来源:互联网 发布:python安装不了 编辑:程序博客网 时间:2024/05/10 15:06
今天学了运算符的重载,对于c++,相比C语言而言语法又多且杂,难背又难记,最近真有点懈怠......还是要加油
#ifndef _STRING_H_#define _STRING_H_#include <iostream>using namespace std;class String{public: //static int MAX_LEN; //friend class StringTool; //friend void print(const String &s1); String(); String(char *str); ~String(); String(const String& other); void myStrcat(); void Display() const; void setMAXLEN(int maxLen); int getMAXLEN(); String& operator=(const String& other); String& operator=(char *str); char& operator[](unsigned int index); const char& operator[](unsigned int index) const; String& operator+=(const String &s); friend String operator+(const String& s1, const String& s2); friend ostream& operator<<(ostream &out, const String &s); friend istream& operator>>(istream &in,String& s); operator char *();private: char *str_; static const int MAX_LEN = 1024; //String(const String& other);//绂佹鎷疯礉};#endif
#include <iostream>#include <string.h>#include "String.h"using namespace std;//int String::MAX_LEN = 1024;String::String(){ str_ = new char('\0'); cout << "default constrcutor String" << endl;}String::String(char *str){ cout << "construct String" << endl;//str_ = str; int len = strlen(str) + 1; str_ = new char[len]; memset(str_,0,len); strcpy(str_,str);}String::String(const String& other){ int len = strlen(other.str_) + 1; str_ = new char[len]; memset(str_,0,len); strcpy(str_,other.str_);}String::~String(){ cout << "destroy String" << endl; delete [] str_;}String::operator char*(){ return str_;}String& String::operator=(const String& other){ if(this == &other) { return *this; } int len = strlen(other.str_) + 1; delete [] str_; str_ = new char[len]; memset(str_,0,len); strcpy(str_,other.str_); return *this;}String& String::operator=(char *str){ cout << "operator char *str" << endl; int len = strlen(str) + 1; delete [] str_; str_ = new char[len]; memset(str_,0,len); strcpy(str_,str); return *this;}char& String::operator[](unsigned int index){ cout << "no const []" << endl; return const_cast<char&>(static_cast<const String&>(*this)[index]);}const char& String::operator[](unsigned int index) const{ cout << "const []" << endl; return str_[index];}String operator+(const String& s1, const String& s2){ /* int len = strlen(s1.str_) + strlen(s2.str_) + 1; char *newptr= new char[len]; memset(newptr,0,len); strcpy(newptr,s1.str_); strcat(newptr,s2.str_); String temp(newptr); */ String temp(s1); temp += s2; return temp;}String& String::operator+=(const String &s){ int len = strlen(str_) + strlen(s.str_) + 1; char *newptr = new char[len]; strcpy(newptr,str_); strcat(newptr,s.str_); delete [] str_; str_ = new char[len]; strcpy(str_,newptr); return *this;}ostream& operator<<(ostream &out, const String& s){ out << s.str_; return out;}istream& operator>>(istream &in, String& s){ char buffer[1024]; in >> buffer; int len = strlen(buffer) + 1; delete [] s.str_; s.str_ = new char[len]; strcpy(s.str_,buffer); return in;}void String::Display() const{ cout << str_ << endl;}void String::setMAXLEN(int maxLen){}int String::getMAXLEN(){ return MAX_LEN;}
#include <iostream>#include "String.h"#include "Stringtool.h"using namespace std;int main(){ #if 0 String s1; s1 = "hello"; cout << s1[1] << endl; s1[1] = 'E'; s1.Display(); String s2("world"); cout << s2[1] << endl; //s[2] = 'L'; String s3("kkk"); //s3 = s1 + s2; s3+=s1;//s3 = s3 + s1; s3.Display(); #endif String s("hello world"); cout << s << endl; cin >> s; cout << s << endl; char *ptr = static_cast<char *>(s); cout << ptr << endl; return 0;}
0 0
- c 运算符重载总结
- C++_运算符重载 总结
- C++_运算符重载总结
- C++_运算符重载 总结
- C++_运算符重载 总结
- 运算符重载总结
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- RxJava学习总结3
- 死锁的四个必要条件
- 1072. Gas Station (30) -- Dijkstra
- adb通过WiFi对手机进行调试
- B. Mahmoud and a Triangle
- c++运算符重载总结
- django入门与学习-django的安装与测试-1
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现
- 排序3:插入排序(普通插入排序 + 折半插入排序 + 链表插入排序 + 希尔排序)
- springmvc中messageConverter用法
- hdu2544 最短路
- 字符串替换
- 专业渲染户型图|制作手法概述
- leecode 解题总结:57. Insert Interval