You are on page 1of 45

Object-Oriented Programming Using C#

©ationale

Object-oriented concepts form the base of all modern


programming languages. Understanding the basic concepts
of object-orientation helps a developer to use various
modern day programming languages, more effectively. C#
(C-Sharp) is an object-oriented programming language
developed by Microsoft that intends to be a simple, modern,
and general-purpose programming language for application
development.
The course is applicable to students who want to enter the
world of object-oriented programming, using the C#
language. This course provides a strong foundation in
object-oriented programming approaches and the
fundamentals of C# programming language.

  c

  c  
Object-Oriented Programming Using C#
Objectives

Àn this session, you will learn to:


Explain features of the object-oriented methodology
Describe the phases of the object-oriented methodology
Define classes in C#
Declare variables
Write and execute C# programs

  c

  c  
Object-Oriented Programming Using C#
Object-Oriented Methodology

Object orientation is a software development methodology


that is based on modeling a real-world system.
An object oriented program consists of classes and objects.

Let us understand the terms²class and objects

  c

  c  
Object-Oriented Programming Using C#
Object-Oriented Methodology (Contd.)

 

Car

Suzuki Reno Toyota Camry Honda Acura

Obj

  c

  c  
Object-Oriented Programming Using C#
he Foundation of Object Orientation

An object means a µmaterial thing¶ that is capable of being


presented to the senses.
An object has the following characteristics:
Àt has a state
Àt may display behavior
Àt has a unique identity
Objects interact with other objects through messages.

Let us understand these concepts.

  c

  c  
Object-Oriented Programming Using C#
he Foundation of Object Orientation (Contd.)
 
    

c

   





  b  




! "

  c

  c  
Object-Oriented Programming Using C#
he Foundation of Object Orientation (Contd.)

Car is flashing the lights to pass


the message to the other car

  c

  c # 
Object-Oriented Programming Using C#
Just a minute

Àdentify the possible states of the following objects:


1. A cell phone
2. A stereo

Solution:
1. States of a cell phone: Off, Ring, Vibrate, and Call
2. States of a stereo: Play, Pause, Rewind, and Forward

  c

  c $ 
Object-Oriented Programming Using C#
Just a minute

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 tickets
for the Flying Express in the first class. Àdentify the following:
1. The possible receiver of the message in this situation.
2. The possible method that the receiver can use.

Solution:
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 the desired class and for the desired date. Àf
the tickets are available, the clerk will enter the details (name,
age, departure date, and seat), confirm the reservation, and
collect the required fare.
  c

  c % 
Object-Oriented Programming Using C#
Characteristics of the Object-Oriented Approach

Realistic modeling
Reusability
Resilience to change
Existence as different forms

  c

  c  
Object-Oriented Programming Using C#
Just a minute

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)

Solution:
1. Àt 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. Àt represents reusability because a pump can be used for
suction of water as well as petrol. Àt is not necessary to use
the same pump in both the cases. Two separate machines
can be used because both belong to the µPump¶ class.

  c

  c  
Object-Oriented Programming Using C#
Phases of Object Orientation

The following phases are involved in the software


development:
The Analysis phase
The Design phase
The Àmplementation phase

Let us discuss the process of constructing a building.

  c

  c  
Object-Oriented Programming Using C#
Just a minute

As a member of a team that is developing software for


DialCom Telecommunications, Ànc., you have been
assigned the task of creating a software module that
accepts and displays customer details such as name, age,
and phone number. Àdentify the class that you will create
and the methods of the class.

Solution:
As per the problem statement, the class required is:
Customer
The class should have the methods to:
Accept customer details
Display customer details

  c

  c  
Object-Oriented Programming Using C#
Àntroducing C#

A program is a set of instructions to perform a specific task.


Programming languages use programs to develop software
applications.
A compiler is a special program that processes the
statements written in a particular programming language
and converts them into a machine language.
This process of conversion is called compilation.

  c

  c  
Object-Oriented Programming Using C#
Àntroducing C# (Contd.)

C#, also known as C-Sharp, is a programming language


introduced by Microsoft.
C# is specially designed to work with the Microsoft¶s .NET
platform.

