Today - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Today

Description:

need code here to print out the stack. return os; Fall 2003 CS 325 Class Notes. Page 25 ... Added word protected to 'Pair' Needed for visibility (explain shortly) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 57
Provided by: davidc73
Category:
Tags: print | searches | to | today | word

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 5
  • Today
  • Project comments
  • Unix
  • Iterator for output
  • Homework
  • Project Two is due at 9pm, Sept. 17 (Wednesday)

2
Class Exercises
  • Write hello world using the GNU C compiler
  • Write a program (using GNU C) that declares a
    template class for a dynamic linked list and then
    adds five integers to an integer instantiation of
    the list

3
Project 2
  • Iterators for CSTreeltTgt

4
Project Description
  • Already have a templated CSTree class
  • Constructor
  • add()
  • Create templated iterators for CSTreeltTgt
  • CSTreeIterator_INORDER
  • CSTreeIterator_PREORDER
  • CSTreeIterator_POSTORDER
  • CSTreeIterator_LEVELORDER

5
Iterator methods
  • Constructor
  • Initialize iterator -- set current to first
    element
  • bool more()
  • return false if no more elements
  • int get (T v)
  • return current data (return type wont be int)
  • int set (T v)
  • return current data (return type wont be int)
  • bool next()
  • return false if cannot advance
  • void reset()
  • go back to beginning (re-initialize)

6
Binary Tree Traversals
7
Iterator Logic
  • Preorder (N L R)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.right
  • push current.left
  • push current.data
  • end loop
  • return pop
  • Inorder (L N R)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.right
  • push current.data
  • push current.left
  • end loop
  • return pop

8
More Iterator Logic
  • Postorder (L R N)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.data
  • push current.right
  • push current.left
  • end loop
  • return pop
  • LevelOrder
  • Initnq root node
  • while front is node loop
  • current dq
  • nq current.data
  • nq current.left
  • nq current.right
  • end loop
  • return dq

9
Files on the web
  • CSTreeIterator.cpp
  • Sample main
  • CSTreeIterator.h
  • Stubs

10
Project Comments
  • Must run on Unix with GNU C
  • May require small changes
  • Best programming style works on both
  • General format
  • Template classes for Iterators (4)
  • One File For Compilation
  • CSTreeIterator.h

11
Moving files to/from Unix
  • Moving files between a PC and Unix
  • Use ftp(file transfer protocol)
  • PC must be connected to the network
  • C\gtftp bama.ua.edu
  • Connected to bama.ua.edu.
  • 220-
  • 220- Use of this system (lines suppressed here)
  • 220 bama.ua.edu FTP server .
  • User (bama.ua.edu(none)) cordes
  • 331 Password required for cordes.
  • Password ltenter password heregt
  • 230 User cordes logged in.
  • ftpgt put hello.C
  • 200 PORT command successful.
  • 150 Opening ASCII mode data connection for
    hello.C.
  • 226 Transfer complete.
  • ftp 85 bytes sent in 0.00Seconds
    85000.00Kbytes/sec.
  • ftpgt quit

12
Moving files to/from Unix (cont)
  • Other ftp commands
  • dir, lists all the files on the remote system
  • cd xxx, changes directory on the remote
    system(also have lcd to change local directory)
  • get xxx, gets a file from the remote system
  • binary, toggles to binary mode (non-ASCII files)
  • mput .cpp, copies all your cpp files to remote
    sys(also have mget to retrieve files)
  • help, gives list of commands

13
Windows version
  • Most pcs have a windows version of ftp
  • Start
  • Programs
  • WS_FTP (usually)
  • WS_FPT
  • Beside host name/Address type bama.ua.edu
  • Enter your ID and password in appropriate spots
  • Click OK
  • This is a much easier way to ftp, however, be
    familiar with Unix version as well

14
Class Exercises (short)
  • Find a C file on your local PC
  • Copy it to your account on bama.ua.edu
  • Copy a file from your bama account to your local
    PC

15
Unix File Permissions
  • Each file in Unix has three levels of security
  • What the user can do to it
  • What people in the users group can do to it
  • What everyone else can do with it
  • The ls command can display security
  • /fs/cordes ls -l
  • total 10
  • drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
    cs325
  • drwx------ 2 cordes facstaff 512 Aug 30 0510
    foo
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • drwx------ 2 cordes facstaff 512 Aug 30 0937
    mail
  • drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
    networks
  • ls l shows files and directories and gives extra
    information such as permissions, userid, group,
    last modified date

16
Unix File Permissions (cont)
  • /fs/cordes ls -l
  • total 10
  • drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
    cs325
  • drwx------ 2 cordes facstaff 512 Aug 30 0510
    foo
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • drwx------ 2 cordes facstaff 512 Aug 30 0937
    mail
  • drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
    networks

permissions user group last-mod-date filename
17
Unix File Permissions (cont)
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • - r w r - - r - -
  • indicates this is a regular file, not a
    directory
  • r w indicates the user can read write this
    file
  • r indicates the group can read this file
  • r indicates that others can read this file

18
Changing Permissions
  • chmod ltpermissionsgt filename
  • Permissions consist of who /- what
  • Who is u (user), g (group), o (others)
  • What is r (read), w (write), x (execute)
  • gives permission
  • - takes permission
  • Examples
  • chmod u-w hello.C (user cannot write)
  • chmod gor hello.C (group others can read)
  • chmod ar hello.C (a is short for u, g, o)
  • man chmod talks about changing permissions in
    terms of octal numbers

19
Class Exercises
  • Type ls l in your home directory. Explain
    what permissions are set for each file.
  • The file .plan in your home directory can tell
    information about you (what you do). In order
    for others to read this file, they need to be
    able to execute your home directory (.) and also
    read your .plan file. Set it up so that other
    people can finger userid and see your .plan.
  • Note the finger command may be disabled

20
Unix requirements for 325
  • Basic manipulation of your files on Unix
  • Create sub-directories for organization, etc
  • Edit and compile C files
  • Some projects are done on Unix
  • Move files between PCs and Unix

21
Unix Directories
  • mkdir
  • Creates directory
  • rmdir
  • Removes directory (must be empty)

22
Unix apropos
  • apropos keyword
  • Searches on keyword for relevant info
  • Useful when you cannot use man (looking for
    command)
  • man
  • man keyword

23
Class Exercises
  • Teams new to Unix
  • Editors how do you
  • Copy a line
  • Delete a line
  • Give the command to
  • Rename hello.cpp to hello.C
  • Copy (to your home dir) hello.cpp from
    /st3/yessi001/cs325
  • Mail hello.cpp to yourself
  • Teams that know Unix
  • Give a one-line command (you can use pipes) to
  • Find the home directory of the user cordes
  • Print your last name in hex (and octal)
  • Print the first name of all the users on the
    system with the last name Smith
  • Convert the contents of hello.C to uppercase

24
More practice with iterators
  • Can use iterators to build output operators for a
    given data structure
  • Dont need to know anything about internal
    representation to output the data structure
  • Can define an overloaded output operator and then
    use the iterator within that function to print
    out the data structure
  • ostream operatorltlt(ostream os, const
    Stackltintgt theStack)
  • // need code here to print out the stack
  • return os

25
Output operator
  • Notes
  • This operator is not a friend of Node, Stack, or
    StackIter
  • We are simply using an instance of the StackIter
    class to traverse the list and print.
  • We will not directly access any private data
    members of any class, we will access them via
    public class methods
  • Lets look at the code using the iterator
  • template ltclass Tgt
  • ostream operatorltlt(ostream os, const StackltTgt
    theStack)
  • StackIterltTgt it (theStack)
  • while (!it.done())
  • os ltlt it.get() ltlt
  • it.next()
  • return os

26
Useful string functions
  • The operator concatenates two strings
  • Compare strings using lt, lt, , gt, gt
  • strn references char n, so does str.at(n)
  • Str.length() returns the length of the string
  • str.find(substring) finds substring
  • str.find(char) finds character
  • find( ) returns stringnpos if not found
  • int fooa.find('z')
  • if (foo stringnpos) not found

27
Class Exercises
  • The file (TBA) on bama.ua.edu contains a List
    class (using templates). Add an output operator
    for list using the iterator class so that you
    can print out the list.

28
Object-Oriented Programming
  • 3 major ideas in object-oriented programming
  • Encapsulation (hiding/protecting data)
  • Generalization (templates)
  • Inheritance (well learn about today)

