1 Object-Oriented Programming Object: external or problem-oriented view: an object can represent...
-
date post
21-Dec-2015 -
Category
Documents
-
view
231 -
download
0
Transcript of 1 Object-Oriented Programming Object: external or problem-oriented view: an object can represent...
1
Object-Oriented Programming
Object:external or problem-oriented view: an object can represent any entity in the solution of a problem; objects interact by sending messages to each other.internal or implementation-oriented view: each object is like a separate computer with its own code and its own private memory to hold values.
2
A drawing application Diagrams are built out of shapes.
Shapes can be grouped into classes based on their properties.
Shapes in the list draw and align themselves next to each other from left to right.
3
Class definition for Diagram
The class definition contains the variables for its objects, and the methods that are executed when the object receives a message.
4
The method definition (Pseudo code)
class Diagram: method initialize;shapelist := a new empty list of shapes;angle := 0;
class Diagram: method setangle(a);angle := a;
class Diagram: method add(shape s);send message setalign(angle) to s;add s to the end of shapelist;
class Diagram: method draw;previous := a new dummy shape;for each shape s on shapelist do
previous := the result of sending draw(previous) to s;
5
A class hierarchy
The converse of subclass is superclass. A subclass S of a class C is depicted as a child of C
in the class hierarchy.
7
An object of a subclass can implement a message in its own way by supplying its own method to be executed in response to the message.Example:
class Text: method draw(previous) returns Shape;center string on previous;return previous;
Class Ellipse: method draw(previous) returns Shape;center := center of this ellipse relative to previous;lay out an ellipse centered at center;return this ellipse object;
Inheritance
8
inheritance changes: If all interactions are through the interface to a superclass, then the program can be extended by adding subclasses.
Class Ellipse: method draw(previous) returns Shape;P := result of sending offset(align) to previous;this ellipse computes E from P and other ellipse informati
on;
Information hiding for extensibility
9
Adding a subclass
Example: Allow a tree-like diagram, in addition to a linear list of shapes.
Example of non-OO way:procedure draw(diagram d);{ for each shape s in diagram d do begin
case s ofBOX: code to draw a box;ELLIPSE: code to draw an ellipse;
…..}
11
Adding a subclass (cont)
Class Brace: method draw(previous) returns Shape;
send draw(previous) to adiagram;
return previous; Class Diagram: method draw(init);
previous := init;
for each shape s on shapelist do
previous := the result of sending
draw(previous) to s; Class Diagram: method draw;
sending draw(a new shape) to this diagram object;
13
Virtual function
virtual function (in the base class): allow a derived class to supply the function body; taken from the derived class where possible.
Example:
Shape *s = new Ellipse;
Shape *p = ……
s->draw(p)//s->Ellipse::draw(p);
14
C++ for the drawing application
main( ){Diagram *d = new Diagram;d->setangle(-4);d->add(new Box(1.00, 0.25));d->add(new Text(“C++ program”));d->draw( );}
void Diagram::draw( ){Shape * prev = new Shape;Shape * s;…for each shape s in the list {…
prev = s->draw(prev);}}
15
Another Example for the Virtual Function class B {
public: virtual char f() { return ‘B’;}char g() { return ‘B’;}char testF() { return f( );}char testG() { return g( );}
} Class D: public B {
public: char f( ) {return ‘D’;}char g( ) {return ‘D’;}
} Main( ) { D d;
print d.testF( ), d.testG( );}=>The answer is ‘D’, ‘B’.
16
Public base class
public base classes in C++ has the class declaration: class < derived > : public < base > { < member-declarations > }; An object of a derived class can appear wherever an
object of a public class is expected. Members of a public base class retain their
accessibility in the derived class. A public member of the base class is a public member of
the derived class, similarly for private members.
17
Private base class
private base class has the class declaration: class < derived > : private < base > { < member-declaration > } ;
A derived class simply shares the code of the private base class. Such code sharing is sometimes called implementation inheritance.
By default, all members inherited by < derived > from < base > become private members of <derived > . Nonprivate inherited members can be made visible by wri
ting their full names in the derived class.