OOP'S Concept in C#, OOP in C#, Object oriented programming structure on c#

0
OOP'S Concept in C#:-


OOP'S means Object Oriented Programming Structure, It is used to create a real-world based application that provides dynamic memory allocation, security, reusability, extendibility features in an application.

C# Programming language follows 80 to 90% of features of oop's but it is not 100% Object-oriented because it supports primitive datatype which has been created in C and C++ languages.


Features of OOP'S:-


1)  Security ----->   Data abstraction and Data Encapsulation

2)  Extendibility:----->     Polymorphism

3)  Reusability:-       Inheritance

4) Accessibility:-       Access Specifier

5) Usability:-     Static Polymorphism


,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Class and Object:-

Class:-   It is a user-defined datatype that will work as a blueprint of an object, Object definition will be contained underclass.
a class can define data members, member function, constructor, properties underclass.

for example, electronics is the class that is used to define the properties and features of electronic devices such as Mobile, Laptop, etc.


Object:-   It is a real-world entity that has an identity,  state, and behavior, Object address will work as an identity, Memory allocation will work as a state and we can store and call data member and member function under Object is called behavior.


Syntax of class:-


class Classname
{
        Data member ;  //variable
        Member Function ;  //method
        Main Function ();

}

Syntax of Object:-

Classname  reference = new Classname();
obj.FunctionName();



Now I am creating an Addition program to explain the complete functionality of Class and Object?

class Addition
    {
        int a, b, c;
        void add()
        {
            Console.WriteLine("Enter first number");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter second number");
            b = int.Parse(Console.ReadLine());
            c = a + b;
            Console.WriteLine("Result is {0}",c);
        }

        static void Main()
        {
            Addition obj = new Addition();
            obj.add();
            Addition obj1 = new Addition();
            obj1.add();
            Console.ReadKey();
        }
    }



Now I am creating a Student class and define the property of Student Object:-

class Student
    {
        int rno;
        String sname;
        void Accept()
        {
            Console.WriteLine("Enter rno");
            rno = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter name");
            sname = Console.ReadLine();

        }
        void Display()
        {
            Console.WriteLine("Rno is " + rno + " Sname is " + sname);
        }
        static void Main()
        {
            Student obj = new Student();
            obj.Accept();
            obj.Display();
            Student obj1 = new Student();
            obj1.Accept();
            obj1.Display();
            Console.ReadKey();
        }
    }



Component of class:-

1) Data Member:-   It is also called variable, it is used to define the attribute of an object, Data member will be defined using two different ways

1)  Static Data Member:-  It is also called class type data member, it will allocate memory when we compile the program.  It has constant memory means we can not re-create memory for static data members.

Syntax of Static Data Member:-

static datatype identifier = value.

static int x;


Static Data Member will be accessed by Classname, no need to create objects for them.

Example of Static Data Member:-

class StaticExample
    {
        static int x, y, z;
        static void Main()
        {
            StaticExample.x = 100;
            StaticExample.y = 200;
            StaticExample.z = StaticExample.x + StaticExample.y;
            Console.WriteLine(StaticExample.z);
            Console.ReadKey();
        }
    }




2) An instance or Dynamic Data Member:-  This type of data member will be called by Object at run time,

Syntax:-

datatype identifier=value;

Example of Dynamic Data Member in C#:-

class DynamicExample
        {
            int x, y, z;
            static void Main()
            {
                DynamicExample obj = new DynamicExample();
                obj.x = 10;
                obj.y = 200;
                obj.z = obj.x + obj.y;
                Console.WriteLine(obj.z);
                DynamicExample obj1 = new DynamicExample();
                obj1.x = 100;
                obj1.y = 2000;
                obj1.z = obj1.x + obj1.y;
                Console.WriteLine(obj1.z);
                Console.ReadKey();
            }
        }





int x;

 class StaticDynamic
    {
        static int x = 100;  //static variable
        int y = 200;  //dynamic variable
        static void Main()
        {
            Console.WriteLine(StaticDynamic.x);
            StaticDynamic obj = new StaticDynamic();
            Console.WriteLine(obj.y);
            Console.ReadKey();
        }
    }


Another example to explain Static and Dynamic Variable in C#?

class DatamemberExample
    {
        static int x=100, y=20, z;
        int a, b;
        public static void Main()
        {
            x = 10;
            y = 20;

            DatamemberExample obj = null;
            obj= new DatamemberExample();
            obj.a = 1;
            obj.b = 2;
           
            DatamemberExample obj1 = new DatamemberExample();
            obj1.a = 11;
            obj1.b = 22;
            x = 2;  
            y = 3;
            Console.WriteLine(x + y);
            Console.WriteLine(obj.a + obj.b);
            Console.WriteLine(obj1.a + obj1.b);
            Console.ReadKey();


        }

Member Function:-
 

 It is used to define static data members and dynamic data members.  The static member function will be defined under static data members. dynamic member function will be used to define dynamic data members.

Type of Member Function:-

1)  Static Member Function:-   It is used to define static data members under method block.

It will be called by Class name.


static  ReturnType  FunctionName()
{

}
class StaticMemberFunction
    {
        static int a,b,c;
        static void Addition()
        {
            a = 100;
            b = 200;
            c = a + b;
            Console.WriteLine(c);
        }
        static void Main()
        {
            StaticMemberFunction.Addition();
            Console.ReadKey();
        }
    }

1)  Dynamic Member Function:-  
It is used to define dynamic data members under method block.

It will be called by Class name. It will be called Object


  ReturnType  FunctionName()
{

}


 class DynamicMemberFunction
    {
        int a,b,c;
        void Addition()
        {
            a = 100;
            b = 200;
            c = a + b;
            Console.WriteLine(c);
        }
        static void Main()
        {
           DynamicMemberFunction obj = new DynamicMemberFunction();
           obj.Addition();
           Console.ReadKey();
        }
    }


Another example to explain Static and Dynamic?

class MemberFunctionExample
    {
        static int x, y, z;
        int a, b,c;

        static void Addition()
        {
            Console.WriteLine("Enter First Number");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second Number");
            y = int.Parse(Console.ReadLine());
            z = x + y;
            Console.WriteLine(z);
        }
        void Substraction()
        {
            Console.WriteLine("Enter First Number");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second Number");
            b = int.Parse(Console.ReadLine());
            c = a - b;
            Console.WriteLine(c);
            
        }

        static void Main()
        {
            MemberFunctionExample.Addition();
            MemberFunctionExample.Addition();
            MemberFunctionExample obj = new MemberFunctionExample();
            obj.Substraction();
            MemberFunctionExample obj1 = new MemberFunctionExample();
            obj1.Substraction();
            Console.ReadKey();
            

        }

    }


Type of Member Function According to Parameters:

1)  Default:-  

We can not pass any value from parameters all values will be declared under the method block.

Type of Default Function:-


1.1 Without Return type:-
It will not return any output data.

void Method1()
{

 
}


1.2 With Return Type:-  It will return Output data

Datatype Method1()
{
        return value;
 
}

 class DynamicMemberFunction
    {
        int a,b,c;
        void Addition()
        {
            a = 100;
            b = 200;
            c = a + b;
            Console.WriteLine(c);
        }
        int AdditionProgram()
        {
            a = 100;
            b = 200;
            c = a + b;
            return c;
        }
        static void Main()
        {
           DynamicMemberFunction obj = new DynamicMemberFunction();
           int c= obj.AdditionProgram();
           Console.WriteLine(c);
           Console.ReadKey();
        }
    }



Now I am explaining the example of Default and Return Type Function With Example?

class MemberFunctionExample
    {
        static int x, y, z;
        float a, b,c;

        static int Addition()
        {
            Console.WriteLine("Enter First Number");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second Number");
            y = int.Parse(Console.ReadLine());
            z = x + y;
            return z;
        }
        float Substraction()
        {
            Console.WriteLine("Enter First Number");
            a = float.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second Number");
            b = float.Parse(Console.ReadLine());
            c = a - b;
            return c;
            
        }

        static void Main()
        {
           int r= MemberFunctionExample.Addition();
           Console.WriteLine(r);
            MemberFunctionExample obj = new MemberFunctionExample();
           float t= obj.Substraction();
           Console.WriteLine(t);
            MemberFunctionExample obj1 = new MemberFunctionExample();
           float t1= obj1.Substraction();
           Console.WriteLine(t1);
            Console.ReadKey();
            

        }

    }
