OOPROGR - Module 2 (Modular)
OOPROGR - Module 2 (Modular)
OOPROGR
Object Oriented Program
MODULE 2
DATA TYPES, OPERATORS
& EXPRESSIONS
1
MODULE 2
DATA TYPES, OPERATORS & EXPRESSIONS
INTRODUCTION
Generally, we use languages like English, Hindi, etc., to make communication between two
persons. That means when we want to make communication between two persons we need a
language through which persons can express their feelings. Similarly, when we want to make
communication between user and computer or between two or more computers we need a
language through which user can give information to the computer and vice-versa. When a user
wants to give any instruction to the computer the user needs a specific language and that
language is known as a computer language.
Computer languages are the languages through which the user can communicate with the
computer by writing program instructions.
Over the years, computer languages have been evolved from Low-Level to High-Level Languages.
In the earliest days of computers, only Binary Language was used to write programs. The
computer languages are classified as follows:
2
1. Low-Level Language (Machine Language)
Low-Level language is the only language which can be understood by the computer. Binary
Language is an example of a low-level language. Low-level language is also known as
Machine Language. The binary language contains only two symbols 1 & 0. All the
instructions of binary language are written in the form of binary numbers 1's & 0's. A
computer can directly understand the binary language. Machine language is also known as
the Machine Code.
As the CPU directly understands the binary language instructions, it does not require any
translator. CPU directly starts executing the binary language instructions and takes very
less time to execute the instructions as it does not require any translation. Low-level
language is considered as the First Generation Language (1GL).
Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters. Assembly language is an example of
middle-level language. In assembly language, we use predefined words called mnemonics.
Binary code instructions in low-level language are replaced with mnemonics and operands
in middle-level language. But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into binary language. Assembler is a
translator which takes assembly code as input and produces machine code as output. That
means, the computer cannot understand middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer.
Assembler is used to translate middle-level language into low-level language.
3. High-Level Language
A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules that
are used to make instructions more easily. Every high-level language has a set of
predefined words known as Keywords and a set of rules known as Syntax to create
instructions. The high-level language is easier to understand for the users but the computer
can not understand it. High-level language needs to be converted into the low-level
language to make it understandable by the computer. We use Compiler or interpreter to
convert high-level language to low-level language.
1. Procedural - available instructions are used to create self-contained units called procedures
such as:
ALGOL
BASIC
COBOL
C
Fortran
3
Pascal
2. Object-oriented - reusable objects, containing code and data, are manipulated such as:
C++
Java
Python
Ruby
VB.NET
C#
.NET Framework is a technology that supports building and running Windows apps and web
services. .NET Framework is designed to fulfill the following objectives:
Not an operating system;
An environment in which programs run;
4
Resides at a layer between operating system and other applications;
Offers multi-language independence;
One application can be written in more than one language.
Includes over 2,500 reusable types (classes);
Enables creation of dynamic Web pages and Web services; and
Scalable component development.
Why C#?
One of the newer programming languages;
Conforms closely to C and C++;
Has the rapid graphical user interface (GUI) features of previous versions of Visual Basic;
Has the added power of C++;
Has the object-oriented class libraries similar to Java; and
Can be used to develop a number of applications.
5
- Software components;
- Mobile applications;
- Dynamic Web pages;
- Database access components;
- Windows desktop applications;
- Web services; and
- Console-based applications.
C# Relationship to .NET
Many compilers targeting the .NET platform are available;
C# was used most heavily for development of the .NET Framework class libraries;
C#, in conjunction with the .NET Framework classes, offers an exciting vehicle to
incorporate and use emerging Web standards;
C# is object-oriented; and
In 2001, the European Computer Manufacturers Association (ECMA) General Assembly
ratified C# and its common language infrastructure (CLI) specifications into international
standards.
Web Applications
6
Can quickly build applications that run on the Web with C#.
- Using Web Forms: part of ASP.NET
Windows Applications
Applications designed for the desktop;
Designed for a single platform;
Use classes from System.Windows.Form;
Applications can include menus, pictures, drop-down controls, buttons, text boxes, and
labels; and
Use drag-and-drop feature of Visual Studio.
Console Applications
Normally send requests to the operating system;
Display text on the command console; and
Easiest to create.
- Simplest approach to learning software development; and
- Minimal overhead for input and output of data.
7
Example 1: First C# Program
Console-based
application output
Elements of a C# Program
• Comments
✔ line 1 // This is traditionally the first program written;
✔ Like making a note to yourself or readers of your program;
✔ Not considered instructions to the computer;
✔ Not checked for rule violations ; and
✔ Document what the program statements are doing.
Comments
Make the code more readable; and
Three types of commenting syntax.
✔ Inline comments;
✔ Multiline comments; and
✔ XML documentation comments.
Inline Comments
8
Indicated by two forward slashes (//);
Considered a one-line comment;
Everything to the right of the slashes ignored by the compiler; and
Carriage return (Enter) ends the comment.
Multi-line Comments
Forward slash followed by an asterisk (/*) marks the beginning
Opposite pattern (*/) marks the end
Also called block comments
/* This is the beginning of a block multiline comment. It can go on for several lines or just
be on a single line. No additional symbols are needed after the beginning two characters.
Notice there is no space placed between the two characters. To end the comment, use
the following symbols. */
Using Directive
Permits use of classes found in specific namespaces without having to qualify them;
Framework class library; and
- Over 2,000 classes included.
Syntax.
- using namespace Identifier.
Namespace
Namespaces provide scope for the names defined within the group;
- Captain example.
Groups semantically related types under a single umbrella;
System: most important and frequently used namespace; and
Can define your own namespace.
- Each namespace enclosed in curly braces: { }
Predefined namespace
(System) – part of .NET 9
From Example 1-1 FCL
Class Definition
• Building block of object-oriented program;
• Everything in C# is designed around a class;
• Every program must have at least one class;
• Classes define a category, or type, of object; and
• Every class is named.
10
- Stores values associated with the state of the class
✔ Include method members
- Performs some behavior of the class
• Can call predefined classes’ methods.
✔ Main( )
Main( ) Method
• “Entry point” for all applications;
✔ Where the program begins execution; and
✔ Execution ends after last statement in Main( ).
• Can be placed anywhere inside the class definition;
• Applications must have one Main( ) method; and
• Begins with uppercase character.
Method Calls
line 9 Console.WriteLine(“Hello World!”);
• Program statements
• WriteLine( ) → member of the Console class
• Main( ) invoking WriteLine( ) method
• Member of Console class
• Method call ends in semicolon
Program Statements
• Write ( ) → Member of Console class
11
✔ Argument(s) enclosed in double quotes inside ( )
✔ “Hello World!” is the method’s argument
✔ “Hello World!” is string argument
- String of characters
• May be called with or without arguments
✔ Console.WriteLine( );
✔ Console.WriteLine(“WriteLine( ) is a method.”);
✔ Console.Write(“Main( ) is a method.”);
• Read( ) accepts one character from the input device;
• ReadLine( ) accepts string of characters from the input device;
✔ Until the enter key is pressed.
• Write( ) does not automatically advance to next line; and
• Write(“An example\n”).
✔ Same as WriteLine(“An example”); and
✔ Includes special escape sequences.
C# Elements
12
• Create new project; and
• Includes special escape sequences.
- Select New Project on the Start page; and
- OR use File → New Project option.
13
• Change the name of the class and the source code filename.
✔ Use the Solution Explorer Window to change the source code filename.
- Select View → Solution Explorer
14
Figure 1.9 - Execution of an application using Visual Studio
Debugging an Application
• Types of errors
✔ Syntax errors
- Typing error
- Misspelled name
- Forget to end a statement with a semicolon
✔ Run-time errors
- Failing to fully understand the problem
- More difficult to detect
Error Listing
15
REFERENCES:
3. Doyle, B. (2015). C# Programming: From Problem Analysis to Program Design, 4th Edition,
Cengage Learning.
16