C - PowerPoint PPT Presentation

1 / 104
About This Presentation
Title:

C

Description:

C . Object-Oriented Programming. The Class. 2. A class is a ... Canonical orthodox class form. J. Coplien. Default constructor. copy constructor. destructor ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 105
Provided by: brianm46
Category:
Tags: cc | cleaner | orthodox

less

Transcript and Presenter's Notes

Title: C


1
C Object-Oriented Programming
  • The Class

2
A class is a unit of encapsulation
  • Public operations, and
  • private implementation.

3
A class is an abstraction
  • String abstracts the char of C,
  • stack -- the canonical abstraction
  • List
  • Student,
  • etc.

4
Classes and C structures
  • default access for class private
  • default access for structpublic

This is the only difference between a C class
and a C structure.
5
Classes
  • Example of a bad class declaration
  • class Student
  • public
  • float gpa
  • char name

6
Classes and structures
  • Correct example of Class Declaration
  • class Student
  • private
  • float gpa
  • char name
  • do we need the private designation?

How do I access the "private" data?
7
Classes writing set and get functions
  • class Student
  • public
  • void setGpa(float g) gpa g
  • void setName(char n)
  • name new charstrlen(n)1
  • strcpy(name, n)
  • private
  • float gpa
  • char name

provide accessor and modifier functions
What's missing?
8
Classes and Objects
  • Declaration of a class variable (i.e. object)
  • Student s, t
  • Student v
  • int i
  • float x
  • The syntax is identical to conventional variable
    declarations.

This one is on the stack.
This one will be on the heap.
9
Classes
  • Since class declarations will likely be used by
    many program modules, put them in a header file
    that can be included as required
  • include Student.h

10
Classes and Objects
  • Access to class data members
  • s.setGpa(3.5)
  • s.setName(Dana)
  • v new Student(4.0, Fox)
  • t s
  • The Student class also needs constructors

function call
shallow copy?
11
Classes constructors
  • class Student
  • public
  • Student() //default
  • Student(char, float) //conversion
  • Student(const Student) //copy
  • friend ostream operatorltlt(ostream ,
  • const Student )
  • private
  • char name
  • float gpa

This is an example of an improper class
definition!
12
Constructor parameters
  • Default no parameters
  • Conversion parameters to make one
  • Copy the parameter is the copy instance

Default is used with arrays of objects.
Copy is used with "pass by value"
13
Constructors/destructors
  • Guarantee that data is initialized
  • allow classes to manage memory
  • constructors allocate new
  • destructors deallocate delete

Especially important for heap based memory
14
Prefer initialization to assignment
class Student public Student(int a)
age(a), iq(age100) Student(int a) age
a iq age 100 private int age
int iq
15
Prefer init to assign
  • Initialization is more efficient for data members
    that are objects
  • init uses copy constructor
  • assign uses default constructor and assignment
  • only way to pass a parameter to base class

16
Passing param to base class
class Person public Person(int a) age(a)
private int age class Student
public Person public Student(int age, float
g) Person(age), gpa(g) private float
gpa
17
List members in init list in order that they are
declared
class members are initialized in the order
of their declaration in the class! Whats the
value of iq? class Student public
Student(int a) age(a), iq(age100) private
int iq int age
18
Exercise to learn constructors
  • Write 3 constructors and a destructor for Student
    class. Test it with

void fun(Student stu) int main() Student
a, b(Darth Maul, 3.5), c b Student d
new Student(Anakin, 4.0) cout ltlt d ltlt
endl fun(a) return 0
19
Why is this a bad class definition?at least 2
member functions missing
  • class Student
  • public
  • Student() //default
  • Student(char, float) //conversion
  • Student(const Student) //copy
  • friend ostream operatorltlt(ostream ,
  • const Student )
  • private
  • char name
  • float gpa

