Professional Documents
Culture Documents
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.
OR
7
What is object-oriented programming?
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){
cal.sum();
}
Class calculator{
public int a;
public int b;
10
Class Syntex
class $
attr
AttrName1
:
AttrNamen
method Pattern Statement end
:
method Pattern Statement end
end
11
Defining a class
12
Test
13
Test
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
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).
• 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
Class calculator{
public int a; //Attribute
public int b; //Attribute
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;
}
}
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
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
}
}
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