Professional Documents
Culture Documents
BSE II
Lecture 03
= +
Basic Terminology
Abstraction is the representation of the essential
features of an object. These are ‘encapsulated’ into an
abstract data type.
Encapsulation. There should be tight coupling
between data and methods that operate on this data.
Such data should be hidden from the code outside the
current context. This process of data hiding and
combining data and methods in a single logical unit is
called encapsulation. The internal state is usually not
accessible by other objects.
Basic Terminology:
Inheritance
Inheritance means that one class inherits the
characteristics of another class.
This is also called a “is a” relationship:
A car is a vehicle
A dog is an animal
A teacher is a person
Polymorphism
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
... } object_names;
Class
return 0;
}
Destructors
The destructor fulfills the opposite functionality.
It is automatically called
When an object is destroyed, either because its scope of
existence has finished (for example, if it was defined as a
local object within a function and the function ends)
When an object dynamically assigned and it is released
using the operator delete.
The destructor must have the same name as the class,
but preceded with a tilde sign (~) and it must also
return no value.
#include <iostream> CRectangle::~CRectangle
using namespace std; ()
class CRectangle
{
{
int *width, *height; delete width;
public: delete height;
CRectangle (int,int);
~CRectangle (); }
int area () int main ()
{
return (*width * *height);
{
} CRectangle rect (3,4);
}; CRectangle rectb (5,6);
CRectangle::CRectangle (int a, int b)
{ cout << "rect area: " <<
width = new int; rect.area() << endl;
height = new int; cout << "rectb area: " <<
*width = a; rectb.area() << endl;
*height = b;
} return 0;
}
The use of destructors