This is an example of an improper class
definition!
20
Q what output should we get
  • int main()
  • Student x(Count Dooku), y(Anakin)
  • x y
  • y.setName(Darth Maul)
  • cout ltlt x ltlt endl

Soln need deep copy operator
21
Copying a class object
Original Object
Shallow Copy
22
Copying a class object
Original Object
Deep Copy
23
Canonical orthodox class form J. Coplien
  • Default constructor
  • copy constructor
  • destructor
  • assignment operator

operatorltlt
24
What functions does Csilently write?
class Empty class Empty public
Empty() Empty(const Empty ) Empty()
Empty operator(const Empty ) Empty
operator() const Empty operator()
const
If you write this
You get this if needed!
25
How would you need them?
const Empty e1 // need default constructor,
and // destructor Empty e2(e1) // copy
constructor e2 e1 // assignment
operator Empty pe2 e2 // address-of
operator (non const) const Empty pe1 e1 //
address-of operator (const)
26
What do the compiler generated functions look
like?
inline EmptyEmpty() inline EmptyEmpty()
inline Empty Emptyoperator() return
this inline const Empty Emptyoperator()
const return this The copy constructor
and assignment operator simply do a member wise
copy, i.e., shallow. Note that the default
assignment induces a memory leak in Student!
note destructor is NOT virtual
27
When do we need to write a copy constructor,
destructor assign operator?
  • Anytime the class has heap-based data members
  • all base classes should have a virtual destructor
  • Studies have shown that, if you dont need
    copy/assign, the compiler generated functions run
    10-20 faster than programmer generated ones.

28
How do we overload assignment?
Student operator(const Student stu) if
(this stu) return this delete name
name new charstrlen(stu.name)1
strcpy(name, stu.name) gpa stu.gpa
return this
(1) Why the comparison on the first line? (2)
Could the first line be if (this stu)? (3)
Why return this? What does it enable? (4) Why
not return stu, rather than this?
29
Formula for overloaded assignment
  • Check for equality of lhs rhs
  • delete storage for lhs
  • create new storage for lhs, thats size of rhs
  • copy rhs stuff to lhs
  • return this

30
an overloaded binary operator
  • can be written in infix form
  • a b
  • cout ltlt stu
  • or can be written in prefix form
  • a.operator(b)
  • cout.operatorltlt(stu)

here it's more obvious what "this" is
31
Principle of least privilege
  • Can reduce debugging and provide documentation by
    only allowing a function the least amount of
    privilege
  • can prevent a function from modifying a parameter
  • can prevent a member function from modifying data
    attributes
  • allow a function enough data access to accomplish
    its task and no more!

32
Explicitly disallow any implicit functions you
dont want!
  • Suppose you want to write a class template array
    that behaves like C arrays, except that it does
    bounds check
  • C does not allow array assignment e.g. int
    a10, b10 a b //error

dont define the operator to keep member
friend functions from using it!
class Array private Array operator(const
Array )
33
Overloading operators
  • almost all operators can be overloaded
  • operators are binary or unary
  • have the same precedence as their compiler
    counterpart
  • can be members or friends, usually
  • overloaded output operator cannot be a member of
    any user defined class

34
overloading output operatoras a friend function
class Student public getName() return
name getGpa() return gpa
friend ostream operatorltlt(ostream , const
Student ) private char name float
gpa ostream operatorltlt(ostream out,
const Student s) out ltlt s.getName() ltlt
\t ltlt s.getGpa() return out
35
Overloading output operatoras stand-alone
function
class Student public getName() return
name getGpa() return gpa
private char name float
gpa ostream operatorltlt(ostream out,
const Student s) out ltlt s.getName() ltlt
\t ltlt s.getGpa() return out
What's wrong with this class definition?
36
Put prototype in header file,and body in
implementation file
class Student public getName() const
return name getGpa() const return gpa
private char name float
gpa ostream operatorltlt(ostream , const
Student ) ostream operatorltlt(ostream out,
const Student s) out ltlt s.getName() ltlt
\t ltlt s.getGpa() return out
header file
implementation file
37
Exercise with classes
  • Complete the definition of Student with all 3
    constructors, overloaded assignment, overloaded
    output, and get and set
  • Write a main program that contains 2 functions
  • a function to init the list with 3 Students
  • a function that prints the list

