Upload
ledat
View
222
Download
1
Embed Size (px)
Citation preview
Programming / Object Oriented Programming Slide 32
Object Oriented Programming
OOP is a programming paradigm.
It's key features are:
Information hiding,
Data abstraction,
Encapsulation,
Modularity,
Polymorphism,
and inheritance.
Most programming languages developed since the 1990 support OOP. For example: C++, Java, C#, Ruby, Eiffel, Ada95, ...
Programming / Object Oriented Programming Slide 33
OOP Vocabulary (I)
Interface: (What something can do)
The public part, the user accessible part.
Implementation: (How something does something)
The private part, carrying the functionality and information.
Encapsulation:
The internal state (data objects) of an object is shielded from external access to make it impossible change the state of an object in an uncontrolled way. Rather, other objects must use the public interface of the object to change its state. Encapsulation is achieved by specifying which classes may use the members of an object.
The object can therefore give “guarantees” about its internal state.
Ideally, the internal implementation can changeindependent of the external interface.
Programming / Object Oriented Programming Slide 34
OOP Vocabulary (II)
Abstraction:
Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features).
Polymorpishm:
Polymorphism is the ability of one type, D, to appear as and be used like another type, B.
For example: D is a subclass of B. Then D extends B and therefore can act as an B. D “is-a” B.
Methods being called at runtime depend on the actual type of the object (D), not the apparent type (B).
Programming / Object Oriented Programming Slide 35
OOP Vocabulary (III)
Class:
Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features).
Object:
A pattern (exemplar) of a class.
Instance:
One can have an instance of a class or a particular object. The instance is the actual object created at runtime. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class.
Programming / Object Oriented Programming Slide 36
OOP Vocabulary (IV)
Methods:
Functions the object provides, i.e. an object's abilities.
Inheritance:
"Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.
Access specifiers:
Private – only accessible by the object itself
Protected – private for everyone, but accessible for subclasses
Public – accessible for everyone
Friend – a method/object that gets full access to protected and private fields
Programming / Object Oriented Programming Slide 37
Classes in C++ (I)
A simple class:
Instances are created by calling the constructor which initializes all member variables (usually in the order they are specified).
The destructor is responsible for cleaning up when the object is destroyed.
class Complex {public:
Complex(double r, double i) :real(r), img(i) { } // Constructor
~Complex() { real = 0; img = 0; } // Destructor
private:double real, img; // data members
};
Programming / Object Oriented Programming Slide 38
Classes in C++ (II)
Introducing methods:
Method qualifiers tell the compiler to use a method when encountering an instance of the class which has the selected property.
class Complex {public:
Complex(double r, double i) :real(r), img(i) { } // Constructor
~Complex() { } // Destructordouble abs() const { return sqrt( real*real +
img*img ); }double imgV() const { return img; }double& imgV() { return img; }
private:double real, img;
};
Programming / Object Oriented Programming Slide 39
Classes in C++ (III)
Example:
The compiler decides which version to call, the programmer has to supply them. If you do not code “const” versions of your methods, you cannot use them with a const object.
Within a “const” method, the object itself is “const” and cannot be modified. Rule: Make all methods “const” if possible.
void doSomething( const Complex& c, Complex& d ) {double v = c.abs(); // OKdouble w = d.abs(); // OK
v = c.imgV(); // OK, calls const versionw = d.imgV(); // OK, calls normal version
c.imgV() = 1.1; // FAILS “cannot assign lefthandvalue”d.imgV() = 1.1; // OK, modifies d.img
}
Programming / Object Oriented Programming Slide 40
Declaration and Definition (I)
Declaration: Interface specification
Defintion: Implementation details
class Complex {public:
// rest as beforevoid conjugate();
};
#include “complex.h”
void Complex::conjugate() {img *= 1;
}
complex.cpp
complex.h
Programming / Object Oriented Programming Slide 41
Declaration and Definition (II)
Header files should not be included more than once. First, it's un-economical. Second, the compiler can't know that it's the same declaration and generates an error (“multiple declaration of ...”).
Solution: “Include Guards”:
Choose a unique name (FILENAME_H) and define the symbol with the preprocessor command. Next time the file is included, the #ifndef statement will be false and thus the text till #endif ignored.
#ifndef COMPLEX_H#define COMPLEX_H// class declarationclass complex {
//...};#endif
complex.h
Programming / Object Oriented Programming Slide 42
OOP in C++ (I)
A Base class:
A Base class must declare all polymorphic functions “virtual”, the derived class then declares exactly the same function.
If a class is to be used as a base class, it should declare the destructor “virtual”, so the correct one is called at runtime.
class Base {public:
Base(int I) : v(i) { }virtual ~Base() { }
virtual void x() = 0; // pure virtual functionvirtual void y(int i) { … } // virtual function with
// default implementationprotected:
int v;};
Programming / Object Oriented Programming Slide 43
OOP in C++ (II)
The derived class
The constructors of a derived class should first call the corresponding constructors of the base class with proper arguments, then initialize member variables.
With “public” inheritance, all methods and data structures of the base class are accessible in the derived class if not declared “private”.
class Derived : public Base {public:
Derived(int i) : Base(i) { } // call Base constructor~Derived() { } // Base destructor is called
// automaticallyvoid x() { /* do something */ }void y(int i) { /* do something else */ }
}; Base
Derived
Programming / Object Oriented Programming Slide 44
OOP in C++ (III)
Example:
The constructor of the Base class is executed first, then the destructor of the Derived class.
Destructors are executed in the opposite order. Without the virtual destructor in class Base, the delete statement would not call the destructor in Derived, but the Base destructor. This would entail all kinds of negative effects ( memory leaks, crashes, … ).
void my_function() {Derived d* = new Derived(1); // an instance of DerivedBase* b = d; // disguised as a Base object
d>x(); d>y(1); // calls Derived::x() and Derived::y(int)b>x(); d>y(2); // so do these
delete b;}