CSCI 1302 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CSCI 1302

Description:

Use 'ref' to create by-reference parameter passing ... TopUp = tu; Place common. initialization in a. separate function. Notice same method ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 25
Provided by: jonpr
Category:
Tags: csci | topup

less

Transcript and Presenter's Notes

Title: CSCI 1302


1
CSCI 1302
  • Object-Oriented Programming
  • Intermediate C

2
Agenda
  • Parameter passing (ref, out, value)
  • Static
  • Const
  • Overloading
  • Inheritance

3
Parameter Passing
  • By default, parameters are passed by value
  • A copy of the value is passed
  • The original is not modifiable
  • Use ref to create by-reference parameter
    passing
  • A reference to the value is passed (akin to a
    pointer)
  • The original (actual) parameter is modifiable
  • Use out when data is strictly coming out of
    (and being created in) the function
  • The original has no value
  • A new value will be placed into the parameter by
    the function

4
By Value Parameter Example
  • float CalculateCost (float unit_cost, int
    how_many)
  • return unit_cost how_many
  • static void Main()
  • ...
  • line_cost CalculateCost(uc, hm)
  • ...

5
By Value Parameter Example
  • float CalculateCost (float unit_cost, int
    how_many)
  • return unit_cost how_many
  • static void Main()
  • ...
  • line_cost CalculateCost(uc, hm)
  • ...

Even if you changethese locally, they
wontchange in Main.
6
By Reference Parameter Example
  • void UpdateGrade (ref float grade, float factor)
  • grade factor
  • static void Main()
  • ...
  • for(i0 i lt grades.Count() i)
  • UpdateGrade(gradesi, 0.05)
  • ...

7
By Reference Parameter Example
  • void UpdateGrade (ref float grade, float factor)
  • grade factor
  • static void Main()
  • ...
  • for(i0 i lt grades.Count() i)
  • UpdateGrade(gradesi, 0.05)
  • ...

Grade will be updatedinformation is taken
inand modifies the original
8
Out Parameter Example
  • void ReadChoice (out int choice)
  • do
  • Console.WriteLine("Please enter a choice 1-4
    ")
  • choice Int32.Parse(Console.ReadLine())
  • while ((choice lt 1) (choice gt 4))
  • static void Main()
  • ...
  • ReadChoice(c)
  • ...

9
Out Parameter Example
  • void ReadChoice (out int choice)
  • do
  • Console.WriteLine("Please enter a choice 1-4
    ")
  • choice Int32.Parse(Console.ReadLine())
  • while ((choice lt 1) (choice gt 4))
  • static void Main()
  • ...
  • ReadChoice(c)
  • ...

Choice is generated inthe module and
thenreturned to Main
10
Static
  • Each instance of a class (called an object) has a
    copy of the attributes
  • Changing an attribute in one object doesnt
    affect the attribute of another object
  • But what if we want persistence (shared) among
    all instances?

11
Static Variables
  • void PrintNumbers ()
  • static int count 0
  • Console.WriteLine(count)
  • count
  • static void Main()
  • ...
  • for(i0 i lt 50 i)
  • PrintNumbers()
  • ...

12
Static Attributes
  • class BMW_Z4
  • public static int VehicleId 0
  • ...
  • static void Main()
  • ...
  • BMW_Z4 my_z4 new BMW_Z4() // has VehicleId
    of 0
  • BMW_Z4 your_z4 new BMW_Z4() // has VehicleId
    of 1
  • ...

13
Const
  • Variables are just that variable
  • They can be changed programmatically via the
    assignment operator ()
  • But there are times when some values should be
    immutable
  • Preface the declaration with const
  • Cannot be changed EVER!

14
Const Example
  • class BMW_Z4
  • public const int MaxSpeed 185
  • private int currentSpeed
  • public void Accelerate
  • currentSpeed 5
  • if (currentSpeed gt MaxSpeed)
  • currentSpeed MaxSpeed
  • ...

15
Overloading
  • Overloading involves using the same
    method/function name
  • Vary the number of parameters
  • Vary the type of parameters
  • Cannot just change return type (ambiguity in
    invocation)
  • Useful to keep things simple
  • Squaring a number

16
Overloading Example
  • int Square (int i)
  • return i i
  • float Square (float i)
  • return i i
  • static void Main()
  • ...
  • float x Square(5.3)
  • int y Square(9)
  • ...

17
OperatorOverloading
  • class BMW_Z4
  • ...
  • public BMW_Z4 ()
  • Initialize(0, 2004, false)
  • public BMW_Z4 (int my)
  • Initialize(0, my, false)
  • private Initialize(int cs, int my, bool tu)
  • currentSpeed cs
  • ModelYear my
  • TopUp tu

Notice same methodname, differentparameters
Place commoninitialization in aseparate function
18
Inheritance
  • Inheritance allows one class to take on the
    properties of another
  • Superclass-subclass relationship
  • Sometimes called parent-child relationship
  • Use the keyword extends to express this
    relationship
  • Subclass will inherit certain attributes and
    methods
  • Benefit good design, reuse of code

19
Class Hierarchy
Mammal
int weight
giveBirth( )
Land-Mammal
int numLegs
Question how many attributes does Dog have?
Dog
boolean rabid
Chihuahua
SheepDog
20
Things to Note
  • Land-Mammal is a superclass to Dog, but a
    subclass to Mammal
  • Dog has three attributes
  • weight, numLegs and rabid
  • Two from inheritance, one it declared itself

21
Visibility and Inheritance
  • Public allows all to see
  • Private allows only class in which defined to
    see
  • Protected allows class and all subclasses that
    inherit to see
  • Consequently, well now use protected instead of
    private by default (common)

22
C Syntax
  • class Person
  • ...
  • class Student Person
  • ...
  • class Professor Person
  • ...

Notice the use of
23
The Base Class Object
  • All classes in C inherit (sometimes implicitly)
    from Object
  • Includes common set
  • ToString()
  • GetType()
  • Equals()
  • Often useful to override (implement) these
    virtual functions
  • public override string ToString()

24
FIN
Write a Comment
User Comments (0)
About PowerShow.com