38
Using students
  • In actual practice, we want to store lots of
    students
  • need an array of students
  • Student list10
  • Q How many times does a constructor
    get called for list?
  • Q which constructor?

39
Whats missing
  • Need an easier way to print a student
  • overload the output operator ltlt
  • operatorltlt must be a friend function cannot be
    a member function cuz its already a member of
    class ostream
  • Need a class to manage the list of Students

40
Friends
  • Can access private member functions and private
    data attributes!
  • Can be functions or classes
  • Might not always be best solution!

41
Overloading the output operator
  • Class Student
  • public
  • friend ostream operatorltlt(ostream out,
    const Student student)
  • out ltlt student.name
  • return out

42
Class StudentManager public
StudentManager() count(0) void
insert(const Student ) friend ostream
operatorltlt(ostream , const Student
) private int count Student
list100
42
42
43
Class StudentManager public
StudentManager() count(0) void
insert(const Student student)
listcount student friend ostream
operatorltlt(ostream out,
const Student
student) for (int i 0 i lt count
i) out ltlt studenti ltlt endl
return out private int count
Student list100
43
43
44
Class exercise
  • Complete the StudentManager Class
  • rewrite main to use the List Class.
  • Write a new main program that allows the user to
    choose among commands
  • insert student
  • delete student
  • print the list
  • find a student
  • exit

45
Discussion of Manager class
  • Advantages
  • encapsulates functionality into a single class
  • simplifies the main program
  • Disadvantages
  • there is a limit (100) on number of Students
  • Student default constructor called 100 times!

46
Suggested Naming Conventions
  • global constants ALL CAPS!
  • local global variables ALL LOWER CASE, USE
    UNDERSCORE
  • Class names BEGIN EACH WORD WITH UPPER CASE, NO
    UNDERSCORE
  • Class member functions BEGIN LOWER CASE, then
    BEGIN EACH WORKD WITH UPPER CASE
  • Data members SAME AS MEMBER FUNCTIONS

47
Operator OverloadingADT for binary math
  • Would like variables of type binary to be like
    any other data type. Thus
  • overload arithmetic operators
  • overload output to be binary
  • overload input to use decimal
  • How do we represent internally?

48
Binary should look like int
include Binary.h main() int sum 0
Binary number1, number2 7 number1 15
cout ltlt number1 number2 ltlt endl
49
// This is file Binary.h class Binary public
Binary() number(0) Binary(int n)
number(n) Binary(const Binary bin)
Binary operator(const Binary ) Binary
operator() Binary operator(const Binary
) friend ostream operatorltlt(ostream ,
const Binary ) private int number
Where's the destructor?
49
49
50
include Binary.h // this is file
Binary.cpp Binary Binaryoperator(const Binary
rhs) Binary temp(numberrhs.number)
return temp Binary operator()
number return this
50
50
51
// this is a global function s/b in
Binary.cpp ostream operatorltlt(ostream out,
const Binary bin) stackltintgt stk int
number bin.number while (number)
stk.push( number 2 ) number / 2
while ( !stk.empty() ) out ltlt
stk.top() stk.pop() return out
51
51
52
Exercise for Binary type
  • Complete the class definition for Binary
  • construct a main program that uses Binary
  • Discuss return value transmission mode
  • operator
  • operator
  • operator (postfix, prefix)
  • Discuss merits of friend vs member functions for
    , , output, etc.

53
Discussion of Binary class
  • Compilation can be made simpler by using a
    makefile
  • makefile automatically compiles and links program
    modules
  • makefiles are mysterious