Let us understand the structure of a C# program.

  c

  c  
Object-Oriented Programming Using C#
Classes in C#

Consider the following code example, which defines a class:


à  


à   
 

 

    

 
!"
#
#

  c

  c  
Object-Oriented Programming Using C#
Classes in C# (Contd.)

à   
 

&"  
Às used to declare a
à   
 class
 

 

   


 !"
#
#

  c

  c # 
Object-Oriented Programming Using C#
Classes in C# (Contd.)

à  
 

'
Às used as an identifier
à   
 for a class
 

 

   


 !"
#
#

  c

  c $ 
Object-Oriented Programming Using C#
Classes in C# (Contd.)

à  
 ()* 
Às the entry point of an
à   
 application
 
Às used to create

objects and invoke
 

    member functions


 !"
#
#

  c

  c % 
Object-Oriented Programming Using C#
Classes in C# (Contd.)

à  
c"

 + , ()
Displays the enclosed text on
à   
 the screen
 

 

   


 !"
#
#

  c

  c  
Object-Oriented Programming Using C#
Classes in C# (Contd.)

à  
-
 
Displays New line
à   
 character. Other special
  characters can also be
displayed such as \t, \b
 

    and \r


 !"
#
#

  c

  c  
Object-Oriented Programming Using C#
Demo: Creating Classes

Problem Statement:
As a member of a team that is developing toys for JoyToys,
Ànc., you have been assigned the task of creating a bike
module that accepts and displays bike details. Declare the
Bike class 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´.

  c

  c  
Object-Oriented Programming Using C#
Declaring Variables

A variable is a location in the memory that has a name and


contains a value.
A variable is associated with a data type that defines the
type of data that can be stored in a variable.

  c

  c  
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables

†ou can declare and initialize variables by using the


following syntax:
´ $ Ã %´  $ %&´  %"

  c

  c  
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables (Contd.)

Consider the following 3"


 .
example of declaring and Represents the kind of
initializing a variable:
data stored in a variable
 $'&("
C# provides you with
various built-in data
types, such as:
‡ char
‡ int
‡ float
‡ double
‡ bool
‡ string

  c

  c  
Object-Oriented Programming Using C#
Data ypes in C#

Let us now understand the various data types with the help
of examples.

  c

  c  
Object-Oriented Programming Using C#
Data ypes in C# (Contd.)

string Name = ³Peter´

float Marks = 83.56


int Age = 23
char Vowel = µa¶

  c

  c # 
Object-Oriented Programming Using C#
Data ypes in C# (Contd.)

Consider the following 3"


 .
example of declaring and The following types of
initializing a variable: data types are supported
 $'&(" by C#:
‡ Value types

Ñ  Ñ
 
Ñ  

 
 

  
    

{ 
    

  c

  c $ 
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables

Consider the following 3"


 .
example of declaring and The following types of
initializing a variable: data types are supported
 $'&(" by C#:
‡ Reference types

„   

# „„  

 ! ! "



{ 
 
 
    

  c

  c % 
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables (Contd.)

Consider the following ' / b 


 .
example of declaring and The following rules are used
initializing a variable: for naming variables in C#:
 $'&(" ‡ Must begin with a letter or
an underscore
‡ Should not contain any
embedded spaces or
symbols
‡ Must be unique
‡ Can have any number of
characters
‡ Keywords cannot be used
as variable names

  c

  c  
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables (Contd.)

Consider the following -0 


   10 

example of declaring and ' /  b 

initializing a variable:
Name
 $'&("
#Score

Age

2Strank

Family_Size

Gender

  c

  c  
Object-Oriented Programming Using C#
Declaring and Ànitializing Variables (Contd.)

Consider the !  2 /  b 


 .
following example of Specifies the value that
declaring and needs to be stored in a
initializing a variable: variable. The value could be
 $'&(" an integer, a decimal, or a
character.

  c

  c  
Object-Oriented Programming Using C#
Accepting and Storing Values in Member Variables

To understand how to accept 


 3, ()
value in a variable, let us Às used to accept input
consider the following code from the user and store it
snippet: in the variable
 )  "
)  &

  *
