Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The following information is from "beginning visual c#2012" book. I am trying to implement the classes and interface from these information. I tried to do it but failed. Is there a way to declare instant, milk, sugar and description in interface and declare beantype or leaftype in class coffee and class tea respectively.

<<Interface>> CupOfCoffee - IHotDrink(Interface)
IHotDrink + BeanType:string
--------------
+Instant: bool CupOfTea - IHotDrink (Interface)
+Milk : bool + LeafType:string
+Sugar : byte
+Description : String

C#
namespace Interfaces
{
    interface IHotDrink
    class Coffee: IHotDrink
    {
        public string BeanType;
        public string Description;
        public string Sugar;
        public string Milk;
    }
    class Tea: IHotDrink
    {
        public string LeafType;
        public string Description;
        public string Sugar;
        public string Milk;
    }
    class Program
    {
        static void PrintInfo(IHotDrink item)
        {
            Console.WriteLine("BeanType: {0}, Description: {1}, Sugar: {2}, Milk:  {3}",item.BeanType, item.Description, item.Sugar,
                item.Milk);
        }
        static void PrintInfo(Tea item)
        {
            Console.WriteLine("LeafType: {0}, Description: {1}, Sugar: {2}, Milk:  {3}", item.LeafType, item.Description, item.Sugar,
                item.Milk);
        }
        static void Main()
        {
            Coffee c = new Coffee() { BeanType = "Medium Roast", Description = "Large", Sugar ="No", Milk = "Yes"  };
            Tea t = new Tea() { LeafType = "Green", Description = "Small", Sugar = "No", Milk = "Yes" };
            PrintInfo(c);
            PrintInfo(t);
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Apr-14 20:12pm    
So far, the question makes no sense. You did not show the definition of your interface, did not try to implement it. More importantly, you did not explain your problem.
What's wrong with just reading standard MSDN documentation?
—SA
RalvarezHose 21-Apr-14 20:30pm    
I was looking at the following codes below and trying to do the same with the above informations
RalvarezHose 21-Apr-14 20:30pm    
namespace Interfaces
{
interface IInfo
{
string GetName();
string GetAge();
}
class CA : IInfo
{
public string Name;
public int Age;
public string GetName() { return Name; }
public string GetAge() { return Age.ToString(); }
}
class CB : IInfo
{
public string First;
public string Last;
public double PersonsAge;
public string GetName() { return First + " " + Last; }
public string GetAge() { return PersonsAge.ToString(); }
}
class Program
{
static void PrintInfo(IInfo item)
{
Console.WriteLine("Name: {0}, Age {1}", item.GetName(), item.GetAge());
}
static void Main()
{
CA a = new CA() { Name = "John Doe", Age = 35 };
CB b = new CB() { First = "Jane", Last = "Doe", PersonsAge = 33 };
PrintInfo(a);
PrintInfo(b);
}
}
}
Sergey Alexandrovich Kryukov 21-Apr-14 21:59pm    
This is amazing: you still did not show definition of IHotDrink. How is this code related to the question.
No, please put all code in the body of the question ("Improve question", above) and make it all consistent.
—SA

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