54
makefile/Makefile
  • Consist of definitions,
  • Followed by sequences of 2 line commands.
  • Begins with id, followed by dependencies of id.
  • Second line is the rule to make id this line
    MUST be preceded by a tab
  • To use the make file type make ltidgt

55
CCCg FLAGS-Wall main main.o
Binary.o (CCC) (FLAGS) -o main main.o
Binary.o main.o main.cpp Binary.h (CCC)
(FLAGS) -c main.cpp Binary.o Binary.cpp
Binary.h (CCC) (FLAGS) -c Binary.cpp clean r
m -f main .o core
55
55
56
Discussion of makefile
  • (CCC) permits us to easily switch to another
    compiler e.g. CC
  • make clean will clean the directory of large
    files
  • -o option creates an executable
  • -c option creates .o file

57
C-strings are no fun
  • Programmer must manage memory
  • comparison is awkward
  • concatenation is non-standard
  • strcat(a, b), instead of
  • a b
  • No substring facilities

58
Solution write our own String classWhat do we
need
  • Constructors
  • default
  • conversion
  • copy
  • overloaded operators
  • output
  • concatenation

59
Dynamic storage
  • Need pointers
  • never run out of room -)
  • must allocate storage new
  • must also deallocate storage delete
  • failure to deallocate storage memory leak
  • constructors/destructors were designed to
    facilitate memory management

60
// This class definition of String s/b in file
String.h class String public String()
String(char ) String(const String )
String operator(const String ) int len()
const friend ostream operatorltlt(ostream ,
const String ) private char buf
60
60
61
// This class definition of String s/b in file
String.h class String public String(int n
0) buf(new charn1) buf0 \0
String(char s) buf(new charstrlen(s)1)
strcpy(buf, s) String(const String )
String() delete buf char
operator(int i) return bufi String
operator(const String ) int len() const
return strlen(buf) friend ostream
operatorltlt(ostream , const String ) private
char buf
61
61
62
// This is file String.cpp include
String.h ostream operatorltlt(ostream out,
const String s) out ltlt s.buf return
out String Stringoperator(const String
rhs) String temp( strlen(buf)strlen(rhs.buf)
) strcpy(temp.buf, buf) strcat(temp.buf,
rhs.buf) return temp
62
62
63
We need operator
  • Discuss the merits of the following comparison
    function
  • bool Stringoperator(const String rhs)
  • return (this rhs)

64
String exercise
  • Discuss whats missing from String spec
  • Complete the class definition and implementation
    write copy constructor
  • Write a main program that uses Strings.
  • Write a makefile
  • Thoroughly test the class
  • Is there one in the standard C library?

65
String exercise
  • Add operator to String
  • Q when are constructors called?
  • Q when is assignment called?
  • Q which is better initialization or assignment?
  • Consider main.cpp, String.h and String.cpp

orthodox canonical class form
66
include String.h String Stringoperator(co
nst String rhs) if (this rhs) return
this delete buf buf new
charstrlen(rhs.buf) 1 strcpy(buf,
rhs.buf) return this
66
66
67
Discussion of Stringoperator
  • Note the return transmission mode. Why?
  • what happens if we do not delete buf?
  • Why do we need 1 when we new buf ?
  • What does return this mean?
  • Discuss the following 3 options
  • if (this rhs) return this
  • if (this rhs) return this
  • if (strcmp(buf, rhs.buf) 0) return this

68
include String.h int main() String a,
b(hello), c(a), d world
What constructors are called?
68
68
69
// Note that its more efficient if
constructors // use initialization rather than
assignment. // To see place cout in
constructors for String! class Test public
Test(char n) name(n) // initialization
Test(char n) name n //
assignment private String name main()
Test t(surprise)
69
69
70
Making Strings useful
  • Sometimes we want to run through a string i.e.
    iterate through the string.
  • For example, if we read a line of text from a
    file, we might want to access each word on the
    line.
  • Can write a special class called an iterator

