You are on page 1of 32

Introduction to Object Oriented

Programming
(Lecture 2)

1
Programming Languages
• Programming languages allow programmers to code software.
• The three major families of languages are:
o Machine languages
o Assembly languages
o High-Level languages
Machine Languages
• Comprised of 1s and 0s
• The “native” language of a computer
• Difficult to program – one misplaced 1 or 0 will cause the
program to fail.
• Example of code:
1110100010101 111010101110
10111010110100 10100011110111
Assembly Languages
• Assembly languages are a step towards easier
programming.
• Assembly languages are comprised of a set of
elemental commands which are tied to a specific
processor.
• Assembly language code needs to be translated to
machine language before the computer processes it.
• Example:
ADD 1001010, 1011010
High-Level Languages
• High-level languages represent a giant leap towards
easier programming.

• The syntax of HL languages is similar to English.

• Historically, we divide HL languages into two groups:


o Procedural languages
o Object-Oriented languages (OOP)
Procedural Languages
• Early high-level languages are typically called procedural
languages.

• Procedural languages are characterized by sequential sets of


linear commands. The focus of such languages is on structure.

• Examples include C, COBOL, Fortran, LISP, Perl, HTML,


VBScript
What is object-oriented programming?

Object-oriented programming (OOP) refers to a type of computer


programming (software design) in which programmers define
new data types along with their operations (functions).

Traditional data types: int, float, char


Complex data structures: Address

OR

Object-oriented programming (OOP) is a programming language


model organized around objects rather than "actions" and data

7
What is object-oriented programming?

In old style programming, you had:


• Data (variables) , which was completely passive
• Functions, which could manipulate any data

An object contains both data and methods that


manipulate that data.

8
Procedural Appraoch
static void Main(string[] args){

int a =10;
int b = 20;

int sum(){
return a + b;
}

9
object-oriented approach
static void Main(string[] args){

calculator cal = new calculator();


cal.a = 10;
cal.b = 20;

cal.sum();
}

Class calculator{
public int a;
public int b;

public int sum(){


return this.a + this.b;
}
}

10
Class Syntex

A class is also a value that can be in an expression


position

class $
attr
AttrName1
:
AttrNamen
method Pattern Statement end
:
method Pattern Statement end
end
11
Defining a class

The classes contain some fields or characters and


actions or methods.

For Example: Class CAT has characters color, age, size


and performs action eat, sleep, run

How to write CAT class?

12
Test

Write down a CALCULATOR class which performs four operations


(addition, deletion, subtraction and division) with two values.
Class calculator{ Public getNum2(){
Private float num1; return this.num2;
Private float num2; }

Public setNum1(float a){ Public float add(){


this.num1 = a; return this.num1 + this.num2;
} }
Public getNum1(){
return this.num1;
} Public float subtract(){
Public setNum2(float b){ return this.num1 - this.num2;
this.num2 = b; }
} ….

13
Test

Write down a STUDENT class?


Hint: Before writing code identify characters or fields and methods

Class STUDENT {
private int age;
private string rollNo;
private string subject;
private string date;
public void setAge(int a){
this.age = a;
}
public int getAge(){
return this.age;
}
…..
}
14
this keyword
• The this keyword is a predefined variable available
in non-static function members
o Used to access data and function members unambiguously

public class Person {


private string name;
public Person(string name) {
this.name = name;
}
public void Introduce(Person p) {
if (p != this)
Console.WriteLine(“Hi, I’m “ + name);
}
}
What is object-oriented programming?

Object
• An entity with unique identity that encapsulate state
• state can be accessed in a controlled way from outside
• The access is provided by means of methods (procedures that can directly
access the internal state)
Class
• The class defines all the common properties of the different objects that
belong to it.
• A category of objects.

16
Classes and Objects
• A class is a data type that allows programmers to create
objects. A class provides a definition for an object, describing
an object’s attributes (data) and methods (operations).

• An object is an instance of a class. With one class, you can


have as many objects as required.

• This is analogous to a variable and a data type, the class is the


data type and the object is the variable.

For example: int a;


Instance of a class
Class Calculator{
public int a;
public int b;
Public int sum(){
return this.a + this.b;
}
}
Calculator cal1 = new Calculator();
Calculator cal2 = new Calculator();
18
Objects or instances contain

• Attributes or fields
• State
• procedures or methods

19
Fields
• A field or member variable holds data for a
class or struct
• Can hold:
o A built-in value type
o A class instance (a reference)
o A struct instance (actual data)
o An array of class or struct instances
(an array is actually a reference)
o An event
Methods
• All code executes in a method
o Constructors, destructors and operators are special types of methods
o Properties and indexers are implemented with get/set methods

• Methods have argument lists


• Methods contain statements
• Methods can return a value
Example of object-oriented programming?

Class calculator{
public int a; //Attribute
public int b; //Attribute

public int sum(){ // Function


return this.a + this.b;
}
}

calculator cal = new calculator();


cal.a = 10; //State
cal.b = 20; //State
22
Object Oriented Concepts
• Object – Unique programming entity that has methods, has
attributes and have a state.
• Method – Things which an object can do; the “verbs” of
objects. In code, usually can be identified by an “action” word
-- Hide, Show
• Attribute / characters – Things which describe an object; the
“adjectives” of objects. In code, usually can be identified by a
“descriptive” word – Enabled, BackColor
Object Oriented Concepts
• Class – Provides a way to create new objects based on a
“meta-definition” of an object (Example: The automobile
class)

• Constructors – Special methods used to create new instances


of a class. Constructor of a class is a special method, which
does not have a return type, has the name of the class and is
called using the keyword new. The task of the constructor is to
initialize the memory, allocated for the object, where its fields
will be stored
Calling a constructor:
Calculator cal = new Calculator();
Example of object-oriented programming?

Class calculator{
private int a;
private int b;
public calculator(int x, int y){ //constructor
this.a = x;
this.b = y;
}
public int sum(){
return this.a + this.b;
}
}

calculator cal = new calculator(10, 20);


25
Constructors
Constructor allocates memory for new object resets its fields to
some values and at the end returns a reference to the newly
created object.
For example:
calculator cal = new calculator();

cal Object

10 a: int

20 b: int
Best Practices for classes
1- Use get and set functions for accessing attribute values
2- Set default values through constructors
Parameters
Parameters are used to pass values or references

Static void main(){


int number =0;
setValue1(number);
Console.writeLine(number); ??????

setValue2(ref number);
Console.writeLine(number); ????
}
Static void setValue1(int n){
n = 5;
}
Static void setValue1(ref int n){
n = 10;
}
Static vs. Instance Members
• By default, members are per instance
o Each instance gets its own fields
o Methods apply to a specific instance

• Static members are per type


o Static methods and attributes saved globally
o No this variable in static methods
o Static methods can’t access instance data
Static vs. Instance Members
public class Calculator{
static a;
private b;
static void sum (){

}
}
Calculator cal = new Calculator();

Calculator.sum();
Classes vs. Structs
• Both are user-defined types
• Both can contain
o Data
• Fields, constants, events, arrays
o Functions
• Methods, properties, indexers, operators
Classes vs. Structs
Class Struct
Reference type Value type
Can inherit from any No inheritance
non-sealed reference (inherits only from
type System.ValueType)
Can have a destructor No destructor
Can have user-defined
No user-defined
parameterless
parameterless constructor
constructor

You might also like