Advertisement
Jgug

l4_v13

Dec 23rd, 2012
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <string.h>
  7. #include <fstream>
  8. #include <math.h>
  9. #define SIZE 256
  10.  
  11. using namespace std;
  12.  
  13. class String
  14. {
  15.     char* str1;
  16.     char* str2;
  17. public:
  18.     String();
  19.     ~String();
  20.     String(char*);
  21.     char operator = (char*);
  22.     void print();
  23.     void set (char*);
  24.     char* run();
  25. };
  26.  
  27. String::String()
  28. {
  29.     str1 = new char[SIZE];
  30.     str1[0] = '\0';
  31.     str2 = new char[SIZE];
  32.     str2[0] = '\0';
  33. }
  34.  
  35. String::String(char *inStr)
  36. {
  37.     str1 = new char[strlen(inStr)];
  38.     str1[0] = '\0';
  39.     str2 = new char[strlen(inStr)];
  40.     str2[0] = '\0';
  41. }
  42.  
  43. String::~String()
  44. {
  45.     delete[] str1;
  46.     delete[] str2;
  47. }
  48.  
  49. char* String::run()
  50. {
  51.     char *subStr;
  52.     if (strlen(str1)>12)
  53.     {
  54.         subStr = strchr(str1, ')');
  55.         strcpy(str2, subStr);
  56.  
  57.     }
  58.     else
  59.     {
  60.         strcpy(str2, str1);
  61.     }
  62.     return str2;
  63. };
  64.  
  65. char String::operator = (char* str1)
  66. {
  67.     strcpy(str2,str1);
  68.     return *str2;
  69. }
  70.  
  71. void String::set(char *inStr)
  72. {
  73.     unsigned int i;
  74.     for (i=0; i<strlen(inStr); i++)
  75.     {
  76.         str1[i] = inStr[i];
  77.     }
  78.     str1[i] = '\0';
  79. };
  80.  
  81. void String::print()
  82. {
  83.     cout<<str1<<endl;
  84.     cout<<str2<<endl;
  85. }
  86.  
  87. void main()
  88. {
  89.     char inStr[256];
  90.     cout<<"Input string: "<<endl;
  91.     cin.getline(inStr, 256);
  92.     String str;
  93.     cout<<endl<<"Source & converted strings:"<<endl;
  94.     str.set(inStr);
  95.  
  96.     ofstream file("text.txt",ios::out);
  97.     file<<"sourse string: "<<inStr<<"\n"<<"converted string: "<<str.run()<<"\n";
  98.     file.close();
  99.  
  100.     str.print();
  101.  
  102.     String *s1, *s2;
  103.     s1 = new String();
  104.     s2 = new String();
  105.     s2->set(inStr);
  106.     s1=s2;
  107.     cout<<endl<<"String, copied using the overloading:"<<endl;
  108.     s1->print();
  109.  
  110.     _getch();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement