Solved

Inheritance

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now