47 lines
728 B
C++
47 lines
728 B
C++
#include <iostream>
|
|
#include <stdlib.h>
|
|
|
|
class Strings {
|
|
public:
|
|
Strings(char *s);
|
|
~Strings();
|
|
void Print();
|
|
void Set(char *s);
|
|
|
|
private:
|
|
int length;
|
|
char *str;
|
|
};
|
|
|
|
Strings::Strings(char *s) {
|
|
length = strlen(s);
|
|
str = (char *)malloc(length + 1);
|
|
strcpy(str, s);
|
|
}
|
|
|
|
Strings::~Strings() {
|
|
free(str);
|
|
}
|
|
|
|
void Strings::Print() {
|
|
std::cout << str << std::endl;
|
|
}
|
|
|
|
void Strings::Set(char *s) {
|
|
free(str);
|
|
length = strlen(s);
|
|
str = (char *)malloc(length + 1);
|
|
strcpy(str, s);
|
|
}
|
|
|
|
int main() {
|
|
Strings* str1 = new Strings("didig");
|
|
Strings* str2 = new Strings("dirir");
|
|
|
|
str1->Print();
|
|
str2->Print();
|
|
str1->Set("ifirk");
|
|
str1->Print();
|
|
|
|
return 0;
|
|
} |