Week2 1
Week2 1
https://anees-soomro.neocities.org
OOP CONCEPTS
Examples: Examples:
all primitive types from last slide strings, arrays, objects
and structs
char x;
x = '\u0020'; //
char y = 'c'; blank
float height1;
height1 = 1.443F;
float height2 =
float.Parse("1.22
2");
string s;
s = "def";
string t =
"abc";
int[]
values;
int count
values= =more.Length; // will be 3
new int[] {
3, 2, 7, 18
};
Conditions
if (more.Length == if (<boolean>)
14) {
{ evenmore[0] += ...
22; }
}
if (more.Length < 10) { evenmore[0] +=
22;
} else {
evenmore[2] = -17;
}
foreach(int x in values)
{ if(x > currentMax){
currentMax = x;
}
}
Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
+, -, *, / Arithmetic operators
% Rest of integer division (Remainder)
<, <=, ==, !=, >=, > Comparing operators
++x, x++, --i, i-- Pre- and post-incrementing / decrementing
Operator Meaning
|, &, ^ Binary OR, AND, XOR
<< Binary leftshift
>> Binary rightshift
Methods (functions)
public int computeMax(int[] values)
{
int currentMax = values[0];
Recall HelloWorld:
using System;
class HelloWorld
{
static void
Main()
{
Console.W
riteLine(
"Hello
world!");
}
Objects
class Employee
{
private string FirstName; 2 fields
private string LastName;
class Program
{
static void Main(string[] args)
{
Namespace02.Car carA = new
Namespace02.Car(); Car carB = new Car();
SpecialCar carC = new SpecialCar();
System.Console.WriteLine("Abraham");
Console.WriteLine("Bebraham"); //
possible due to the "using System";
}
}
Passing arguments by reference or value
Passing by value
public void Swap(int a, int b) public void Swap(string a, string b)
{ {
int temp = a; string temp = a;
a = b; a = b;
b = temp; b = temp;
} }
Passing by reference
public void SwapRef(ref int a, ref int b) public void SwapRef(ref string a, ref strin
{ b)
int temp = a; {
a = b; string temp = a;
b = temp; a = b;
} b = temp;
}
does affect the assigned values in the caller.
Returning more than one value
Calling code:
public int GetMax(int[] values)
{ a = GetMax(myArray);
return values.Max();
}
Property features:
●
read-only (without getter setter methods)
● write-only (without getter setter methods) a
access
When to use a property, when a field?
●
Property: if simple access to data and no complex computation.
●
Otherwise field with getter and setter.
// note: as soon
as you define ANY
constructor,
// the default
constructor is no
longer available
}
OOP – inheritance
class PdaItem
{
public string Name { get; set; }
public DateTime LastUpdated { get;
set; }
}
class Contact : PdaItem In C# only single inheritance!
{
public string Address { get;
set; } public string Phone
{ get; set; }
}
class Program
{
class PdaItem
{
public virtual string Name { get; set; }
}
}
}
class B : A
{
public override sealed void
Method()
{
}
}
class C : B
{
// does not compile!
public override void Method()
{
}
}
OOP – base class
class A
{
public virtual void Method()
{
Console.WriteLine("Honolulu"
);
}
}
class B : A
{
public override void Method()
{
base.Method();
Console.WriteLine("+
Luluhono");
}
}
OOP – abstract classes
Abstract classes
●
represent abstract entities.
● can not be instantiated, only derived
Name Description
Declaration: Implementation:
class Contact : PdaItem, IListable You can implement
interface IListable {
{ multiple
public string FirstName;
string[] Values interfaces.
public string LastName;
{ public string Address;
get;
}
}
public string[] Values
{
get
{
return new string[] { FirstName, LastName, Address }
}
} }
Usage: