Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab_5.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cstdio>
- using namespace std;
- template <int max_size, class T> class my_string {
- public:
- my_string () {
- this->size = 0;
- }
- my_string (const char* source_string) {
- this->size = strlen(source_string);
- for (int i = 0; i < strlen(source_string); i++) {
- this->text[i] = source_string[i];
- }
- this->text[strlen(source_string)] = '\0';
- }
- ~my_string () {
- delete [] this->text;
- }
- T get_length () const {
- return size;
- }
- my_string (const string& source_string) {
- this->size = source_string->size;
- strcpy(this, source_string->text);
- }
- char get_char (int index) const {
- return text[index];
- }
- void set_char (char temp, int position) {
- this->text[position] = temp;
- this->size++;
- }
- void read_string () {
- int i = 0;
- char temp;
- do {
- temp = getchar();
- if (i < max_size) {
- this->text[i] = temp;
- i++;
- }
- }
- while (temp != '\n');
- text[i - 1] = '\0';
- }
- my_string <max_size, T> operator+(const my_string& s) {
- my_string <max_size, T> res;
- int count = 0;
- while (this->get_char(count) != '\0') {
- res.set_char(this->get_char(count), count);
- count++;
- }
- for(int i = 0; count < max_size; i++){
- if (s.get_char(i) == '\0') { res.set_char('\0', count); break; }
- res.set_char(s.get_char(i), count);
- count++;
- }
- return res;
- }
- my_string& operator=(const my_string& s){
- for (int i = 0; i < size; i++){
- this->set_char(i, s.get_char(i));
- }
- return *this;
- }
- void print() {
- cout << text << endl;
- }
- private:
- char text[max_size];
- T size;
- };
- int _tmain(int argc, _TCHAR* argv[]) {
- char* X = "some text";
- char* Y = ", new text!";
- my_string<64, int> *S1 = new my_string<64, int>(X);
- my_string<64, int> *S2 = new my_string<64, int>(Y);
- my_string<64, int> *S3 = new my_string<64, int>();
- *S3 = *S1 + *S2;
- //S1->print();
- //S2->print();
- //S3->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement