On inheritance and polymorphism in cats and dogs (and other beasts too)

Derix via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 21 02:42:36 PST 2014


Thank you kindly for this detailed response. Your directions bore
frution, and I now have cats that breed cats (and not dogs).
However I can't get rid of the following warning on compilation :

|...
| void mixGenetics(Cat mother,Cat father) {...}
|...

> Warning: Beast.Beast.mixGenetics(Beast mother, Beast father) 
> hidden by Cat is deprecated. Use 'alias Beast.mixGenetics 
> mixGenetics;' to introduce base class overload set.

Well, I know every of those words, but I can't make sense out of
the sentence. Hidden function ? Alias function ?

I tried some variants, which yields errors :

|...
| override void mixGenetics(Cat mother,Cat father) {
|...

> Error: function CatsAndDogs.Cat.mixGenetics does not override 
> any function, did you mean to override 
> 'Beast.Beast.mixGenetics'?

Why, yes, this is precisely what I'm trying to do, stupid
compiler.


|...(in class Cat)
|...
| void mixGenetics(Beast mother,Beast father) {
|   super.mixGenetics(mother,father);
|   this.pickiness = (mother.pickiness + father.pickiness) / 2 ;
| }
|...

> Error: no property 'pickiness' for type 'Beast.Beast'

I'm not that surprised here, I tried this one just in case, but
this leads me to a part of your previous answer I did not quite
get :
>You can cast Beasts back into Cats with cast(Cat) beast. Be sure 
>to check for null - if beast is a dog, that cast will return 
>null.

I do get the idea, but wasn't able to implement it, and I find
the online litterature to be quite terse on the topic of casting
user-defined types.
http://dlang.org/operatoroverloading#cast

Does that mean that I must define a cast operator in my classes ?

Thanks again.

-----------------------------------------------------------------------
For the record, I include below the code that works despite the
warning
-----------------------------------------------------------------------

module Beast;
import std.conv;

class Beast{
	string name;
	int generation;

	this(){}

	this(string s)	{
		name=s;
		generation=0;
	}

	//protected
	void mixGenetics(Beast mother, Beast father) {
		this.name=mother.name~father.name;
		this.generation=father.generation+1;
	}

	override
	string toString() {
		return "*"~this.classinfo.name~"* Name : "~this.name ~"; Gen. :
"~ to!string(this.generation);
	}
}

class Cat:Beast
{
	int pickiness;
	this(){};
	this(string name, int pickiness){
		super(name);
		this.pickiness=pickiness;
	}

	this(string name, string pickiness){
		super(name);
		this.pickiness=to!int(pickiness);
	}

	protected void mixGenetics(Cat mother,Cat father) {
		super.mixGenetics(mother,father);
		this.pickiness = (mother.pickiness + father.pickiness) / 2 ;
	}

	override string toString() {	return super.toString ~" ; Pick. :
"~to!string(this.pickiness);}

	Cat breed(Cat father){
		Cat kitten=new Cat();
		kitten.mixGenetics(this, father);
		return kitten;
	}
}

class Dog:Beast
{
	int fetchiness;

	this(){};

	this(string name, int fetch)	{
		super(name);
		fetchiness=fetch;
	}

	this(string name, string fetch){this(name, to!int(fetch));}

	override string toString() {	return super.toString ~" ; Fetch. :
"~to!string(this.fetchiness);}

	protected void mixGenetics(Dog mother,Dog father) {
		super.mixGenetics(mother,father);
		this.fetchiness = (mother.fetchiness + 2*father.fetchiness) / 3
;
	}

	Dog Breed(Dog father){
		Dog puppy=new Dog();
		puppy.mixGenetics(this, father);
		return puppy;
	}

}

---------------------- here be main ------
module Main;
import Beast;
import std.stdio;

void main(string[] args){
	string aName = (args.length>1)?args[1]:"default name";
	string aArg2 = (args.length>2)?args[2]:"default arg2";
	Cat rita = new Cat(aName,aArg2);
	writeln(rita.toString);
	Cat ringo = new Cat("Ringo", 8);
	writeln(ringo.toString);

	Cat Riton = rita.breed(ringo);
	writeln(Riton.toString);

	Dog dolly = new Dog("Dolly",20);
	Dog rocky=new Dog("Rocky",14);
	Dog snoopy=dolly.Breed(rocky);
	writeln(dolly.toString);
	writeln(rocky.toString);
	writeln(snoopy.toString);
}

---------------------- and finally a satisfactory output sample :
*Beast.Cat* Name : Rita; Gen. : 0 ; Pick. : 15
*Beast.Cat* Name : Ringo; Gen. : 0 ; Pick. : 8
*Beast.Cat* Name : RitaRingo; Gen. : 1 ; Pick. : 11
*Beast.Dog* Name : Dolly; Gen. : 0 ; Fetch. : 20
*Beast.Dog* Name : Rocky; Gen. : 0 ; Fetch. : 14
*Beast.Dog* Name : DollyRocky; Gen. : 1 ; Fetch. : 16



More information about the Digitalmars-d-learn mailing list