Session 02 and 03: C# OOP 1

wakecabbagepatchSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

91 views

Session 02 and 03:

C# OOP 1

OOP in C#:

Classes and Objects in C#.

Object
-
Oriented Design.

UML Class Diagram.

Object Interaction.


FEN 2013
-
01
-
27

1

AK IT: Softwarekonstruktion

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

2

Object
-
Oriented Programming

“ The Three Pillars of OOP”:


Encapsulation


Inheritance

Polymorphism

In later
sessions

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

3

OO
-
Principles

-
encapsulation


Seen from the outside an object is an entity offering a
number of services (public methods and properties).


The implementation and data representation are hidden
or encapsulated.


This makes it possible to change implementation without
affecting other parts of the program.


Also it becomes possible to think and talk about and use the
object without knowing the implementation.


Hiding data behind methods and properties are called
encapsulation

or
data abstraction
.


Encapsulation

or
data abstraction

is one the fundamental
principles of object
-
oriented programming.

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

4

Definition of Object and Class


Object


Represents a
real
-
world concept
, realised by
data

(attributtes)
associated with the concept and a number of
operations

(methods)that may be used to access the attributes of the object.


Class


A
type

that defines the attributes and methods of a set of objects
that all represent instances of the same real
-
world concept.


The class describes the structure of the concept, and the
objects are the actual instances of the class.


The class is static


exists only compile time.


Objects are dynamic


exist runtime.

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

5

Attributes (data)


Attributes define the data to be stored (name and
type). Attributes are defined in the class, and are
assign values in the objects.


E.g.:


BankAccount:


accountNo, balance, maxLimit, interestRate etc.


Employee:


name, departmentNo, salery, jobTitle etc.


The
state

of an object is given by the value of its
attributes at a given time.

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

6

Methods (operations)


The operations of an object are defined by the
methods implemented in the class.


Calls to methods either return information about the
state of the object (accessors) or change the state of
the object (mutators).


BankAccount


WithDraw(), Deposite(), GetBalance() etc.


Employee


GiveASaleryRaise (), SetTitle() etc.

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

7

Properties

(C# speciality)


Are used for getting and setting attribute values.
(Replace set
-

and get
-
methods in Java and C++).


Provide a syntax similar to direct access of the
attributes.


(Anders Hejsberg footprint?)

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

8

Constructor


Method(s) with the same name as the class and no return
type.The job of a constructor is to initialise the attributes
of the object during object creation.


E.g. Creating an object


BankAccount acc = new BankAccount();


BankAccount()

is a call to the constructor.


The
new

command


Allocates memory for the object.


Assigns the variable (the reference)
acc

to the allocated block of
memory


new

is actually a function that returns an address in
the heap.


Constructors may be overloaded

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

9

The Anatomy of a Class

Classes are usually written by this pattern:


class

ClassName


{


declaration of attributes


constructors


properties


methods

}

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

10

Methods



<access modifier> <return type> Metodenavn (parameter list) {



statements

}



public int

SumOf2Ints (int int1, int int2)


{



int sum;



sum = int1 + int2;



return sum;


}

Acessmodifier:
public/protected
/private



Local variable



return



Parameters

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

11

The Class
BankAccount

-

attributes and constructor

namespace Banking

{


public class BankAccount


{



private double balance;



private int accNo;



private int interestRate;




public BankAccount(int no, int ir)



{




balance = 0;




accNo = no;




intrestRate = ir;



}

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

12

Methods

public bool Withdraw(double amount)



public void Deposite(double amout)



public void GiveInterest()

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

13

Properties

public
int

InterestRate

{


get{return
interestRate
;}


set{if( value>=0)
interestRate

= value;}

}


Lets do it in C# using Visual Studio.

Source here
.

UML class diagrams


Can be used for different things:


Domain model (or conceptual model) showing objects in
the users world ((problem) domain) and their relationships.


Design class diagram showing the structure of the code.


For the moment our class diagrams will do both.

FEN 2013
-
01
-
27

AK IT:
Softwarekonstruktion

14

UML class diagram


A class is described by:


Its name


Its attributes (field variables, data members)


Its methods (operations)

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

15

Class name

Attributes

Methods

For simplicity attributes and/or
methods may be omitted, so only
structure is shown

UML class diagram


The classes are connected


in UML:

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

16

The connection is called an
association
, and is shown as a line.

The
multiplicity

or
cardinality

of
the association is indicated by
numbers. 1 means exactly one, so
a bank account belongs to
exactly one customer

Multiplicity may have a minimum and
a maximum: 0..1 means zero or one,
so a customer may be associated
with zero or one bank account.

Object Interaction


The Banking example shows object
interaction:


The classes are connected


in UML:

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

17

In the Code

public
class

Customer{


//…


private
BankAccount

account
;


//…




public void
CreateAccount
(
int

no, double
ir
, double
bal
){


account= new
BankAccount
(no,
ir
,
bal
);


}


//…

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

18

Customer is responsible for creating
BankAccount objects.

The association is
implemented by an object
reference (attribute).

In the Code

public class Program{


//…


Customer c = new Customer(1, "Peter Thomsen");


//…



Console.WriteLine("Customer: "+
c.Name
+" has DKK “


+
c.Account.Balance

+ " in the bank");

FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

19

Methods in the other class is
called using the reference.

Cardinality or Multiplicity


Tells how many objects an object
may be associated with:


One customer may have one
account, an account must belong to
a customer.



One customer may have many
accounts, an account must belong to
one customer.



A customer may have zero or more
accounts, an account may belong to
one or more customers.


FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

20

Exercise


Do the first parts of the
ForestExercise

(classes
Owner and Forest, no 1


13).




The rest of the
ForestExercise
.


FEN 2013
-
01
-
27

AK IT: Softwarekonstruktion

21