Parametrized:-  We can pass parameters to calling function to called function, which means input will be decided at run time.

 class ParametrisedFunctionExample
    {
        void Addition(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        int Subtraction(int x, int y)
        {
            return x - y;
        }
        static void Main()
        {
            ParametrisedFunctionExample obj = new ParametrisedFunctionExample();
            obj.Addition(1000, 200);
            int res = obj.Substraction(100,20);
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }


Another example of a parametrized function?

class ParametrisedExaple
    {
        static float si;
        int c;
        static internal void CalulateSI(float p,float r,float t)
        {
            si = (p * r * t) / 100;
            Console.WriteLine("p={0},r={1} and t={2} and si={3}", p, r, t, si);
        }
        internal int CalulateMulti(int a, int b)
        {
            c = a * b;
            return c;
            
        }

        public static void Main()
        {
            ParametrisedExaple.CalulateSI(45000, 2, 2);
            ParametrisedExaple obj = new ParametrisedExaple();
            int data= obj.CalulateMulti(1000, 20);
            Console.WriteLine("Result is {0}",data);
            Console.ReadKey();
        }
    }


What is out keyword in C#?

It is used to declare out the type of parameters that are used to contain the output of a particular variable, Out is used to change the direction of parameters in the Program.

Example of Out Parameters?

class OutExample
    {
        void Addition(int x, int y, out int z)
        {

            z = x / y;
        }

        public static void Main()
        {
            OutExample obj = new OutExample();
            int a=100,b=20,c;
            obj.Addition(a, b,out c);
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }



What is ref keyword in c#?

It is used to pass the reference of calling parameter to called parameter, if we want to implement call by reference functionality under c# program then we can use it.

Now I am creating the program where swapping will be performed into the actual address.

class Swap
    {
        
        void swap(ref int a, ref int b)
        {
           a=a+b;
           b=a-b;
           a=a-b;
           Console.WriteLine("a={0} and b={1}", a, b);

        }

        static void Main()
        {
            Swap obj = new Swap();
            int x = 10, y = 2;
            obj.swap(ref x, ref y);
            Console.WriteLine("x={0} and y={1}", x, y);
            Console.ReadKey();
        }
    }



What is Parametrised Array in C#?

We can pass an array as a parameter to pass the array variable from the calling function to called function.

Example of Parametrised Array?

class ParamArray
    {
        void calulateSum(int[] arr)
        {
            int s = 0;
            foreach (int data in arr)
            {
                s = s + data;
            }

            Console.WriteLine("Result is {0}", s);
        }

        static void Main()
        {
            ParamArray obj = new ParamArray();
            int[] x = { 11, 23, 34, 67, 12 };
            obj.calulateSum(x);
            Console.ReadKey();
        }


    }



Another Example of Parametrised Array in C#?

 class ParamArray
    {
        int s = 0;
        void SumofArray(int[] arr)
        {
            foreach (int a in arr)
            {
                s = s + a;
            }
            Console.WriteLine("Sum is {0}", s);
        }
        int[] ReverseArray(int [] arr)
        {
            int[] arr1 = new int[arr.Length];
            for (int i = 0, j = arr.Length - 1; i < arr.Length; i++, j--)
            {
                arr1[i] = arr[j];
            }
            return arr1;
        }

        static void Main()
        {
            ParamArray obj = new ParamArray();
            int[] d = { 1, 2, 3, 4, 7, 9 };
            obj.SumofArray(d);
            int[] arr = obj.ReverseArray(d);
            foreach (int a in arr)
            {
                Console.WriteLine(a);
            }
            Console.ReadKey();
        }
    }



               
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)