Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
using System;

namespace StudyExample1
{
    class Program
    {
        class A
        {
            static int Fa = 2;
            protected A(int a) { Fa += a; }

            protected A() { Fa++; }

            protected int f 
            { 
                get { return Fa++; }
                set { Fa += value; }
            }
        }

        class B : A
        { 
            int Fb = 6;
            public B(int a) { Fb-=2*a; }
            public B(int a, int b)
                
            {
                Fb += b+a;
            }
            public new int f 
            {
                get { return Fb; }
                set { Fb += value; }
            }
            public override string ToString()
            {
                base.f=-1;
                return String.Format("{0}", base.f - f);
            }
        }

        static void Main(string[] args)
        {
            B obj1 = new B(2), obj2 = new B(2, 1);
            obj1.f = 1;
            obj2.f = 2;
            Console.Write(obj1);
            Console.Write(obj2);
            Console.Write(obj2);
            Console.Write(obj1);
             
        }
    }
}


What I have tried:

C#
using System;

namespace StudyExample1
{
    class Program
    {
        class A
        {
            static int Fa = 2;
            protected A(int a) { Fa += a; }

            protected A() { Fa++; }

            protected int f 
            { 
                get { return Fa++; }
                set { Fa += value; }
            }
        }

        class B : A
        { 
            int Fb = 6;
            public B(int a) { Fb-=2*a; }
            public B(int a, int b)
                
            {
                Fb += b+a;
            }
            public new int f 
            {
                get { return Fb; }
                set { Fb += value; }
            }
            public override string ToString()
            {
                base.f=-1;
                return String.Format("{0}", base.f - f);
            }
        }

        static void Main(string[] args)
        {
            B obj1 = new B(2), obj2 = new B(2, 1);
            obj1.f = 1;
            obj2.f = 2;
            Console.Write(obj1);
            Console.Write(obj2);
            Console.Write(obj2);
            Console.Write(obj1);
             
        }
    }
}
Posted
Updated 20-Dec-17 22:17pm
Comments
[no name] 20-Dec-17 15:28pm    
What do you expect to get?
What do you get?
What are your doubts on the difference?
j snooze 20-Dec-17 17:22pm    
We have no idea what you are trying to do. You simply dropped a bunch of code and said "I don't know what this is, you tell me"...perhaps you should ask your teacher if you don't understand, we have no syllabus, or specs on what you are learning or trying to accomplish through this code.

1 solution

Row by row:

namespace StudyExample1
{
    class Program
    {
        class A
        {
            static int Fa = 2; //this variable is visible by class name like A.Fa. So, it value exists in only one place in memory
			                   // you can create many objects od A, but they will have same Fa.
            protected A(int a) { Fa += a; } //constructor, add to Fa value;
 
            protected A() { Fa++; } // constructor, adds to Fa 1
 
            protected int f //property
            { 
                get { return Fa++; }// always return Fa + 1, but not change Fa
                set { Fa += value; }//when set, add to Fa the value
            }
        }
 
        class B : A
        { 
            int Fb = 6;//inner variable
            public B(int a) { Fb-=2*a; }//constructor
            public B(int a, int b)//overloaded constructor- same name, other params
                
            {
                Fb += b+a;
            }
            public new int f //aha, hiding the property f od base class A - when called from object of type B, will return 6
            {
                get { return Fb; }
                set { Fb += value; }//set add to Fb the value
            }
            public override string ToString()
            {
                base.f=-1;// call to base class property (add -1 to Fa)
                return String.Format("{0}", base.f - f);// call to base f (get) than call child class B property, than substract one from another.
            }
        }
 
        static void Main(string[] args)
        {
            B obj1 = new B(2), obj2 = new B(2, 1); //Pay attention, all B obj1 and obj2 have same Fa - and different Fb.
            obj1.f = 1;
            obj2.f = 2;
            Console.Write(obj1);
            Console.Write(obj2);
            Console.Write(obj2);
            Console.Write(obj1);
             
        }
    }
}
 
Share this answer
 
v3

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