29
Classes Inheritance
  • Want efficient techniques to develop code
  • Method 1 Templatesdefine a class generically,
    can plug in any type of data that you want
  • Method 2 Inheritancetake an existing class and
    add to it
  • Inheritance builds off of what already exists
  • Dont build from scratch
  • Find something similar, expand on it
  • Examples
  • Window
  • Account

30
Inheritance
  • What types of things do we obtain from our
    parents?
  • Attributes
  • Behaviors
  • What is a class composed of in general?
  • Data (attributes, data members)
  • Operations upon that data (behaviors, member
    functions, methods)
  • When a class inherits from another class, it
    inherits the attributes and behaviors (data
    members and methods) from its parent class

31
Inheritance
  • Can define attributes and behaviors for a general
    class, then inherit and refine those attributes
    and behaviors to fit a more specific case.
  • Existing class (yours or not yours) may do most
    of what you want, but you want to extend it
    without having to redo what has already been done
  • Parent general
  • Child - specific

32
Inheritance Basic terminology
  • Inheritance extension of an existing class to
    provide necessary functionality and promote reuse
  • Base Class existing class to be reused (parent)
  • Derived Class class that inherits attributes and
    behaviors of some base class (child, subclass)
  • A derived class can also become a base class of
    another derived class

33
Relationships
  • Has a
  • Deals with construction
  • This room has a door
  • A List has a Node pointer
  • Is a (Inheritance)
  • A square is a rectangle
  • An equilateral triangle is a triangle
  • A lab is a room
  • A savings account is a bank account

34
Natural Hierarchies
Shape
Ellipse
Rectangle
Triangle
Equilateral Triangle
Square
35
Class Exercises
  • Think of derived classes (specialized,
    extensions) for the following draw hierarchy.
    Decide what data members and methods would be in
    base classes and the extra data members and
    methods should be added to derived classes
  • class person (in a university setting)
  • class account (financial)
  • class list (single-linked list)

36
private, public w/ inheritance
  • Public any public member of the base class can
    be seen by a derived class
  • Private any private member of the base class
    cannot be seen by a derived class
  • Need Public so derived class can see members
    (else inheritance isnt inheritance)
  • Need Private else we lose the idea of
    encapsulation
  • So
  • Note member may refer to either data members or
    member functions (methods)

37
protected section
  • There can also be a protected section in a base
    class.
  • This section can be seen by a derived class with
    the is a property
  • This section cannot be seen by other classes
    with a has a property

38
Basic Syntax more to come
  • class BaseClass
  • //members methods
  • class DerivedClasspublic BaseClass
  • //extra members methods

39
Classes Inheriance
  • Want efficient techniques to develop code
  • Method 1 Templatesdefine a class generically,
    can plug in any type of data that you want
  • Method 2 Inheritancetake an existing class and
    add to it
  • Inheritance builds off of what already exists
  • Dont build from scratch
  • Find something similar, expand on it

40
Consider a class Pair
  • Stores two integers
  • Methods for
  • Setting values
  • Retrieving values
  • Friend operator for output
  • class Pair
  • int x
  • int y
  • public
  • Pair( ) x(0), y(0)
  • Pair(int a, int b) x(a), y(b)
  • void setX(int a) x a
  • void setY(int a) y a
  • int getX(void) return x
  • int getY(void) return y
  • friend ostream operatorltlt (ostream,
    const Pair)

41
Now want to build a new class
  • Triple stores three integers
  • Need methods to set values
  • Need methods to retrieve values
  • Need friend operator for output
  • Dont want to repeat code unnecessarily
  • Solution build off of (inherit from) Pair
    class
  • Simply add additional functionality to that class
  • Terminology
  • Base class (original class)
  • Derived class (augments, specializes base class)

42
Building the class Triple
  • class Pair
  • protected
  • int x
  • int y
  • public
  • Pair( ) x(0), y(0)
  • Pair(int a, int b) x(a), y(b)
  • void setX(int a) x a
  • void setY(int a) y a
  • int getX(void) return x
  • int getY(void) return y
  • friend ostream operatorltlt (ostream,
    const Pair)
  • class Triple public Pair
  • int z
  • public
  • Triple( ) z(0)
  • Triple(int a, int b, int c)
  • Pair(a, b), z(c)
  • void setZ(int a) z a
  • int getZ(void) return z
  • friend ostream operatorltlt
  • (ostream, const Triple)

