OBJECTORIENTED PROGRAMMING - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

OBJECTORIENTED PROGRAMMING

Description:

One of the objects has to be an object of the class. Can't define new operators ... Operator overloading should be used with care, only in cases where it is needed ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 39
Provided by: stude1833
Category:

less

Transcript and Presenter's Notes

Title: OBJECTORIENTED PROGRAMMING


1
OBJECT-ORIENTED PROGRAMMING
  • Spring 2009 Haifa University

2
OOP - OPerators
  • Operators Intro
  • Overloading operators
  • Streams
  • Examples

3
Operators in C / 1
  • Operators in programming languages are similar to
    operators in math
  • C supports many operators
  • Arithmetic -b (negative), ab, a-b, ab, a/b,
    ab, a, a--, a, --a, a b, a-b, ab, a/b,
    ab
  • Comparison ab, altb, agtb, altb, agtb, a!b
  • Logical a b, a b , !b
  • Bitwise ab, ab, ab, a, agtgtb, altltb, ab,
    ab, altltb, altltb
  • Others ab, a() function call, ab array
    member, new a, new a, delete a, delete a,
    ab, a.b, a-gtb, a, a, a?bc

4
Operators in C / 2
  • Operator classification by number of operands
  • Unary -b (negative), !b, a, new a, new a,
    delete a, delete a, a, a--, a, --a, a, a,
    and etc
  • Binary ab, a-b, ab, a/b, ab, a b, a-b,
    ab, a/b, ab, ab, altb, agtb, altb, agtb,
    a!b, a b, a b, ab, ab, ab, agtgtb, altltb,
    ab, ab, altltb, altltb, ab, and etc
  • Ternary a?bc

5
Operators Precedence / 1
  • Precedence classifies all operators in to groups.
    An operator of higher precedence is evaluated
    first
  • x 2 4 2
  • a 2 x a 2

6
Operators Precedence / 2
int main() // example 1 int a 2 //
0b0101 int b 3 // 0b0011 int c 4 //
0b0100 int d 5 // 0b0101 int ans a
b c d // ans ? // example 2 int a
5 0b0101 int b 7 0b0111 bool ans a
b 2 // true or false
if (a b2)
7
Operators Associativity / 1
  • Associativity determines how operators of the
    same precedence are grouped in the absence of
    parentheses
  • Left-to-right, right-to-left

8
Operators Associativity / 2
9
Operators Associativity / 3
int main() // example 1 int a,b1 a b
3 // a ?, b ? // example 2 int
a2, b8 int c b / a 2 // c ? //
example 3 int b 2 b 3 // ? b
3 // ?
10
Operators with Classes
  • Which operators are defined for classes?
  • new, new, delete, delete create/destruct
  • a.b access class member
  • a-gtb access class member from a pointer to
    class object
  • a b default assignment operator
  • Default behavior can be overloaded
  • What about all others arithmetic, comparison,
    logical, bitwise?
  • There is no default behavior
  • Could be user-defined

11
OOP - OPerators
  • Operators Intro
  • Overloading operators
  • Streams
  • Examples

12
Why overload an operator?
  • Use class objects like primitive types
  • Programmer convenience
  • For example, overload arithmetic operators for
    complex classes like matrix
  • Reuse code that was written for the primitive
    types
  • For example, overload comparison operators (lt, gt,
    , !) for sorting
  • Create new behavior
  • Overload an array access operator for the
    LinkedList class and manage it like a simple
    array
  • Overload operator for string and use it as
    concatenator

13
How to overload an operator?
  • It is possible to overload operator of a specific
    class
  • An operator can emulated by a function
  • For example a b can be emulated in two ways
  • a.operator(b) // like a member function of the
    class
  • b can be anything not necessarily an object
    of the same class. For example, a1.
  • operator(a,b) // a regular function defined for
    a specific class
  • At least one of the operands a or b has to be
    an object. For example, 1b, a1
  • Rules
  • One of the objects has to be an object of the
    class
  • Cant define new operators
  • Cant change precedence/associativity

14
Example / 1
  • Overloading operator (member function)
  • Overloading operator (friend function)
  • class Matrix
  • public
  • Matrix (int m, int n)
  • Matrix (const Matrix other)
  • Matrix()
  • int get_cell(int m, int n) const
  • void set_cell(int m, int n, int val)
  • int getM() const return m
  • int getN() const return n
  • Matrix operator(const Matrix other)
  • friend Matrix operator(const Matrix a,
    const Matrix b)
  • private
  • int m,n
  • int vals

