MAC
MAC

Reputation: 6577

classes and functions

I have 3 classes named maths, alphabets and main. The Maths Class contains Add function and alphabet class contains a function same as Maths class. But the third class is for calling the function which is used for calling the above functions defined in the above classes.

How it will work?

Upvotes: 0

Views: 170

Answers (4)

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36977

C# programs do not contain "functions", but instead methods, which are attached to classes. So you call Math.Add or Alphabet.Add. Conflicting function names do not exist, in C#, for that reason. Conflicting class names are resolved by name spaces.

Upvotes: 0

user110714
user110714

Reputation:

Is this what you mean?

public class Maths
{
    public Maths() { }
    public Double Add(Double numberOne, Double numberTwo)
    {
        return numberOne + numberTwo;
    }
}

public class Alphabet
{
    public Alaphabet() { }
    public String Add(Char characterOne, Char characterTwo)
    {
        return characterOne.ToString() + characterTwo.ToString();
    }

}

public void Main()
{
    Alphabet alaphatbet = new Alphabet();
    String alphaAdd = alphabet.Add('a', 'b'); // Gives "ab"

    Maths maths = new Maths();
    Double mathsAdd = maths.Add(10, 5); // Gives 15
}

Upvotes: 1

ozczecho
ozczecho

Reputation: 8849

By using an interface that defines an Add function and having Math and alphabets implement that interface.

Upvotes: 0

sharptooth
sharptooth

Reputation: 170479

If the functions are static you'll have to explicitly tell which class they belong to - the compiler will be unable to resolve otherwise:

Maths.Add();

If they are not static the compiler will determine this based on the object type:

Maths maths = new Maths();
maths.Add();  // the necessary class and function will be resolved automatically

Upvotes: 2

Related Questions