43
What Happened?
  • Started with Pair to create Triple
  • Added word protected to Pair
  • Needed for visibility (explain shortly)
  • Class Triple built on top of Pair
  • public Pair implies we are building off of Pair
  • Inherited functions getX, getY, setX, setY
  • Just needed to define setZ, getZ
  • Needed to define new output operator
  • Old one does not work for triple

44
Instantiation
  • //outside of class
  • Pair one
  • Triple two
  • Triple three(3,4,5)

X Y
0
0
X Y Z
0
0
0
X Y Z
5
4
3
45
Class Exercises
  • The file on the web site has the Pair and Triple
    classes in them. Build on this to define a new
    class Quadruplet that contains four integers.
  • Use all three of these classes in a main program
    (set values, print them out).

46
Derived classes
  • Inherit from the base class
  • Members
  • Methods
  • Extends the base class in some way
  • Additional members
  • Additional methods
  • Both additional members additional methods

47
Public, Private Protected
  • Up until now, only mentioned public private
    parts to a class
  • public visible to users (outside world)
  • private cannot be seen (except by friends)
  • C actually has three levels of protection
  • private only the class (and friends) can see
  • protected class subclass (and friends) can see
  • public everyone can see

48
Class Exercises
  • Change the protected data in Pair and Triple to
    private.
  • What happens?
  • Can I still declare objects of type Triple and
    Quadruplet?
  • What methods still work for Triple?
  • setX, setY, setZ, getX, getY, getZ
  • What methods still work for Quadruuplet?

49
Constructors and Inheritance
  • Since a derived class builds off its base class
  • To instantiate an object in a derived class
  • Initialize base-class members (base constructor)
  • Initialize derived-class members (derived
    constructor)
  • Derived class constructor always calls the
    constructor of its base class first
  • If no constructor exists for derived class,
    default system constructor for derived class
    calls base-class default constructor

50
Class Exercises
  • What is the output of the following program?
  • void main(void) Pair aTriple bPair
    c(1,2)Triple d(3,4,5)Triple e(6)
  • class Pair
  • protectedint x int y
  • publicPair( ) x(0), y(0) cout ltlt "Pair 1"
    ltlt endl Pair(int a, int b) x(a), y(b)
    cout ltlt "Pair 2" ltlt endl
  • class Triple public Pair int z
  • publicTriple( ) z(0) cout ltlt "Triple 1" ltlt
    endl Triple(int a, int b, int c) Pair(a, b),
    z(c) cout ltlt "Triple 2" ltlt endl Triple(int
    a) Pair(), z(a) cout ltlt "Triple 3" ltlt endl

51
Another inheritance example
  • Dynamically-allocated linked list of integers
  • Add integers to front of list
  • Node class contains the actual data
  • Extend this class to a doubly-linked list
  • Have a set of forward links
  • Have a set of backward links

Head
Tail
27
38
51
52
2 pointers in our node class
  • class Node
  • int data
  • Node next // forward pointer
  • Node last // backwards pointer
  • public
  • Node( )
  • Node(int e) data(e)
  • friend class List
  • friend class DoubleLinkList

53
Base List Class (forward only)
  • class List
  • protected
  • Node head
  • public
  • List() head(0)
  • void add(int e) Node pnew Node(e)
    p-gtnexthead headp
  • void Print(void) Node p head while (p)
    cout ltlt p-gtdata ltlt " " pp-gtnext cout ltlt
    endl

54
Derived List Class
  • Allows you to traverse forward backward
  • Change add routine (replace base version)
  • Change print routine
  • Prints forward
  • Prints backward
  • Dont declare any new methods, just re-define
    existing methods to work in this context

55
Derived List Class
  • class DoubleLinkList public List
  • protectedNode tail
  • publicDoubleLinkList() tail(0) void
    add(int e) void Print(void) Node p
    head while (p) cout ltlt p-gtdata ltlt " "
    pp-gtnext cout ltlt endl p tail while (p)
    cout ltlt p-gtdata ltlt " " pp-gtlast cout ltlt
    endl

56
Class Exercises
  • Complete the derived list class from the previous
    slide. The code is on the web site.
Write a Comment
User Comments (0)
About PowerShow.com