#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;
class String
{
private:
char *s;
public:
String()
{
s=new char[];
}
String(const char *t)
{
s=new char[];
strcpy(s,t);
}
String(const String &t)
{
s=new char[];
strcpy(s,t.s);
}
~String()
{
delete []s;
}
String & operator=(const char *t)
{
strcpy(this->s,t);
return *this;
}
String & operator=(const String &t)
{
this->s=t.s;
return *this;
}
String operator+(const char *t)
{
char *t1=nullptr;
strcpy(t1,this->s);
strcat(t1,t);
return t1;
}
String operator+(const String &t)
{
char *t1 = nullptr;
strcpy(t1,this->s);
strcat(t1,t.s);
return t1;
}
String & operator+=(const char *t)
{
strcat(this->s,t);
return *this;
}
String & operator+=(const String &t)
{
strcat(this->s,t.s);
return *this;
}
friend istream & operator>>(istream & is, String &t1)
{
is>>t1.s;
return is;
}
friend ostream & operator<<(ostream & os, String &t2)
{
os<<t2.s;
return os;
}
friend bool operator==(const String &t1, const char *t2)
{
if(strcmp(t1.s,t2))
return ;
else
return ;
}
friend bool operator==(const String &t1, const String &t2)
{
if(strcmp(t1.s,t2.s))
return ;
else
return ;
}
friend bool operator!=(const String &t1, const char *t2)
{
return strcmp(t1.s,t2);
}
friend bool operator!=(const String &t1, const String &t2)
{
return strcmp(t1.s,t2.s);
}
};
int main()
{
String s;
s += "hello";
cout<<s<<endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return ;
}

C++练习 | 运算符重载练习(字符串相关)的更多相关文章

  1. C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)

    运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...

  2. 编码实现字符串类CNString实现运算符重载

    题目描述: 编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下"="运算符."+"运算."[]& ...

  3. C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载

    编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...

  4. C#基础加强(6)之引用相等与运算符重载

    引用相等 介绍 在 C# 中可以通过 object.ReferenceEquals(obj1, obj2) 方法来判断两个变量引用的是不是同一个地址,如果是,那么就是引用相等. 引用相等是针对引用类型 ...

  5. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  6. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  7. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  8. C# 类型运算符重载在类继承中的调用测试

    这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest ...

  9. C++运算符重载——重载特殊运算符

    1.重载赋值运算符= 赋值运算符用于同类对象间的相互赋值.赋值运算符只能被重载为类的非静态成员函数,不能重载为友元函数和普通函数. 对于用户自定义的类而言,如果没有重载赋值运算符,那么C++编译器会为 ...

随机推荐

  1. LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树

    中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...

  2. BigInteger方法总结

    BigInteger 可以用来解决数据的溢出问题. 下面我总结几种关于BigInteger的常用用法: 1.probablePrime和nextprobablePrime.(判断质数,并返回) Big ...

  3. java笔记--正则表达式的运用(包括电话,邮箱验证等)

    正则表达式 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877402.html "谢谢-- 正则表达式符号:" ...

  4. leetcode summary-section II

    151 Reverse Words in a String class Solution { public: void reverseWords(string &s) { string res ...

  5. python字典的排序

    # -*- coding:UTF-8 -*- def dict_sort(): # 按照value的值从大到小的顺序进行排序 dic = {'a': 31, 'bc': 5, 'c': 3, 'asd ...

  6. layui实现checkbox的目录树tree

    layui.use([ 'tree' ], function() {$ = layui.jquery;form = layui.form;//获取节点数据getTreeData();}); funct ...

  7. [翻译] CNPGridMenu

    CNPGridMenu CNPGridMenu is a Mailbox style grid menu with a blurred background for iOS 7 & iOS 8 ...

  8. August 31st 2017 Week 35th Thursday

    Whatever happened in the past is gone, the best is always yet to come. 无论过去发生什么,最好的永远尚未到来. Correct j ...

  9. December 07th 2016 Week 50th Wednesday

    Life is a flower, and love is the honey of the flower. 人生是花儿,而爱情就是花的蜜. My life is not as beautiful a ...

  10. 在python中逐行读取大文件

    在我们日常工作中,难免会有处理日志文件的时候,当文件小的时候,基本不用当心什么,直接用file.read()或readlines()就可以了,但是如果是将一个10G大小的日志文件读取,即文件大于内存的 ...