Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyStringv2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class MyString { char* str; public: MyString(const char*); MyString(const MyString&); ~MyString(); MyString& operator+=(const MyString&); MyString& operator+=(const char*); friend ostream& operator<<(ostream&, const MyString&); }; MyString::MyString(const char* s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s) + 1))); strcpy(this->str, s); } MyString::MyString(const MyString& s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s.str) + 1))); strcpy(this->str, s.str); } MyString::~MyString() { free(str); } MyString& MyString::operator+=(const MyString& s) { *this += s.str; return *this; } MyString& MyString::operator+=(const char* s) { int newSize = strlen(this->str) + strlen(s) + 1; this->str = static_cast<char*>(realloc(this->str, newSize * sizeof(char))); strcpy(this->str + (strlen(str)), s); return *this; } MyString operator+(const MyString& s1, const MyString& s2) { MyString s = s1; return s += s2; } MyString operator+(const char* s1, const MyString& s2) { MyString s(s1); return s += s2; } MyString operator+(const MyString& s1, const char* s2) { MyString s(s1); return s += s2; } ostream& operator<<(ostream &output, const MyString &s) { output << s.str; return output; } int main() { MyString s1("abc"); MyString s2("def"); MyString s3 = s1 + s2 + s1 +"s"; cout << s1 << " + " << s2 << " = " << s3; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.31 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.46 sec
fork mode
|
history
|
discussion
abc + def = abcdefabcs