OOP and C#

download OOP and C#

of 8

Transcript of OOP and C#

  • 8/9/2019 OOP and C#

    1/8

    IT111P B55

    Object Oriented

    ProgrammingOOP and the programming industryPatrick Villar

    8/26/2010

    It shows how and what an OOP is; its usage, concepts features.

  • 8/9/2019 OOP and C#

    2/8

    Object Oriented Programming

    OOP is simply including objects outside like sound files, image files, video files or user-defined

    files to design applications and computer programs. Some of OOPs features or techniques are

    Inheritance, Abstraction, Polymorphism and Encapsulation. Some if its basic concepts are Classes,

    Objects, and Methods. OOP minimizes the fragmentation of codes making it easier to modify existingcodes and be used as objects instead. It helps the programmer to organize their own codes for

    developing graphical user interface applications.

    An advantage of OOP is at ease of modifying objects reducing the maintenance costs of the

    system. OOP is also a complicated procedural programming but allows flexible interactions. According to

    ArchWing Innovations OO systems are also easier for non-technical personnel to understand and easier

    for them to participate in the maintenance and enhancement of a system because it appeals to natural

    human cognition patterns.OOP can speed up developing time thus many objects are standard in some

    systems and can be reused.

    According to AWI there are almost two-dozen major OO languages, the leading commercial OO

    languages are:

    y C++y Smalltalky Java

    C++

    C++ is an OO version of C. It is compatible with C (it is actually a superset), so that existing C code can be

    incorporated into C++ programs. C++ programs are fast and efficient.

    Smalltalk

    Smalltalk is a pure OO language. A rich class library and a dynamic development environment makes

    Smalltalk a favorite of OO developers.

    Java

    Java is the latest, flashiest OO language. It has taken the software industry by storm due to its close ties

    with the Internet and Web browsers. Java is a mixture of C++ and Smalltalk.

  • 8/9/2019 OOP and C#

    3/8

    OOP andC#

    The new programming language C# is fully object-oriented and in many respects combines the

    best elements of C++, Smalltalk, Java, and Visual Basic. C# is closest to Java among other programming

    languages, and the basic structure of the .NET environment is rather close to the Java environment, with

    C# and other .NET languages generating an intermediate language, which executes on a virtual machine(the Common Language Runtime). But C# contains some important enhancements. The most significant

    is that in C# "everything is an object," as in Smalltalk. This uniformity greatly simplifies use of the

    language, enabling numbers to be treated like objects, stored in collections, and so on. But whereas

    Smalltalk paid a heavy performance penalty for such uniformity, C# has a unique concept of "boxing"

    and "unboxing," in which a primitive value like a number can be "boxed" into an object wrapper when

    there is need for the value to be treated as an object. But for usages where an object is not required,

    simple values can be efficiently manipulated directly. http://zone.ni.com/

    We can access an objects data and behaviour through and interface. The objects data and

    behaviour is within the object so a program can treat it like a box accessible only through and interface.

    Parts of an object

    y Interfacey Behavioury Member or instance variablesThe code inside the method is a behaviour since it is the code that actually makes the object do the

    work. Programs outside can use the object even if we change the implementation for as long as we

    dont change the interface. As long as method name, parameter and returned value is unchanged, we

    can alter the behaviour for as long as we want.

    Classes inC#

    [attributes] [modifiers] class identifier[:base-list] { class-body}[;]

    Here are the parts of this statement:

    y attributes (Optional)Attributes hold additional declarative information, as we'll see in Chapter14, "Using Attributes and Reflection."

    y modifiers (Optional)The allowed modifiers are new, static, virtual, abstract, override, and avalid combination of the four access modifiers we'll see in this chapter.

    y identifierThe class name.y base-list (Optional)A list that contains the base class and any implemented interfaces,

    separated by commas.

    y class-bodyDeclarations of the class members.

  • 8/9/2019 OOP and C#

    4/8

    Constructors

    [attributes] [modifiers]identifier([formal-parameter-list])

    [initializer] { constructor-body}

    Here are the parts of this statement:

    y attributes (Optional)Hold additional declarative information, as we'll see in Chapter 14.y modifiers (Optional)The allowed modifiers are new, static, virtual, abstract, override, and a

    valid combination of the four access modifiers.

    y identifierThe same as the class name.y formal-parameter-list (Optional)The optional parameters passed to the constructor. The

    parameters must be as accessible as the constructor itself.

    y initializer (Optional)Invoked before the execution of the constructor body. The initializer canbe one of the following with an optional argument-list:

    :base (argument-list)

    :this (argument-list)

    y constructor-bodyThe block that contains the statements that initialize the object.Namespaces

    namespacename[.name1] ...] {

    type-declarations

    }

    Here are the parts of this statement:

    y name, name1A namespace name can be any legal identifier, and it can include periods.y type-declarationsWithin a namespace, you can declare one or more of the following types:

    another namespace, a class, interface, struct, enum, or delegate.

    You can create as many namespaces in a file as you want; just enclose the code for each namespace in

    the code block following the namespace keyword. For example, here's how we can put the Messager

    class into the namespace Output:

    namespace Output

    {

    classMessager

    {

    public string message = "Default message.";

    public void DisplayMessage()

    {

  • 8/9/2019 OOP and C#

    5/8

    System.Console.WriteLine(message);

    }

    }

    }

    To access Messager outside the Output namespace, you qualify its name as Output.Messsager, just as

    you can qualify Console.Writeline as System. Console.WriteLine.

    Some Examples

    // private data, not publicly accessible

    boolOnOff;

    int volume;

    int channel;

    // accessor methods

    SwitchOn() ;

    SwitchOff() ;

    SetVolume(int volume ) ;

    SetChannel(int channel) ;

    intGetVolume() ;

    intGetChannel() ;

  • 8/9/2019 OOP and C#

    6/8

    Student Class Program

    //Class

    class Student

    {

    // Data Members

    public string Name;

    public int Age;

    // Member Function

    public void Display()

    {

    Console.WriteLine("Name {0}\n Age {1}", Name, Age);

    }

    }

    //Main

    using System;

    namespace CSTutorials

    {

    class Student

    {

    // Data Members

    public string Name;

    public int Age;

  • 8/9/2019 OOP and C#

    7/8

    // Member Function

    public void Display()

    {

    Console.WriteLine("Name {0}\n Age {1}", Name, Age);

    }

    }

    class Program

    {

    static void Main(string[] args)

    {

    Student S1; // Declaration

    S1 = new Student(); // Instantiation

    Student S2 = new Student(); // Single line Declaration & Instantiation

    // Assigning value to member variables

    S1.Name = "Michael";

    S1.Age = 37;

    S1.Name = "Kimi";

    S2.Age = 25;

    // Invoking member function

    S1.Display();

    S2.Display();

    Console.ReadLine();

    }

    }

    }

    Summary

    Programming is made easier and efficient because of the OOPs concepts and features and

    never ending improvement of the world of programming. More namespaces that we can include can

    become or narrow down to small classes. Simply, OOP is used to make the programming industry

    efficient in memory management and prevent the fragmentation of files used by users.

  • 8/9/2019 OOP and C#

    8/8

    Very useful references

    http://www.archwing.com/technet/technet_OO.html

    http://www.c-

    sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11162005070743AM/obj

    ectorientedcsharp.aspx

    http://cplus.about.com/od/learnc/ss/csharpclasses.htm

    http://www.informit.com/articles/article.aspx?p=101373&seqNum=2

    http://zone.ni.com/devzone/cda/ph/p/id/45

    http://www.astahost.com/info.php/Tutorial-Lesson-4-Object-Oriented-

    Programming_t15397.html

    http://www.google.com.ph/