第二次作业。
This commit is contained in:
47
OOP/02/Optional02.cpp
Normal file
47
OOP/02/Optional02.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user