+ ,(

 - 
 "

  c

  c  
Object-Oriented Programming Using C#
Accepting and Storing Values in Member Variables (Contd.)

To understand how to accept   ! ()


value in a variable, let us Converts the value
consider the following code entered by the user to
snippet: the int data type
 )  "
)  &

  *
+ ,(

 - 
 "

  c

  c  
Object-Oriented Programming Using C#
Jriting and Executing a C# Program

Let us know learn to write, compile, and execute a C#


program.

  c

  c  
Object-Oriented Programming Using C#
Creating a Sample C# Program

A C# program can be written by using an editor like Notepad.


Consider the following code, which declares a class Car and
also creates an object MyCar of the same class:
  " The using keyword is used to include
 the namespaces in the program.

Comments are used to explain the
..     code and are represented by //
 / " symbols.
 )
012 " Member variables are used to store
..  1  
 the data for a class.

3 Ã 4 
Member functions are declared inside
the class that are used to perform a
specific task.

  c

  c  
Object-Oriented Programming Using C#
Creating a Sample C# Program (Contd.)




   /  2 / 
 "
/ &

 -  "


   /  2   
12 "
)
012 &

  *
+ ,(

 -  "
#
à 
4Ã4 



   *2 / 
 5 6#
/ "


   *2   
172 
 5 6#)
012 "
#
#

  c

  c # 
Object-Oriented Programming Using C#
Creating a Sample C# Program (Contd.)

..  
    2 
/8   The Execute class is used as a class from
where the Car class can be instantiated.
à   
 

& 7"
3 Ã 4 "
4Ã4 "
#
#

  c

  c $ 
Object-Oriented Programming Using C#
Compiling and Executing C# Program

After writing the program in a Notepad, you need to compile


and execute it to get the desired output.
The compiler converts the source code that you write into
the machine code, which the computer can understand.
The following steps are needed to compile and execute a
C# program.
1. Save the code written in the Notepad with an extension .
2. To compile the code, you need to go to the 
 c
  4  window. Select c la 
4 / 
l 
 
 c l 
 c

l 
 c  4 The 
 
c  4  window is displayed to
compile the program.
3. Àn the 
 c  4  window, move
to the location where the programs file is saved.

  c

  c % 
Object-Oriented Programming Using C#
Compiling and Executing C# Program (Contd.)

V. Compile the program file by using the following command:


/8  
5. To execute the code, type the following in the command
prompt:
/8   8

  c

  c  
Object-Oriented Programming Using C#
Demo: Creating a C# Program

Problem Statement:
David is the member of a team that is developing the
Automatic Ranking software for a tennis tournament. †ou have
been assigned the task of creating a program. The program
should accept the following details of a tennis player and
display it:
Name, containing a maximum of 25 characters
Rank as an integer
Winning average as a decimal value
Help David to create the program.

  c

  c  
Object-Oriented Programming Using C#
Summary

Àn this session, 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 have the following characteristics:
State
Behavior
Àdentity
A class consists of a set of objects that share a common
structure and behavior.
Àf 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 actions taken by the receiver constitutes the
method.

  c

  c  
Object-Oriented Programming Using C#
Summary (Contd.)

The features of the object-oriented approach are:


Realistic modeling
Reusability
Resilience to change
Existence as different forms
A model of a system is built in the stages of analysis, design
and implementation.
The purpose of the model is to help developers understand the
reality that they are trying to imitate.
Àn C#, a class is created by using the keyword class. Àt 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.

  c

  c  
Object-Oriented Programming Using C#
Summary (Contd.)

Escape characters are used to display special characters such


as the newline character.
A variable is a named location in the memory, which contains a
specific value.
A datatype defines the type of data that can be stored in a
variable.
The two types of data type are Value type and Reference type.
The ReadLine() method is used to accept inputs from the
user.
The using keyword is used to include the namespaces in the
program.
A namespace contains a set of related classes.
Member variables are declared inside the class body.

  c

  c  
Object-Oriented Programming Using C#
Summary (Contd.)

Comment entries are notes written by a programmer in the


code so that others reading that code can understand it better.
An object is an instance of a class.
The compiler software translates a program written in a
language like C# into the machine language.

  c

  c  

You might also like