matrix.h
15
Example / 2
  • include matrix.h
  • Matrix Matrixoperator(const Matrix other)
  • Matrix temp Matrix(m,n)
  • for(int i0iltmi)
  • for(int j0jltnj)
  • temp-gtvalsij valsij
  • other.valsij
  • return temp
  • Matrix operator(const Matrix a, const Matrix
    b)
  • Matrix temp Matrix(a.m,b.n)
  • for(int i0ilta.mi)
  • for(int j0jltb.nj)
  • temp-gtvalsij 0
  • for(int l0llta.nl)
  • temp-gtvalsij
  • a.valsilb.valslj
  • return temp

matrix.cpp
16
A Friend Access Modifier
  • A friend access modifier is used in
    object-oriented programming to allow access to
    private or protected data in a class from outside
    the class
  • C supports friend class and friend function

class A class B public friend void foo(B
b) friend class A
17
Encapsulation Clarification
  • Friend modifier breaks the encapsulation
    principle
  • Per-class protection vs Per-object protection
  • In per-class protection class methods can access
    any member of any object of that class and not
    just the receiver (C)
  • MyClass a,b
  • a.foo(b) can access any fields/methods of b
  • In per-object protection class methods can access
    any member of the receiver object, but only
    public members of other objects of that class
    (Java)
  • MyClass a,b
  • a.foo(b) can access only public fields/methods
    of b

