tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,19 @@
class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig.duplicate();
writeln(orig);
writeln(copy);
}

View file

@ -0,0 +1,46 @@
class T {
this(T t = null) {} // Constructor that will be used for copying.
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T(this); }
bool custom(char c) { return false; }
}
class S : T {
char[] str;
this(S s = null) {
super(s);
if (s is null)
str = ['1', '2', '3']; // All newly created will get that.
else
str = s.str.dup; // Do the deep-copy.
}
override string toString() {
return "I'm the instance of S p: " ~ cast(string)str;
}
override T duplicate() { return new S(this); }
// Additional procedure, just to test deep-copy.
override bool custom(char c) {
if (str !is null)
str[0] = c;
return str is null;
}
}
void main () {
import std.stdio;
T orig = new S;
orig.custom('X');
T copy = orig.duplicate();
orig.custom('Y');
writeln(orig);
writeln(copy); // Should have 'X' at the beginning.
}