71
Iterator class
  • Allows user to look at each item in the data
    structure
  • can be forward or backward
  • can have more than one iterator on a given data
    structure

72
iterator
  • For a linked list would return each item in the
    list, starting with the first an proceeding
    through the list
  • for a string might return each word in the list,
    or it might return each character
  • for a tree would return each item in the tree,
    in some order (preorder, depth first, etc.)

73
// partial specification for iterator over
String class StringIterator public
StringIterator(const String s, char d )
str(s), index(0) , delim(d)
void operator() String operator() ()
const bool atEnd() const return index
str.len() private String str unsigned
int index char delim
73
73
74
StringIterator spec discussion
  • Constructor must init data members in the order
    that they are listed in the declaration
  • default delimiter is a blank
  • overloaded parens looks odd
  • note that str.len() is one past the end!
  • we could overload operatorltlt for debugging

75
// implementation of operator include
StringIterator.h void StringIteratoroperator
() while (index lt str.len()
strindex ! delim) index while
(index lt str.len() strindex delim)
index
75
75
76
// implementation of operator() String
StringIteratoroperator() () const char
buf80 int temp_index index, i 0
while (temp_index lt str.len()
strtemp_index ! delim) bufi
strtemp_index bufi \0
return String(buf)
might be better to overload operator()
76
76
77
StringIterator exercises
  • Complete the specification and implementation
  • overload operatorltlt for debugging
  • thoroughly test your ADT
  • extend StringIterator to accept a set of
    delimiters
  • what happens if you initialize the data members
    in different order than they are declared?

78
Stack a common data structure
  • Public operations
  • push
  • pop
  • test empty
  • possibly test full
  • private data representation

The canonical abstraction
78
79
class IntStack public IntStack()
count(EMPTY) void push(int n)
itemscount n void pop()
--count int top() const
return itemscount bool isEmpty() const
return count EMPTY bool isFull() const
return count 99 private enum
EMPTY -1 int items100 int count
79
79
80
Stack exercise
  • Implement and test Stack class
  • Discuss alternatives to items100
  • modify IntStack so that it accepts a parameter
    describing the size of items
  • Discuss drawbacks of IntStack
  • What are solutions to drawbacks?

81
Template classes
  • Functions accept variables as parameters
  • template classes accept types as parameters
  • functions can also use templates

82
template ltclass Tgt class Stack public
Stack() count(EMPTY) void push(const T
n) itemscount n void pop()
--count const T top() const
return itemscount bool isEmpty()
const return count EMPTY bool
isFull() const return count 99
private enum EMPTY -1 T
items100 int count
82
82
83
Discussion of template stack
  • Note the change in parameter transmission from
    IntStack to StackltTgt
  • Note that top() passes return by value can
    pass-by-value return be avoided?
  • Can arrays, in C, be dimensioned dynamically?
  • What happens if user pops from empty stack?

84
Exceptions
  • Useful for handling error conditions.
  • Also useful for exceptional cases, such as eof,
    end-of-line, etc.
  • keywords try, catch, throw

85
How to use exceptions
  • Each try block has corresponding catch block(s)
  • calls to functions that may throw an exception
    are placed in a try block.
  • The called function, when faced with exceptional
    condition, performs a throw
  • the exception is thrown back, through the call
    chain, to matching catch, or
  • program bombs!

86
// give the output for the program below void
fun(int number) if (number 2 0)
throw error cout ltlt This is fun ltlt
endl main() try fun(2)
fun(3) catch (char msg)
cout ltlt msg ltlt endl return 1
86
87
// give the output for the program below void
fun(int number) if (number 2 0)
throw error cout ltlt This is fun ltlt
endl main() int n 2 while (n)
try fun(n--) catch (char
msg) cout ltlt msg ltlt endl