18
Can we manage without friend?
  • It is possible in classes where every private
    member have a selecting function (a getter)
  • include matrix.h
  • // remove this function declaration from the
    header file
  • Matrix operator(const Matrix a, const Matrix
    b)
  • Matrix temp Matrix(a.getM(),b.getN())
  • for(int i0ilta.getM()i)
  • for(int j0jltb.getN()j)
  • temp-gtset_cell(i,j,0)
  • for(int l0llta.getN()l)
  • temp-gtset_cell(i,j(temp-gtget_cell(i
    ,j)
  • a.get_cell(i,l)b.set_cell(l,j)
  • return temp

19
What can we overload?
- / ! lt gt
- / ltlt gtgt ltlt gtgt ! lt gt
-- , -gt -gt () what not ? .
. ? sizeof
20
Overloading Operator / 1
  • Default behavior is a bit-by-bit copy, like in
    copy-constructor
  • Has the same problem as the default
    copy-constructor
  • Generally, if there are pointers in class (data
    members), when all constructor, copy-constructor,
    destructor and operator should be overloaded

21
Overloading Operator / 2
  • class Student
  • public
  • Student(long id 0)
  • Student(char id)
  • Student(const Student other)
  • Student()
  • Student operator(const Student other)
  • long get_id()
  • void set_id (long id 0)
  • void set_id(char id)
  • char get_name()
  • void set_name(char fname)
  • private
  • long id
  • char fname

student.h
22
Overloading Operator / 3
  • include Student.h
  • include ltstring.hgt
  • Student Studentoperator(const Student other)
  • id other.id
  • fname new charstrlen(other.fname)1
  • strcpy(other.fname, fname)
  • return this
  • int main()
  • Student s1(12345)
  • Student s2
  • s2 s1

s2 s2.operator(s1)
student.cpp
23
Overloading unary operators
  • Unary operator
  • -a (negative), !a, a, new a, new a, delete a,
    delete a, a, a--, a, --a, a, a, and etc
  • Member function
  • MyClass operator (void)
  • Non-member (friend)
  • MyClass operator (const MyClass )

24
Overloading and --
  • Pre-increment and pre-decrement
  • a, --a
  • MyClass operator()
  • MyClass operator--()
  • Post-increment and post-decrement
  • a a--
  • Should be also const MyClass operator()
  • Should be also const MyClass operator--()
  • How to resolve the ambiguity
  • Post-increment and post-decrement use dummy
    parameter
  • const MyClass operator(int)
  • const MyClass operator--(int)

25
OOP - OPerators
  • Operators Intro
  • Overloading operators
  • Streams
  • Examples

26
Streams
  • C provides a new library for I/O called
    ltiostreamgt
  • ltiostreamgt provides the following classes
  • ostream (output stream)
  • write //block of data
  • put // char
  • flush
  • operatorltlt
  • istream (input stream)
  • get, getline
  • read
  • operatorgtgt
  • ltiostreamgt defines standard streams supported by
    OS
  • ostream cout (standard output)
  • ostream cerr (standard error)
  • istream cin (standard input)

27
Example / 1
  • include ltiostreamgt
  • include student.h
  • void Studentprint() const
  • cout ltlt ID ltlt id ltlt endl
  • if (fname ! NULL)
  • cout ltlt Full name ltlt fname ltlt endl
  • else
  • cout ltlt "Full name unspecified\n"
  • cout ltlt Started degree in ltlt start_year
    ltlt

  • \n\n"

student.cpp
28
Example / 2
  • include ltiostreamgt
  • include student.h
  • int main()
  • cout ltlt Enter id
  • long id 0
  • cin gtgt id
  • cout ltlt Enter full name
  • char fname new char20
  • cin gtgt fname
  • cout ltlt Enter first academic year
  • int year
  • cin gtgt year
  • Student s1(id)
  • s1.set_name(fname)
  • s1.set_year(year)
  • s1.print()
  • delete fname

student.cpp
29
Operator overloading with I/O
  • Stream operators ltlt and gtgt work on primitive
    types
  • What about classes?
  • No default behaviour, but can be defined
  • class Student
  • public
  • .....
  • friend istream operatorgtgt(istream in, Student
    s)
  • // replaces void print()
  • friend ostream operatorltlt(ostream out, Student
    s)
  • private
  • .....

30
Example / 3
  • ostream operatorltlt(ostream out, Student s)
  • outltltID ltlt s.id ltlt \n
  • if (fname ! NULL) out ltlt Full name ltlt
    s.fname
  • else out ltlt "Full name unspecified\n"
  • out ltlt Started degree in ltlt
    s.start_year
  • return out
  • istream operatorgtgt(istream in, Student s)
  • cout ltlt Enter id
  • in gtgt s.id
  • cout ltlt Enter full name
  • cin gtgt s.fname
  • cout ltlt Enter first academic year
  • in gtgt start_year
  • return in
  • int main()
  • Student s cin gtgt s cout ltlt s

student.cpp
31
OOP - OPerators
  • Operators Intro
  • Overloading operators
  • Streams
  • Examples

32
String Operators / 1
  • Overload operator to concatenate strings
  • Overload comparison operators

class String char chars int
length public String() String(char
value) String() String operator(const
String s) bool operatorgt(const String
s) String operator(const String s) char
getString() void setString(char value)
33
String Operators / 2
bool Stringoperatorgt(const string s)
return strcmp(m_str, s.m_str) gt 0
String Stringoperator(const String
s) int new_len length s.length 1 char
v new char new_len strcpy(v,
chars) strcat(v, s.chars) delete
chars chars v length new_len return
this
34
Vector2D Operators / 1
  • class Vector2D
  • private
  • double x, y
  • public
  • Vector2D(double x_init0, double y_init0)
  • double getX() const return x
  • double getY() const return y
  • friend Vector2D operator( const Vector2D
    left,
  • const Vector2D
    right)
  • double operator(int index) const
  • friend ostream operatorltlt(ostream ostr,
  • const Vector2D v)

35
Vector2D Operators / 2
  • friend Vector2D operator (const Vector2D left,
  • const Vector2D
    right)
  • return Vector2D(left.xright.x,left.yright.y
    )
  • double operator(int index) const
  • return (index 0)? xy
  • friend ostream operatorltlt(ostream ostr,
  • const Vector2D v)
  • return ostr ltlt ( ltlt v.x ltlt , ltlt v.y ltlt
    )
  • //usage
  • Vector2D a(3,2), b(1)
  • Vector2D c ab
  • cout ltlt ( ltlt c.getX() ltlt , ltlt c.getY() ltlt
    )

36
Member vs. Non-Member operators
  • Use members
  • When returning a reference
  • Assignment operators
  • Unary operators, usually
  • Use friends for
  • Symmetric operators (arithmetic, comparison,
    logical)
  • Matrix m1 double arr
  • m1 arr // can work as a member function
  • arr m1 // cant work as a member function,
    since the first element isnt a Matrix. Use
    friend function
  • Operators on classes which cannot be altered
  • istream, ostream

37
Danger in overloading operators
  • Operator overloading changes the usual and
    expected behavior
  • Could be easily misused by a programmer who is
    unaware about the change
  • Operator overloading should be used with care,
    only in cases where it is needed
  • When overloading operators try to keep an
    expected behavior as much as possible

38
HANDS-ON ZONE
Write a Comment
User Comments (0)
About PowerShow.com