All pastes #1756662 Raw Edit

Anonymous

public text v1 · immutable
#1756662 ·published 2010-01-18 22:21 UTC
rendered paste body
Animal.cs:
using System;

namespace AnimalInheritance
{
    internal abstract class Animal
    {
        protected int itsAge;
        protected string itsGender;
        protected string itsName;
        protected int itsWeight;

        protected Animal()
        {
        }

        protected Animal(string name, int age, int weight, string gender)
        {
            itsName = name;
            itsAge = age;
            itsWeight = weight;
            itsGender = gender;
        }

        ~Animal()
        {
        }

        //accessors - not needed...yet?
        public int GetAge()
        {
            return itsAge;
        }

        //void SetAge(int);
        public int GetWeight()
        {
            return itsWeight;
        }

        //void SetWeight();
        public string GetGender()
        {
            return itsGender;
        }

        public string GetName()
        {
            return itsName;
        }

        public void PrintAnimalInfo()
        {
        }

        public void Speak()
        {
        }

        public void Sleep()
        {
        }

        public void Eat()
        {
        }
    }

    internal class Dog : Animal
    {
        protected string itsBreed;
        // Constructors
        public Dog(string name, int age, int weight, string gender, string breed)
        {
            itsName = name;
            itsAge = age;
            itsWeight = weight;
            itsGender = gender;
            itsBreed = breed;
            Console.WriteLine("Creating a dog called {0}", GetName());
        }

        public Dog()
        {
        }

        ~Dog()
        {
            Console.WriteLine("Destroying the dog called {0}", GetName());
        }

        // Accessors - not needed, possibly never will
        //BREED GetBreed() const;
        // void SetBreed(BREED);

        // Other methods
        internal void WagTail()
        {
            Console.WriteLine("{0}: Wag tail.", GetName());
        }

        internal void BegForFood()
        {
            Console.WriteLine("{0}: Whine whine whine.", GetName());
        }

        //int GetAge();
        //   void SetAge(int);
        //   int GetWeight();
        //   void SetWeight();
        //   string GetGender();
        //   string GetName();
        internal string GetBreed()
        {
            return itsBreed;
        }

        internal void PrintAnimalInfo(Dog myDog)
        {
            Console.WriteLine("{0} is {1} years old and is a {2} {3}, weighs {4} pounds and is a {5}.", GetName(),
                              GetAge(),
                              GetBreed(), GetAnimalType(myDog), GetWeight(), GetGender());
        }

        private static string GetAnimalType(Dog myDog)
        {
            return myDog.ToString();
        }
    }

    internal class Cat : Animal
    {
        protected string itsBreed;
        // Constructors
        public Cat(string name, int age, int weight, string gender, string breed)
        {
            itsName = name;
            itsAge = age;
            itsWeight = weight;
            itsGender = gender;
            itsBreed = breed;
            Console.WriteLine("Creating a cat called {0}", GetName());
        }

        public Cat()
        {
        }

        ~Cat()
        {
            Console.WriteLine("Destroying the cat called {0}", GetName());
        }

        // Accessors - not needed, possibly never will
        //BREED GetBreed() const;
        // void SetBreed(BREED);

        // Other methods
/*
        internal void WagTail()
        {
            Console.WriteLine("{0}: Wag tail.",GetName());
        }
*/

        internal void Purrr()
        {
            Console.WriteLine("{0}: Purrr...", GetName());
        }

/*
        internal void BegForFood()
        {
            Console.WriteLine("Whine whine whine.");
        }
*/

        //int GetAge();
        //   void SetAge(int);
        //   int GetWeight();
        //   void SetWeight();
        //   string GetGender();
        //   string GetName();
        internal string GetBreed()
        {
            return itsBreed;
        }

        internal void PrintAnimalInfo(Cat myCat)
        {
            Console.WriteLine("{0} is {1} years old and is a {2} {3}, weighs {4} pounds and is a {5}.", GetName(),
                              GetAge(),
                              GetBreed(), GetAnimalType(myCat), GetWeight(), GetGender());
        }

        private static string GetAnimalType(Cat myCat)
        {
            return myCat.ToString();
        }
    }
}
Program.cs:
using System;

namespace AnimalInheritance
{
    internal class Program
    {
        //Relevant dog and cat information for Animal.cs breeds:
        //http://en.wikipedia.org/wiki/List_of_dog_breeds
        //http://en.wikipedia.org/wiki/List_of_cat_breeds
        private static void Main(string[] args)
        {
            var nancy = new Dog("Nancy", 7, 150, "female", "hounddog");
            var harold = new Cat("Harold", 10, 185, "male", "american shorthair");
            nancy.PrintAnimalInfo(nancy);
            harold.PrintAnimalInfo(harold);
            //This will not work because nancy is not a cat.
            //nancy.Purrr();
            harold.Purrr();
            nancy.WagTail();
            Console.WriteLine("Press any key to continue.");
            Console.Read();
        }
    }
}