Solved

Inheritance

Posted on 2008-09-30
4
190 Views
Last Modified: 2012-05-05
I have written code in one file(FoodItem.cs).Solution name is ConsoleApplication2:
using System;

namespace ConsoleApplication2
{
      /// <summary>
      /// Summary description for FoodItem.
      /// </summary>
      public class FoodItem
      {
            
            extern int  Calories_Per_Gram=9;
            private  int fatgrams;
            protected int servings;            
      
            public FoodItem(int Fatgrams1,int servings1)
            {
                  fatgrams=Fatgrams1;
                  servings=servings1;
            }
            
            private int Calories()
            {
                  return fatgrams *Calories_Per_Gram ;
            }

            public int CaloriesPerSavings()
            {      
                  return(Calories ()/servings );
            }
      }

}

I build the above file.
Now in Another file(Pizza.cs) in ConsoleApplication2,I code:

using System;

namespace ConsoleApplication2
{
      /// <summary>
      /// Summary description for Pizza.
      /// </summary>
 public class Pizza extends  FoodItem
      {
            public Pizza (int FatGrams)
                  {
                        super(Fatgrams,8);
                  }
            }
      
}


It is showing errors:
1) at extern
2) public pizza(at the constructor)
3) at super
0
Comment
Question by:sarang8180
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:xRalf
xRalf earned 150 total points
ID: 22603820
Hello sarang8180,

extends and super are not C# keywords. Maybe you were using java. Try to look at the C# syntax.

Regards,

xRalf
0
 
LVL 10

Accepted Solution

by:
oxyoo earned 350 total points
ID: 22603833
Hi,

Here's a working example (namespace part not included)...
    /// <summary>
    /// Summary description for FoodItem.
    /// </summary>
    public class FoodItem
    {
        private extern int Calories_Per_Gram = 9;
        private readonly int fatgrams;
        protected int servings;
 
        public FoodItem()
        {
        }
 
        public FoodItem(int Fatgrams1, int servings1)
        {
            fatgrams = Fatgrams1;
            servings = servings1;
        }
 
        private int Calories()
        {
            return fatgrams*Calories_Per_Gram;
        }
 
        public int CaloriesPerSavings()
        {
            return (Calories()/servings);
        }
    }
 
 
 
 
 
 
    /// <summary>
    /// Summary description for Pizza.
    /// </summary>
    public class Pizza : FoodItem
    {
        public Pizza(int fatGrams) : base(fatGrams, 8)
        {
        }
    }

Open in new window

0
 

Author Comment

by:sarang8180
ID: 22603998
now it is showing error in FoddItem.cs

the modifier 'extern' is not valid for this item
0
 
LVL 10

Expert Comment

by:oxyoo
ID: 22604008
Hi,

Change
        private extern int Calories_Per_Gram = 9;

To
        private readonly int Calories_Per_Gram = 9;

You are not allowed to use extern there.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 93
C# Service FileSystemwatcher isse 9 64
Build a string of emails from a gridview 2 31
.Net remove carriage returns, line feeds and tabbs 5 36
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question