87
88
class Clean public Clean() cout ltlt
constructing Clean ltlt endl Clean()
cout ltlt destroying Clean ltlt endl void
fun() Clean c throw error main()
try fun() catch(char msg) cout
ltlt msg ltlt endl
what's the output? what's the point?
88
89
Stack exercise
  • Add exception handling to Stack class
  • write a main program with try/catch block that
    uses Stack

90
Dynamic storage
  • Need pointers
  • never run out of room -)
  • must allocate storage new
  • must also deallocate storage delete
  • failure to deallocate storage memory leak
  • constructors/destructors were designed to
    facilitate memory management

91
Linked list
  • Dynamic memory used to store data (usually in a
    node!)
  • node usually stores data and pointer to next node
    (at least)
  • can be singly linked, doubly linked, circularly
    linked
  • can have dummy header node and/or dummy footer
    node

92
Singly linked list
head
node
node
node
92
93
// partial specification implementation for
Node class class Node public friend class
List Node() data(0), next(NULL)
Node(int d, Node n) data(d), next(n)
int getData() const void setData(int d)
friend ostream operatorltlt(ostream ,
const
Node ) private int data Node next
93
93
94
// partial specification implementation for
Node class class Node public friend class
List Node(int d 0, Node n NULL)
data(d), next(n) int getData() const
return data void setData(int d) data d
friend ostream operatorltlt(ostream out,

const Node node) out ltlt node.data
return out private int data Node
next
94
94
95
include Node.h class List public List()
head(NULL), current(NULL) void insert(int
data) head new Node(data, head) void
delete() Node temp head head
temp-gtnext delete temp Node
getFront() current head return current
Node getNext() currentcurrent-gtnext return
current friend ostream operatorltlt(ostream
, const List ) private Node head
Node current
95
95
96
Discussion of List class
  • insert actually inserts at the front
  • getFront, getNext and current allow us to iterate
    through the list. Is there a better way?
  • Why is List a friend of Node?
  • Can we construct Node and List so that friendship
    is not required?
  • Note that deleting an arbitrary element is
    tricky. Would a doubly linked list simplify?
  • Should List be templated?

97
List class exercise
  • Complete specification implementation, adding
    insertAtRear, insertAtFront, insert, deleteFront,
    deleteRear, deleteNth, ...
  • Write a main program that uses List and test
  • Re-write Node and List so that friendship is not
    required.
  • Push the envelope template List
  • Write an iterator class for List

98
List exercise
  • Find a solution to the Josephus problem for
    inputs n and m
  • describe a solution, using IntList, to the
    problem of arbitrary precision arithmetic.
    Compare the speed of your program with bc on
    unix.

99
Doubly linked list
  • Easier to delete an arbitrary element
  • more overhead to implement
  • more storage
  • sometimes use a dummy header node to facilitate
    inserting into an empty list and deleting from a
    list of size 1.
  • Can use a pointer to the front and rear,
    facilitating insertion at either end.

100
Doubly linked list
dummy
node
node
front
rear
node
number
next
prev
101
include ltiostream.hgt class Node public
friend class List friend class ListIterator
Node(int i 0, Node n NULL, Node p
NULL) number(i), next(n), prev(p)
friend ostream operatorltlt(ostream , const
Node ) private int number Node
next Node prev
101
102
include Node.h class List public friend
class ListIterator List() rear front
new Node void insertAtFront(int n) void
insertAtRear(int n) bool empty const return
front rear int size() const friend
ostream operatorltlt(ostream , const List
) private Node front Node rear
102
103
class ListIterator public ListIterator
(const List list)
front(list.front), current(list.front-gtnext)
void init() current front
void operator() current
current-gtnext Node operator() () const
return current bool more() const
return current ! NULL private Node
front Node current
103
104
Doubly linked list exercises
  • Complete the spec and implementation
  • test the list
Write a Comment
User Comments (0)
About PowerShow.com