Код: Выделить всё
#include "stdafx.h"
#include "conio.h"
#include <string.h>
using namespace std;
int a;
class String {
private:
char* ptr;
public:
String(){
cout << "Vizivaetsja konstructor ()" << endl;
ptr = new char[1];
ptr[0] = '\0';
}
String(char* s){
cout << "Vizivaetsja konstruction char*" << endl;
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
String(const String& src){
cout << "Vizivaetsja konstruction kopirovanija" << endl;
int n = strlen(src.ptr);
ptr = new char[n + 1];
strcpy(ptr, src.ptr);
}
~String(){
delete [] ptr;
cout << "Vizivaetsja destruction " << endl;
}
String& operator=(const String& src){
cout << "Vizivaetsja operator prisvaivanija" << endl;
cpy(src.ptr);
return *this;
}
String operator+(char* s){
cout << "Vizivaetsja operator+" << endl;
String new_str(ptr);
new_str.cat(s);
return new_str;
}
operator char*() {
cout << "vizvalsja operator preobrazovanija char*" << endl;
return ptr;
}
void cat(char* s){
int n = strlen(ptr) + strlen(s);
char* p1 = new char[n + 1];
strcpy(p1, ptr);
strcat(p1, s);
delete [] ptr;
ptr = p1;
}
void cpy(char* s){
delete [] ptr;
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
};
int main() {
String a, b, c;
a = "I "; //1
b = "am "; //2
c = "so "; //3
String d;
d = a + b + c; //4-7
cout << d << endl; //8
return 0;
}