07_oops_sg_01

Upload: kien-le

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 07_OOPS_SG_01

    1/24

    Introduction to the

    Object-OrientedApproach

    Chapter1

    Object-Oriented Programming (OOP) is one of the mostpopular methodologies in software development. It offers a

    powerful model for creating computer programs. It speeds

    the program development process, improves maintenance,

    and enhances reusability of programs.

    This chapter introduces object-oriented methodology and

    discusses the concepts of objects, classes, messages, andmethods. It provides an overview of the phases involved in

    object-oriented analysis and design. This chapter also

    explains how to define classes in C#.

    In this chapter, you will learn to: Explain features of the object-oriented

    methodology

    Describe the phases of the object-orientedmethodology

    Define classes in C#

    Objectives

  • 8/2/2019 07_OOPS_SG_01

    2/24

  • 8/2/2019 07_OOPS_SG_01

    3/24

    Introduction to the Object-Oriented Approach 1.3NIIT

    Object-orientation is a software development methodology that is based on modeling areal-world system. An object is the core concept involved in object orientation. An object

    is the representation of a real-word entity or concept. For example, an employee, awindow, a car, or a bird can be modeled as objects. You can think of an object-orientedmodel as a collection of objects and their inter-relationships.

    Object-orientation is a type of methodology used for building software applications. An

    object-oriented program consists of classes, object, and methods.

    If you were asked to create a classroom, you will start by creating a room and defining its

    boundaries and features. Next, you will put together all the components, such as chairs,tables, and books. Similarly, if you want to create an aircraft or a high-rise building, you

    can do so by putting together several components or parts. These components or parts are

    analogous to objects in the object-oriented methodology.

    The object-oriented methodology in software development revolves around a single

    concept called the object. Software is developed by breaking the application into

    component objects. These objects interact with each other when the whole application isput together.

    An objectis a combination of messages and data. Objects can receive and send messagesand use messages to interact with each other. The messages contain information that is tobe passed to the recipient object.

    Objects

    An object literally means a material thing that is capable of being presented to thesenses. For our purpose, an object is a tangible entity that may exhibit some well-defined

    behavior. For example, let us consider a tennis ball:

    A tennis ball is a tangible entity, with a visible boundary.

    A tennis ball has a specific defined purpose (such as bouncing).

    You can direct a specific action towards a tennis ball by hitting it with a racquet orby tossing it.

    But the definition of an object is not limited to merely something that can be seen, held,

    and touched, such as a tennis ball or a car. For the purpose of software development, thedefinition of an object needs refinement. For example, consider the Acme Nut and Bolt

    Company. An organization does not have a visible boundary, unlike a tennis ball. While it

    Object-Oriented Methodology

    The Foundation of Object-Orientation

  • 8/2/2019 07_OOPS_SG_01

    4/24

    1.4 Introduction to the Object-Oriented Approach NIIT

    does not possess a physical boundary, it does have a conceptual boundary. Like all

    organizations, it has a specific defined purpose, and one can direct a specific actiontowards it. Thus, the Acme Nut and Bolt Company is an object.

    State, Behavior, and Identity of an Object

    According to Grady Booch, a renowned software architect, an object has the following

    characteristics:

    It has a state.

    It may display behavior.

    It has a unique identity.

    The state of an object is indicated by a set of attributes and their values. For example, achemical can be characterized by its temperature, color, and density. The behavior of a

    chemical refers to the change of its attributes over a period of time. A chemical also hasstate, such as solid, liquid, or gas.

    The preceding paragraph explains the first two points of the definition of an object byGrady Booch An object has state, exhibits some well-defined behavior. Now, you

    will examine the next part of the definition - and has a unique identity.

    Each object has a unique identity, and the identity of an object distinguishes the object

    from all other objects.

    For example, you can take a car as an object. It can have states such as moving and

    stationary. It can accelerate, decelerate, turn right, or turn left, which is its behavior. Thecar also has an identity, such as a unique registration number. The following figure

    depicts the characteristics of a car.

    An Object Has State, Behavior, and Identity

    Two objects may have the same behavior and state, but they will never have the sameidentity. The identity of an object does not change in its lifetime. For example, two tennis

    balls may have the same color, be made of the same material, have the same weight and

    the same circumference, and display the same behavior. However, they will have distinct

    State:

    Stationary

    Behavior:

    AccelerateIdentity: Registration

    Number (XY245)

  • 8/2/2019 07_OOPS_SG_01

    5/24

    Introduction to the Object-Oriented Approach 1.5NIIT

    Just a minute:

    identities (for example, one ball will have the factory seal number A189735 and the

    other S660794).

    Identify the possible states of the following objects:

    1. A cell phone

    2. A stereo

    Answer:

    1. States of a cell phone: Off, Ring, Vibrate, and Call

    2. States of a stereo: Play, Pause, Rewind, and Forward

    Classes

    Look at the world around you. It is full of objects of various shapes, sizes, colors, andbehavior. For example, the earth is inhabited by millions of animals. A zoologist cannot

    do any meaningful study of these objects without organizing this vast variety of animals

    found in the world in a logical manner. Thus, the zoologists have classified animals into

    kingdoms, genus, families, and species.

    All animals and birds, which are essentially objects, can be classified on the basis of theircommon attributes. For example, the peacock, the sparrow, and the kingfisher are all

    birds. All of them share characteristics that are common to the family of birds. All of

    them lay eggs, are covered with feathers, have hollow bone structures, and have theability to fly. Therefore, they share structural and behavioral similarities and belong to the

    class called Birds.

  • 8/2/2019 07_OOPS_SG_01

    6/24

    1.6 Introduction to the Object-Oriented Approach NIIT

    This is shown in the following figure.

    The Class - Birds

    From the preceding example, you may define a class as a declaration, a template, or ablueprint that can be used to classify objects. Similarly, Book is a class and Gone with

    the Wind and Farewell to Arms are the objects of this class.

    Consider a video game that involves two players, Tom and Mark, who fight with each

    other. Tom chooses a shotgun and Mark chooses a pistol as their weapons in the game. Ifyou need to create an object model for this game, you can identify a total of four objects.

    They are Tom, Mark, the shotgun, and the pistol.

    The object shotgun belongs to the class Firearms. It has certain attributes that are commonwith the pistol. However a shotgun is a unique object. Both the shotgun and the pistol are

    examples or instances of Firearms. Therefore, the shotgun and pistol are objects of the

    same class, as shown in the following figure.

    Classes and Objects

    Messages and Methods

    Objects do not exist in isolation. They interact with other objects. These interactions take

    place through messages. Grady Booch has defined behavior as follows:

    Behavior is how an object acts and reacts, in terms of its state changes and messagepassing.

    In the example of a video game, every time Tom attacks, Mark either attacks or defends

    himself.

    Class: Firearms

    Object: Pistol Object: Shotgun

    Birds

    Peacock Kingfisher Sparrow

  • 8/2/2019 07_OOPS_SG_01

    7/24

    Introduction to the Object-Oriented Approach 1.7NIIT

    Just a minute:

    When Tom attacks Mark, Mark receives a message and reacts to that message. The

    reaction can be exhibiting a particular behavior such as running, hiding, dodging, orfighting back. For example, when Tom attacks, Mark receives a message Being

    Attacked. Mark responds to this message by hiding. In this situation, hiding is a behavior

    exhibited by Mark. Therefore, behavior (also called method in object-oriented

    methodology) is simply a set of actions taken by the receiving object in response to amessage.

    Dr. James and Mr. Hyde went to the railway station to book tickets for 3rd December.At the railway station, they requested the clerk at the ticket counter to book two ticketsfor the Flying Express in the first class. Identify the following:

    1. The possible receiver of the message in this situation.

    2. The possible method that the receiver can use.

    Answer:

    1. The receiver of the message in this case will be the clerk at the ticket counter.

    2. The clerk will check if two tickets are available on the requested train in thedesired class and for the desired date. If the tickets are available, the clerk will

    enter the details (name, age, departure date, and seat), confirm the reservation,and collect the required fare.

  • 8/2/2019 07_OOPS_SG_01

    8/24

    1.8 Introduction to the Object-Oriented Approach NIIT

    As discussed earlier, an object has a state, behavior, and identity. An object is reusable.

    Therefore, an object exists as a stand-alone entity that can be used in any relevant context.

    For example, if you have a chair, you can use the same chair in an office, in a classroom,or in a garden.

    An existing object can be used to create a similar object. For example, you have a simplewooden chair and you need to add a headrest to it. You do not need to create a new chair

    because you can easily add a headrest to the existing chair. This is known as resilience to

    change.

    Realistic Modeling

    Because you live in a world of objects, it logically follows that the object-orientedapproach models the real world more accurately. The object-oriented approach allows you

    to identify entities as objects having attributes and behavior. Attributes and behavior

    typically depict how the object acts and reacts. For example, the car is an object belongingto the class Vehicle. The car has attributes such as speed, color, and power. It displays

    behavior such as being stationary, moving slowly, or accelerating.

    Reusability

    In the software industry, using existing classes or objects from other applications saves

    resources spent in recreating the classes from scratch. Consider the following analogy:

    Carcare is a leading car manufacturing company. Carcare manufactures two-seater carsthat are very popular among the consumers aged between 20 and 24. The company has

    decided to enter the market of family segment market and has decided to manufacture a

    four-seater model.

    Carcare can either design a new car for the family segment or convert the two-seatermodel to a four-seater model. The cost involved in designing a new model is very high as

    compared to the cost involved in converting the two-seater model to a four-seater model.

    Characteristics of the Object-Oriented Approach

  • 8/2/2019 07_OOPS_SG_01

    9/24

    Introduction to the Object-Oriented Approach 1.9NIIT

    Just a minute:

    Therefore, the management and the design team may decide to convert the two-seatermodel into a family segment model, as shown in the following figure.

    Example of Reusability

    The preceding scenario depicts the concept of reusability that is supported by theobject-oriented approach. The process of creating a new class by adding some features to

    an existing class is known as inheritance. The benefit of reusability translates to savingsin time and effort, which in turn results in cost benefits.

    State whether the following situations demonstrate reusability:

    1. Recycling paper

    2. Pump reusability (same pump is used in a well and in a fuel station)

    Answer:

    1. IT does not represent reusability because the unusable paper is destroyed before

    paper is recycled for use. The unusable paper loses its identity and cannot be

    considered the same as recycled paper.

    2. It represents reusability because a pump can be used for suction of water as wellas petrol. It is not necessary to use the same pump in both the cases. Two separatemachines can be used because both belong to the Pump class.

    Two-seater Car

    Attributes

    Behavior

    Four-seater Car

    Two-seater Car: AttributesTwo-seater Car: Behavior

    New Attributes

    New Behavior

  • 8/2/2019 07_OOPS_SG_01

    10/24

    1.10 Introduction to the Object-Oriented Approach NIIT

    Resilience to Change

    The object-oriented approach allows systems to evolve. When a change is suggested, theold system need not be completely abandoned. Consider the example of JoyToys, Inc., is

    a company that manufactures toys for children in the age group of 1 to 12. Its car toys are

    popular with children because of their attractive colors, shape, and sound.

    For a couple of years, the company had no cause for complain about the design of the toy

    car. However, due to the advancement in technology and increasing competition, the

    designers now want to stop manufacturing the old car because the market requirementshave changed. They want the company to manufacture a car that has flashing lights and is

    remotely controlled.

    In the object-oriented system, this requirement does not mean that the new car needs to bebuilt from scratch. The new features can be easily incorporated in the old toy car without

    modifying the color, shape, and sound of the old toy car, as shown in the following figure.

    Illustration of Resilience to Change

    Resilience to change also results in easier maintenance. This feature of object-orientedmethodology is known as extensibility. The ability of a class to inherit features from

    another class also makes object-oriented programs more extensible. For the same reason,

    even during construction, parts of the system under development can be refined withoutany major changes to other parts.

    Existence as Different Forms

    Using the object-oriented approach, objects can be made to respond differently to the

    same message. The response is decided based on the information or parameters providedwith the message.

    Old Toy Car

    Color Shape Sound

    New Toy Car

    Color Shape Sound

    Remote Control Flash Lights

  • 8/2/2019 07_OOPS_SG_01

    11/24

    Introduction to the Object-Oriented Approach 1.11NIIT

    NoteInformation is passed as parameters in a function. For a detailed study of functions andparameters, refer to the subsequent chapters.

    If a car collides against an object, the behavior of the car after the collision will depend on

    the speed of the car and nature of the object that hit the car. For example, if the car

    collides with another car at high speed, both the cars will be smashed and the driversmight get injured. On the other hand, if a car collides with a street light at a slow speed,

    the impact would be less. This ability to react differently based on the information

    associated with the message is known aspolymorphism.

  • 8/2/2019 07_OOPS_SG_01

    12/24

    1.12 Introduction to the Object-Oriented Approach NIIT

    Before the actual development of any product, important steps such as analysis and designneed to be performed. The following phases are involved in the object-oriented

    methodology: The Analysis phase

    The Design phase

    The Implementation phase

    Consider an aircraft manufacturing factory where a new jetliner is being assembled and

    several competent engineers and workmen are working with specialized tools. Can youvisualize them working on pure intuition, grabbing the tools, and sitting down to work

    immediately? Or, do you see them working according to a detailed design on which they

    have spent hundreds of hours, and finally constructing the aircraft based on the specified

    design.

    For the safety of the millions of people who fly every day, it is recommended to use a

    detailed design. After all, it is comforting to know that a lot of thought has gone into first

    making a model on paper and then translating the design into the physical jetliner. The

    same thing holds true for almost any item that is constructed. Similarly designers put pen

    to paper before attacking the fabric with scissors. Architects spend hours drawing layoutsof buildings on blueprints before the bricks are laid.

    Therefore the construction of software follows the same approach. The software industrystill relies mainly on the informal paper-and-pencil approach in the upstream development

    phases.

    It is this paper-and-pencil approach that is termed analysis and design. To analyze anddesign a system, you need to build a model of the system. This model is simpler than the

    system that is finally constructed. All the practical aspects of building a system for the

    real world cannot be reflected in the design. However, this does not undermine theimportance of design.

    The Analysis Phase

    The purpose of analysis is to provide a description of a problem. The description mustbe complete, consistent, readable, and reviewable by diverse interested parties, andtestable against reality.- Mellor

    The analysis or the Object-Oriented Analysis (OOA) phase considers the system as asolution to a problem in its environment or domain. Broadly, analysis is the phase where

    users and developers of the system get together and arrive at a common understanding of

    Phases of Object-Oriented Methodology

  • 8/2/2019 07_OOPS_SG_01

    13/24

    Introduction to the Object-Oriented Approach 1.13NIIT

    the system. One of the end products of the analysis phase is the specification on how a

    system functions.

    In the analysis phase, the developer concentrates on obtaining as much information as a

    possible about the problem. The developer has to identify the critical requirements.

    Analysis, therefore, involves meeting and interviewing the concerned persons tounderstand systems that are currently in operation, and identifying all the causes of

    problems (if any) in the current system.

    The Design Phase

    In the design phase, the developers of the system document their understanding of thesystem. Design generates the blueprint of the system that is to be implemented.

    The first step in creating an Object-Oriented Design (OOD) is the identification of classes

    and their inter-relationships.

    Work in the design phase of software development is comparable to the work of anarchitect. The architect will need to create a blueprint or a model of the building before

    the construction of the building starts.

    The Implementation Phase

    The design phase is followed by OOP, which is the implementation phase. OOP provides

    specifications for writing programs in a programming language. One of the most popular

    languages used to write object-oriented programs is C#.

    During the implementation phase, programming is done as per the requirements gatheredduring the analysis and design phases.

    Many of the modern applications are built by using OOP. Developing complex,large-scale business systems can be simplified by using OOP techniques. Some of the

    applications that can be built by using OOP techniques are Computer Aided Design

    (CAD), Computer Aided Manufacturing (CAM), and Object-Oriented Databases.

  • 8/2/2019 07_OOPS_SG_01

    14/24

  • 8/2/2019 07_OOPS_SG_01

    15/24

    Introduction to the Object-Oriented Approach 1.15NIIT

    Computer languages have come a long way since the 1940s. Back then, scientists punchedinstructions into mammoth, room-sized computer systems. These instructions were long

    series of zeroes and ones. These machine language instructions are called the FirstGeneration of computer languages.

    The 1950s saw the emergence of the Second Generation of computer

    languages assembly language - easier to write than machine language but still extremelycomplicated for a common man. However, the computer could still understand only

    machine language. Therefore, the Assembler software was developed to translate the code

    written in assembly language into machine language.

    In 1967, Martin Richard developed a language called BPCL for writing operatingsystems. An operating system is a set of programs that manages the resources of a

    computer and its interactions with users. The era of the Third Generation of computerlanguages had arrived. In 1970, Ken Thompson modified BPCL to create a new language

    called B. While working for Bell Laboratories, Thompson teamed up with Dennis Ritchie

    and wrote an early version of the Unix operating system for a DEC PDP-7 computer.

    Dennis Ritchie was working on a project to further develop the Unix operating system. He

    wanted a low-level language, like the assembly language, that could control hardwareefficiently. At the same time, he wanted the language to provide the features of a

    high-level language, that is, it should be able to run on different types of hardware. B had

    performance drawbacks, so in 1972 he rewrote B and called it C. Therefore, C is

    categorized as both a Second and a Third Generation language. Thompson and Ritchierewrote the Unix operating system in C. In the years that followed, C was widely

    accepted and used over different hardware platforms. This led to many variations of C. In

    1989, theAmerican National Standards Institute (ANSI), along with theInternationalStandards Organization (ISO), approved a machine-independent and standard version of

    C.

    In the early 1980s, Bjarne Stroustrup of Bell Labs developed the C++ language. In hisown words, "C++ was designed primarily so that my friends and I would not have to

    program in assembly, C, or various modern high-level languages. Its main purpose was to

    make writing good programs easier and more pleasant for the individual programmer."

    C++ was originally known as 'C with classes' because two languages contributed to its

    design: C, which provided low-level features, and Simula67, which provided the classconcept. C++ is an object-oriented language. Other object-oriented languages are Java,

    Smalltalk, and C#.

    C#, also known as C-Sharp, is a programming language introduced by Microsoft. C#contains features similar to Java and C++. It is specially designed to work with

    Microsofts .NET platform.

    Introducing C#

  • 8/2/2019 07_OOPS_SG_01

    16/24

    1.16 Introduction to the Object-Oriented Approach NIIT

    Note

    The .NET platform is aimed at providing Internet users with Web-enabled interface forapplications and computing devices such as mobile phones. It also provides developers

    the ability to create reusable modules, thereby increasing productivity.

    Compilers

    All languages have a vocabulary, which is a list of words that have a specific meaning inthat language. Languages also have their own grammar rules, which state the rules for

    combining words to form sentences. This is what ensures that whatever is spoken in a

    particular language is interpreted similarly by all people who understand the language.Similarly, programming languages also have a vocabulary, which is referred to as the set

    of keywords of that language, and a grammar, which is referred to as the syntax.

    Does this mean that computers can directly interpret the instructions written in anyprogramming language? No, they cannot. Then, how do you ensure that a computer

    executes the instructions from a program as intended? What would you do if you got hold

    of a recipe for a delicious dish written in a foreign language that you do not understand?You would get it translated to English or any other language you understand to be able to

    prepare the dish. Similarly, you need a translator to convert the instructions written in a

    programming language to machine language.

    A compileris a special program that processes the statements written in a particularprogramming language and converts them into machine language. Like everything else in

    the computer, the compiler also follows the Input-Process-Output (I-P-O) cycle. It takesthe programming language instructions as input. It processes these instructions to convert

    them to machine language. These instructions can then be executed by the computer. This

    process of conversion is called compilation. For each programming language, there is adifferent compiler available. For example, for compiling a program written in the C

    language, you require a C compiler. For a Java program, you require a Java compiler. For

    C# programs, you will use the csc compiler.

    You will now learn to create classes in the C# language.

  • 8/2/2019 07_OOPS_SG_01

    17/24

    Introduction to the Object-Oriented Approach 1.17NIIT

    Consider the following code example, which defines a class:

    public class Hello{

    public static void Main(string[] args)

    {

    System.Console.WriteLine("Hello, World! \n");

    }

    }

    The preceding class declaration provides a method Main() that will display the message

    Hello, World! on your screen. The parts of the preceding code need to be examined.

    The Main() FunctionThe first line of code that a C# compiler looks for in the source file compiled is the

    Main() function. This function is the entry point of the application.

    The Main() function is ideally used to create objects and invoke member functions.

    The class Keyword

    The class keyword is used to declare a class. Keywords are reserved words that have a

    special meaning. Here, the class keyword defines the class Hello. The braces, known asdelimiters, are used to indicate the start and end of a class body.

    Example:

    class Hello

    {

    ...

    }

    The Class Name

    The class keyword is followed by the name of the class. In the preceding example,

    Hello is the name of the class defined by using the class keyword. When you createclasses, you must consider the following naming conventions and rules.

    Classes in C#

  • 8/2/2019 07_OOPS_SG_01

    18/24

    1.18 Introduction to the Object-Oriented Approach NIIT

    Class Naming Conventions in C#

    Class names should follow certain naming conventions or guidelines. A class name:

    Should be meaningful (strongly recommended). Should ideally be a noun.

    Can use either the Pascal case orCamel case. In Pascal case the first letter is

    capitalized and the rest of the letters are in lower case, such as Myclass. In Camel

    case the first letter is in lower case and the first letter of each subsequent word iscapitalized, such as myClass.

    Rules for Naming Classes in C#

    Name of classes:

    Must begin with a letter. This letter may be followed by a sequence of letters, digits(0-9), or _. The first character in a class name cannot be a digit.

    Must not contain any embedded space or symbol like ? - + ! @ # % ^ & * ( ) [ ] { } .

    , ; : " ' / and \. However, an underscore (_) can be used wherever a space is

    required.

    Must not use a keyword for a class name. For example, you cannot declare a class

    called public.

    System.Console.WriteLine()Console is a class that belongs to the System namespace. A namespace is a collection of

    classes. The System namespace contains the method WriteLine(), which displays the

    enclosed text on the screen. The Console class has other methods, which are used forvarious input/output operations. The character (.) is used to access the function,

    WriteLine(), which is coded in the Console class of the System namespace.

    The preceding line can also be written as Console.WriteLine() if the statement using

    System is included as the first line of code.

    The following code is an example ofConsole.WriteLine():

    Console.WriteLine("Hello World \n");

    The preceding code will display on the screen.

    Hello World

  • 8/2/2019 07_OOPS_SG_01

    19/24

    Introduction to the Object-Oriented Approach 1.19NIIT

    The Escape Characters

    To display special characters such as the New line character or the backspace character,

    you need to use escape characters.

    The following table lists the escape characters used in C#.

    Escape sequence Character name

    \ Single quotation mark

    \ Double quotation mark

    \\ Backslash

    \0 NULL

    \a Alert

    \b Backspace

    \f Form feed

    \n New line

    \r Carriage return

    \t Horizontal tab

    \v Vertical tab

    The Escape Characters

    Example of the New line escape sequence

    Console.WriteLine("Hello \n World");

    The preceding code will display the following message on the screen:

    Hello

    World

  • 8/2/2019 07_OOPS_SG_01

    20/24

    1.20 Introduction to the Object-Oriented Approach NIIT

    Problem Statement

    As a member of a team that is developing toys for JoyToys, Inc., you have been assigned

    the task of creating a bike module that accepts and displays bike details. Declare the Bikeclass and its member functions. The member function that accepts bike details should

    display the message Accepting Bike Details. Similarly, the member function to display

    bike details on the screen should display the message Displaying Bike Details.

    Solution

    The following code will create the Bike class with the required functions:

    class Bike

    {

    void AcceptBikeDetails(){

    Console.WriteLine("Accepting Bike Details \n");

    }

    void DisplayBikeDetails()

    {

    Console.WriteLine("Diplaying Bike Details \n");

    }}

    Activity: Creating Classes

  • 8/2/2019 07_OOPS_SG_01

    21/24

    Introduction to the Object-Oriented Approach 1.21NIIT

    1. Which of the following is not a class?

    a. A Ford Ikon car with the registration number XXXX

    b. Fruitc. Mammal

    d. Fish

    2. Which method displays the message Hello People on the screen?

    a. Console.WriteLine("Hello People");

    b. System.WriteLine("Hello People");

    c. Console("Hello People") ;

    d. Console.writeline (Hello People) ;

    3. Console is a ________.a. Namespace

    b. Classc. Function

    d. Escape sequence character

    4. In a C# program, which is the first function to be executed?

    a. Main()

    b. main()c. Console.WriteLine()

    d. void Accept_bike_details()

    5. Which of the following is used to denote a newline character?

    a. \bb. \n

    c. \v

    d. /n

    Practice Questions

  • 8/2/2019 07_OOPS_SG_01

    22/24

    1.22 Introduction to the Object-Oriented Approach NIIT

    In this chapter, you learned that:

    According to the object-oriented approach, systems consist of component objects

    that interact with each other.

    An object is an entity that may have a physical boundary. However, it should havethe following characteristics:

    State

    Behavior

    Identity

    A class consists of a set of objects that share a common structure and behavior.

    If an object desires an action from another object, it sends a message to that object.

    The object that receives the message is called the receiver, and the set of actionstaken by the receiver constitutes the method.

    The features of the object-oriented approach are:

    Realistic modeling

    Binding attributes and behavior together

    Reusability

    Resilience to change

    Existence as different forms

    A model of a system is built in the stages of analysis and design.

    The purpose of the model is to help developers understand the reality that they aretrying to imitate.

    In C#, a class is created by using the keyword class. It is identified by a name called

    the class name.

    The Console.WriteLine() method is used to display text on the screen.

    Main() is the first function which is executed in a C# program.

    Escape characters are used to display special characters such as the newline

    character.

    Summary

  • 8/2/2019 07_OOPS_SG_01

    23/24

    Introduction to the Object-Oriented Approach 1.23NIIT

    Exercise 1

    Analyze the process of making a call by using a pay phone. Identify the objects involved

    and the behavior of these objects. Also, identify the messages passed between the objectsfor interacting with each other.

    Exercise 2

    As a member of a team that is developing an automated ranking system of players in

    video game parlors, you have been assigned the task of creating a module that accepts the

    details of the player after a game is over. Declare a class Player, which consists of three

    member functions: AcceptPlayerDetail(), ComparePlayerDetail(), andPrintPlayerRank(). Each of these functions should print the appropriate message on the

    screen.

    Exercises

  • 8/2/2019 07_OOPS_SG_01

    24/24