03 iec t1s1 oops session 04

Upload: caininme

Post on 30-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    1/26

    Slide 1 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you will learn to:Define abstraction and encapsulationImplement encapsulation by using access specifiersUse methods

    Objectives

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    2/26

    Slide 2 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Abstraction and encapsulation are important features of anyobject-oriented programming language.Abstraction involves extracting only the relevant information.Encapsulation involves packaging one or more components

    together.

    Introducing Abstraction and Encapsulation

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    3/26

    Slide 3 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    An automobile salesperson is aware that different peoplehave different preferences.

    Some people are interested in the speed of a car, some in itsprice, some in the engine, and the some in its style.Although all of them want to buy a car, each of them isinterested in a specific attribute or feature.The salesman knows all the details of a car, but he presentsonly the relevant information to a potential customer.As a result, a the salesman practices abstraction and presentsonly relevant details to customer.

    Defining Abstraction

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    4/26

    Slide 4 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Encapsulation literally means to enclose in or as if in acapsule.Encapsulation is defined as the process of enclosing one or more items within a physical or logical package.

    It involves preventing access to nonessential details.

    Defining Encapsulation

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    5/26

    Slide 5 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    An access specifier defines the scope of a class member.A class member refers to the variables and functions in aclass.A program can have one or more classes.

    You may want some members of a class to be accessible toother classes.But, you may not want some other members of the class tobe accessible outside the class.

    Implementing Encapsulation by Using Access Specifiers

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    6/26

    Slide 6 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    C# supports the following access specifiers:publicprivateprotected

    internalprotected internal

    Types of Access Specifiers

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    7/26Slide 7 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Problem Statement:Write a program to calculate the area of a rectangle and asquare.

    Demo: Calculating Area and Volume by Using AccessSpecifiers

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    8/26Slide 8 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    A method is a set of one or more program statements,which can be executed by referring to the method name.To use methods, you need to:

    Define methods

    Call methods

    Using Methods

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    9/26Slide 9 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining Methods

    Defining a method means declaring the elements of itsstructure.Consider the syntax of defining a method:

    (Parameter List){

    Method Body

    }

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    10/26Slide 10 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining Methods (Contd.)

    The elements of the method declaration include the methodname, the parameters list, the return type, and the methodbody.The following are the elements of a method:

    Access specifier Return typeMethod nameParameter listMethod body

    Let us understand each of the element of the methoddeclaration.

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    11/26Slide 11 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    This determines theextent to which avariable or methodcan be accessedfrom another class.

    Defining Methods (Contd.)

    Defining a method means declaring theelements of its structure.Consider the syntax of defining amethod:

    (Parameter List)

    {

    Method Body

    }

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    12/26Slide 12 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining a method means declaring theelements of its structure.Consider the syntax of defining amethod:

    (Parameter List)

    {

    Method Body

    }

    A method can returna value of any type.If the method is notreturning any value,use void as thereturn type.

    Defining Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    13/26Slide 13 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining a method means declaring theelements of its structure.Consider the syntax of defining a method:

    (Parameter List)

    {

    Method Body

    }

    This is a uniqueidentifier and iscase-sensitive.The method namecannot be thesame as thevariable name or any other non-method itemdeclared in theclass.

    Defining Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    14/26Slide 14 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining a method means declaring theelements of its structure.Consider the syntax of defining a method:

    (Parameter List)

    {

    Method Body

    }

    This is used topass and receivethe data from amethod. It isenclosed betweenparentheses. Theparentheses areincluded even if there are noparameters.

    Defining Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    15/26Slide 15 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Defining a method means declaring theelements of its structure.Consider the syntax of defining amethod:

    (Parameter List)

    {

    Method Body

    }

    This contains theset of instructionsneeded to completethe required activity.

    Defining Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    16/26Slide 16 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    After defining the method, you can execute it by calling it.You can call a method by using the name of the method.The method name is followed by parentheses even if themethod call has no parameters, as shown in the following

    example:MethodName();

    Calling Methods

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    17/26Slide 17 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    The following is an example of calling methods:using System;

    class Calculator

    {

    public int AddNumber(int num1, int num2){

    int result;

    result = num1 + num2;

    return result;

    }static void Main(string[] args)

    Calling Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    18/26Slide 18 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    {

    Calculator cal = new Calculator();

    // The following statement is calling the

    // AddNumber method and passing 10 and

    // 20 as the parameter list.

    int value=cal.AddNumber(10, 20);

    Console.WriteLine("The result is{0}", value);

    Console.ReadLine();

    }

    }

    Calling Methods (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    19/26Slide 19 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Methods can also be declared with parameters. Consider the example of declared a method with parameters:

    void DisplayResult (int result)

    {

    //..}

    When the methods are declared with parameters, theyshould be called with parameters. The methods withparameters are called by passing the value using the

    following mechanism:ValueReference

    Output

    Using Methods with Parameters

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    20/26Slide 20 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Value : The parameters passed by value creates a separatecopy in the memory. The following example shows theparameters passed by value:

    void CalculateSum( int num1, int num2)

    {

    //

    }

    void Accept()

    {

    int val1=10;int val2=2;

    CalculateSum(val1,val2);

    }

    Using Methods with Parameters (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    21/26Slide 21 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Reference : The parameters passed by reference does notcreates a separate copy of the variable in the memory. Areference parameter stores the memory address of the datamember passed. The following example shows the parameterspassed by reference:

    void CalculateSum( ref int num1,ref int num2){

    //

    }

    void Accept()

    {int val1=10;

    int val2=2;

    CalculateSum( ref val1,ref val2);

    }

    Using Methods with Parameters (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    22/26Slide 22 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Output : The output parameters are used to pass the value outof the method. The following example shows the parameterspassed by reference:

    void CalculateSum( ref int num1,ref int num2, outint result)

    {result=num1+num2;

    }void Accept(){int val1=10;int val2=2;int recieveVal;CalculateSum( ref val1,ref val2,outrecieveVal);}

    Using Methods with Parameters (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    23/26Slide 23 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    Problem Statement:Write a program to swap two numbers by using reference typeparameters in a method.

    Demo: Swapping Two Numbers by Using Methods withParameters

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    24/26

    Slide 24 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you learned that:Abstraction is the process of reducing information content inorder to retain only the relevant information for a particular purpose.Encapsulation is the process of hiding all the details of anobject that do not contribute to its essential characteristics.An access specifier is used to determine whether any other class or function can access the member variables andfunctions of a particular class.The public access specifier allows a class to expose its

    member variables and member functions to other functionsand objects.The private access specifier allows a class to hide its member variables and member functions from other class functions andobjects.

    Summary

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    25/26

    Slide 25 of 26Session 4Ver. 1.0

    Object-Oriented Programming Using C#

    The protected access specifier allows a class to hide itsmember variables and member functions from other classobjects and functions, just like the private access specifier while implementing inheritance.A method is a set of one or more program statements that can

    be executed by referring to the method name.Defining a method means declaring the elements of itsstructure.The access modifiers that can be used with methods arepublic, protected, internal, protected internal, and private.

    Parameters allow information to be passed into and out of amethod. When you define a method, you can include a list of parameters in parentheses.

    Summary (Contd.)

  • 8/14/2019 03 Iec t1s1 Oops Session 04

    26/26

    Object-Oriented Programming Using C#

    Parameters can be passed by using any one of the followingparameters types:

    ValueReferenceOutput

    Pass by value is the default mechanism for passingparameters in C#.A reference parameter is a reference to a memory location of adata member.Output parameters are like reference parameters, except that

    they transfer data out of the method rather than into it.The return statement is used to return the control to the caller.

    Summary (Contd.)