Solved

Inheritance

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 63
Resolve Dependency Issues 4 48
Wav problem 4 19
use of unassigned local variable 'id' 6 32
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

27 Experts available now in Live!

Get 1:1 Help Now