Solved

Inheritance

Posted on 2008-09-30
4
189 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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