11-oo-objects plc
11-oo-objects plc
Programming Languages:
OO Paradigm, Objects
Computer Engineering,METU
15 April 2008
Programming Languages:OO Paradigm, Objects
Outline
Abstraction
Encapsulation
Hiding
Inheritance
Programming Languages:OO Paradigm, Objects
Object Oriented Programming
Encapsulation/Scope
Person
Objects consist of: name
attributes (member variables) surname
methods (member functions) no
encapsulated in a package scope getname()
setno()
attributes: state of objects
methods: behaviour of objects
alternative terminalogy: messages
call a method ≡ send message to an object
A class is the family for similar objects.
An object is an instance of a class.
Programming Languages:OO Paradigm, Objects
Object Oriented Programming
class P e r s o n {
char name [40] , surname [40];
int no ;
public :
const char * getname () { return name ;}
void s e t n o ( int );
} obj ;
void P e r s o n :: s e t n o ( int a ) {
no = a ;
}
Hiding
Abstraction
Constructors
Heap Objects
Array allocation/deallocation:
Person *p=new Person[100];
delete [] p;
Programming Languages:OO Paradigm, Objects
Constructors/Destructors
Destructors
Destructors
Destructor does not solve all problems with objects with heap
members:
Semantics of assignment
Semantics of parameter passing
Semantics of return value
Initialization
Default behaviour of C++ is copy member values byte by
byte.
Java assigns/passes by reference. No copying.
C++ Solution: implement your own semantic by Copy
constructor and overloading assignment operator.
Assignment operator destroys an existing object and replaces
with the data from new one, copy constructor copies data into
an empty object.
Programming Languages:OO Paradigm, Objects
Constructors/Destructors
Copy Constructor
Copy Constructor
class L i s t {
struct Node { int x ; Node * n e x t } * head ;
public : L i s t () { head =NULL;}
L i s t ( c o n s L i s t &); // Copy c o n s t r u c t o r
~ L i s t ();
};
void p a s s b y v a l u e ( L i s t a) {
...
Copy Constructor
}
L i s t r e t u r n a s v a l u e ( L i s t &a) {
L i s t b=a; Copy Constructor, explicit
...
return a;
}
...Copy Constructor
p a s s b y v a l u e (c);
...
d= r e t u r n a s v a l u e ( c );
...
Programming Languages:OO Paradigm, Objects
Constructors/Destructors
Copy Constructor
const Keyword
1 p[3]=’a’; ×
√
2 q[3]=’a’;
√
3 p++;
4 q++; ×
const char * const p
Operator Overloading
All C++ operators except ‘.’ , ‘?:’, ‘::’, ‘.*’ and ‘->*’
For unary operators:
1 void ClassName::operator++();
2 void operator++(ClassName &a);
For binary operators:
1 void ClassName::operator&&(int a);
2 void operator&&(int a,ClassName &b);
First versions are member functions, can exist private
members. Only operand in unary case, LHS in binary case is
the current object
Second versions are outside of the definition. You need friend
declaration if they need to access private members.
Programming Languages:OO Paradigm, Objects
Operator Overloading
R a t i o n a l a ,b , c ; Hash h , j ; P o i n t e r p ,* q;
a +b; a +3; if (a <b) ... ; !a;
++ a ; a ++; x =( double ) a ;
x =* p; q= new P o i n t e r ; delete q;
Programming Languages:OO Paradigm, Objects
Operator Overloading
R a t i o n a l a ,b; Hash h , j ;
i = i +a;
++ a ;
c o u t << a ; c o u t << 3 << a << b ;
c i n >> b;
h += a ;
Programming Languages:OO Paradigm, Objects
Operator Overloading
Friends
Friends
Implementation of Objects
class Person
char name[40] 40*sizeof(char)
int id sizeof(int)
char * getname() sizeof(char *(*)())
void print() sizeof(void (*)())