Title: Class
1Class
2Objectives
- Discuss class basics
- fields
- methods
- access levels
- Present object creation
- new operator
3Class
- Class represents concept in application domain
- defines a new type
- contains data and operations
- fundamental to object oriented programming
class Stock ...
class CheckingAccount ...
class Rectangle ...
class Aircraft ...
class Rational ...
4Instance fields
- Class instance fields store object state
- each instance gets own copy
- also called instance variables
class Stock string name double price
int shares ...
instance fields
5Object creation
- Objects created using new operator
- memory allocated for all instance fields
- object also called instance
- object creation also called instantiation or
activation
Stock ibm new Stock() Stock sun new Stock()
allocate
ibm
name price shares
objects
sun
name price shares
6Member access
- Object members accessed using dot operator
Stock ibm new Stock() Stock sun new
Stock() ibm.name "IBM" ibm.price
56.0 ibm.shares 100 sun.name
"SUN" sun.price 10.0 sun.shares 200
access fields
ibm
name IBM price 56.0 shares 100
sun
name SUN price 10.0 shares 200
7Method
- Class methods implement the behavior of the class
- specify return type, name, parameters, and body
- use void if no value returned
class Stock void Buy(int shares) ...
void Sell(int shares) ... void
SetPrice(double price) ... double
Value() ... ...
methods
8Method invocation
- Method invoked on object
- use dot operator
- pass parameters
Stock ibm new Stock() ibm.SetPrice(56.0) ibm.
Buy(100) ibm.SetPrice(99.0) ibm.Sell(50)
call methods
9Method implementation
- Method implementation uses keyword this
- handle to object on which method was called
- used to access members from inside method
class Stock string name double price
int shares void Buy(int s)
this.shares s ...
Stock ibm new Stock() ibm.SetPrice(56.0) ibm.
Buy(100)
ibm/this
name price 56.0 shares 100
implement method
use this
10Required this
- Must use this when local name conflicts with
member name
class Stock string name double price
int shares void Buy(int shares)
this.shares shares ...
parameter and field have same name
must use this
11Omitting this
- Can omit this when there is no ambiguity
- class members accessed implicitly
class Stock string name double price
int shares void Buy(int s) shares
s ...
omit this
12Member declaration order
- Members may be declared in any order
- no requirement to declare before use
- different rule than local variables
class Stock void Buy(int s) shares
s string name double price int
shares ...
use
declare
13Return value
- Use return to send data out of method
- value sent back to caller
class Stock double Value() return
price shares ...
return
Stock ibm new Stock() ibm.SetPrice(56.0) ibm.
Buy(100) double v ibm.Value()
14Method overloading
- Can have several methods with same name
- parameters lists must be different
- compiler selects version to call based on passed
parameters - cannot overload on return type alone
class Stock void Buy(int shares)
this.shares shares void Buy(int
shares, double price) this.shares
shares this.price price ...
overload
Stock ibm new Stock() ibm.Buy(100, 56.0)
call (int,double) version of Buy
15Value parameter
- Pass by value is default parameter passing
mechanism - data copied into method
- any changes to parameter inside method affect
local copy only
void F(int x) x 0
value parameter
change local copy
int y 9 F(y)
y unchanged
16Ref parameter
- ref parameter passes data in and out
- use keyword ref in definition and call
- must use variable in call
- must initialize passed variable before call
ref parameter, initially 9
void H(ref int x) x x 2
int y 9 H(ref y)
y set to 18
17Out parameter
- out parameter returns data through parameter
- use keyword out in both definition and call
- actual parameter must be a variable, cannot pass
literal value - must assign to parameter inside method or
compiler error
void G(out int x) x 11
out parameter
assign to parameter
int y G(out y)
y set to 11
18Member default access
- Class members default to private
- redundant to use keyword private
class Stock string name private
double price private int shares
void Buy (int shares) ... private void
Sell(int shares) ... ...
equivalent
equivalent
19Meaning of private
- Access to private member limited to code in that
class only - compiler error to attempt access from external
class
class Stock private int shares void
Sell(int shares) this.shares shares
...
private member
access ok in Stock class
class Test static void Main() Stock
ibm new Stock() ... ibm.shares 9
error to access in other class
20Public member access
- Members can be made public
- can then be accessed outside the class
class Stock private string name private
double price private int shares public
void Buy (int shares) ... public
void Sell (int shares) ... public
void SetPrice(double price ) ... public
double Value () ...
public
21Encapsulation
- Access levels used to separate interface from
implementation - interface made public
- implementation made private
- Separation allows implementation to be easily
changed - without breaking user code
- supports object-oriented principle of
encapsulation
22Naming
- Naming guidelines
- class intercaps with initial capital
- method intercaps with initial capital
- method parameter intercaps with initial lower
case - public field intercaps with initial capital
- private field intercaps with initial lower case
follow naming guidelines
class SavingsAccount public void
Deposit(double amountOfDeposit) ... public
int GetAccountNumber() ...
public double Balance private int
accountNumber ...
23Summary
- Class is basic unit of C programming
- contains data (fields) and operations (methods)
- Several parameter passing options available
- value, out, ref
- Several options for protection level of class
members - supports encapsulation and information hiding
24Initialization
25Objectives
- Present field initialization options for fields
- default values
- variable initializers
- constructors
26Instance field default values
- Instance fields set to default values when object
created - 0 for numeric types
- false for bool
- '\x0000' for char
- null for references
class Rational int numerator int
denominator ...
instance fields
a
numerator 0 denominator 0
Rational a new Rational()
set to default values
27Variable initializer
- Instance fields can be initialized at point of
definition - called variable initializer
- executed each time an object is created
- convenient way to overwrite default values
class Rational int numerator int
denominator 1 ...
initialize
28Constructor
- Class can supply constructor to do initialization
- automatically invoked when object created
- implemented using same name as class with no
return type
class Rational int numerator int
denominator public Rational(int numerator,
int denominator) this.numerator
numerator this.denominator denominator
...
constructor
29Invoking constructor
- Constructor automatically invoked when object
created
Rational a new Rational(2, 5)
create object, invoke constructor
a
numerator 2 denominator 5
30Multiple constructors
- Class can supply multiple constructors
- parameter lists must be different
class Rational public Rational(int numerator,
int denominator) this.numerator
numerator this.denominator denominator
public Rational(int numerator)
this.numerator numerator
this.denominator 1 ...
constructor
constructor
31Default constructor
- Can supply constructor that takes no arguments
- often called the default constructor
class Rational public Rational()
this.denominator 1 ...
no argument constructor
32Selecting constructor to invoke
- Compiler selects constructor version
automatically - based on arguments passed
Rational a new Rational(2, 5) Rational b
new Rational(6) Rational c new Rational()
two ints
one int
no arguments
33Constructor initializer
- One constructor can invoke another constructor
- use this(...) syntax before constructor body
- called constructor initializer
- can put common code in constructor that others
call
class Rational public Rational(int numerator,
int denominator) this.numerator
numerator this.denominator denominator
public Rational(int numerator)
this(numerator, 1) ... ...
call 2 argument constructor
34Compiler generated constructor
- Compiler creates default constructor
- only if no constructors supplied by programmer
- Compiler generated constructor
- takes no arguments
- has empty body
- calls no argument constructor for base class
35Initialization order
- Initialization options executed in well defined
order - fields set to default values
- variable initializers run in textual order top to
bottom - constructor executed
36Summary
- Three common options for field initialization
- default values, variable initializers,
constructors - executed in well defined order