Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- enum { STRLEN_MAX = 255 };
- class String {
- public:
- char s[STRLEN_MAX];
- String() {
- s[0] = 0;
- }
- String(const char *x) {
- s[0] = 0;
- int i = 1;
- while (*x && i < STRLEN_MAX) {
- s[i] = *x;
- ++x;
- ++i;
- }
- s[0] = i - 1;
- }
- char &operator [](int i) {
- if (i > STRLEN_MAX) {
- std::cerr << "Index out of range\n";
- return s[0];
- }
- return s[i];
- }
- char operator [](int i) const {
- if (i > s[0]) {
- std::cerr << "Index out of range\n";
- return s[0];
- }
- return s[i];
- }
- int get_len() const {
- return s[0];
- }
- String(const String &other) {
- for (int i = 0; i <= other.get_len(); ++i) {
- s[i] = other[i];
- }
- }
- String operator +(const String &other) {
- String res(*this);
- int i = 1;
- while (res[0] + 1 < STRLEN_MAX && i <= other.get_len()) {
- res[res[0] + 1] = other[i];
- ++i;
- ++res[0];
- }
- return res;
- }
- };
- std::ostream& operator <<(std::ostream &out, const String &S) {
- for (char i = 1; i <= S.get_len(); ++i) {
- out << S[i];
- }
- return out;
- }
- int main() {
- String X("abcdef");
- String Y("ababa");
- String T = X;
- std::cout << T << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement