Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Below is a code snippet with errors. I want to execute the code, by passing a typed parameter to Hit() method or any thing.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CatDog
{

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Cat cat = new Cat();
            Human human = new Human();
            human.Hit(cat);
            human.Hit(dog);
            Console.ReadLine();
        }
    }

    class Dog
    {
        public void Run()
        {
            Console.WriteLine("Dog Runs");
        }
    }
    class Cat
    {
        public void Run()
        {
            Console.WriteLine("Cat Runs");
        }
    }

    class Human
    {
        public void Hit(_____ x)
        {
            
            x.Run();
        }
    }
}
Posted
Comments
thatraja 12-Dec-13 4:27am    
what's the error message? Always include that in your question
maajanes 12-Dec-13 4:29am    
This is an interview question. The error is "Identifier expected" in the Hit method definition.

I am including this variation on OriginalGriff's use of an abstract Class for the OP's information: it demonstrates taking advantage of the fact that you can delegate to an abstract Class some implementation of features that all inheritors will use/need.

I encourage anyone reading this to vote for either OriginalGriff's or Karthik's excellent answers, not for this variation.

C#
public abstract class Animal
{
    public abstract void AnimalHit();

    // we can implement a method here that
    // inheritors can use directly
    public void AnimalRun(string AnimalRunString)
    {
        Console.WriteLine(AnimalRunString);
    }
}

public class Dog: Animal
{
    public override void AnimalHit()
    {
        AnimalRun("dog runs");
    }
}

public class Cat: Animal
{
    public override void AnimalHit()
    {
        AnimalRun("cat runs");
    }
}

public class Human
{
    public void Hit(Animal theAnimal)
    {
        theAnimal.AnimalHit();
    }
}
You'll note that using base.AnimalRun("") is not required here, but using 'base would work.
 
Share this answer
 
Comments
Richard MacCutchan 12-Dec-13 6:01am    
5 for the extra detail. I wonder who gave Griff a 1 vote?
BillWoodruff 12-Dec-13 6:54am    
As John Lennon sang: "I think I'm not the only one." Imagine
There are a couple of ways you could do this. The first is to pass an object:
C#
public void Hit(object x)
   {
   }
But this has big problems - you have to check explicitly what type teh incoming parameter is and deal with it appropriately:
C#
public void Hit(object x)
   {
   Cat c = x as Cat;
   if (c != null)
      {
      c.Run();
      }
   Dog d = x as Dog;
   if (d != null)
      {
      d.Run();
      }
   }
That isn't a nice approach, and it's difficult to expand, and prone to errors.

The better approach would be to create a Base Class:
C#
public abstract class Animal
    {
    public abstract void Run();
    }
(We declare it as abstract so that you can't create an instance of it)
And then derive each animal class from that:
XML
public class Dog : Animal
    {
    public override void Run()
        {
        Console.WriteLine("Dog Runs");
        }
    }
public class Cat : Animal
    {
    public override void Run()
        {
        Console.WriteLine("Cat Runs");
        }
    }
(The override keyword tells the system this replaces the base class version with a Cat or Dog specific version)
You can then declare your Hit method to take an Animal, and the system will sort out which Run method to use:
C#
public class Human
    {
    public void Hit(Animal x)
        {
        x.Run();
        }
    }
 
Share this answer
 
Comments
BillWoodruff 12-Dec-13 5:52am    
+5 Excellent.
Karthik_Mahalingam 12-Dec-13 6:53am    
Good
HI

You should specify a Type in the parameter..
if u specify as Dog , u can be able to access only Dog Class Run Method, not the Cat class and vice versa,
To fix this issue u have to create an interface with the Common method as Run()
and implement the interface in the DOG and Cat Classes.
then you try to call the Human class Run method with any Animals which is implemented by IAnimal Interface..

If it is an Interview question, the answer is the above.



Try like this..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CatDog
{

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Cat cat = new Cat();
            Human human = new Human();
            human.Hit(cat);
            human.Hit(dog);
            Console.ReadLine();
        }
    }

    interface IAnimal { void Run();}
    class Dog : IAnimal
    {
        public   void Run()
        {
            Console.WriteLine("Dog Runs");
        }
    }
    class Cat : IAnimal
    {
        public   void Run()
        {
            Console.WriteLine("Cat Runs");
        }
    }

    class Human
    {
        public void Hit(IAnimal x)
        {

            x.Run();
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 12-Dec-13 5:52am    
+5 An excellent answer that nicely rounds-out OriginalGriff's response. Whoever down-voted this is stupid.
Karthik_Mahalingam 12-Dec-13 6:51am    
Thank you BillWoodruff..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900