Inheritance

bEgInSlIdE
Inheritance
What is common to a Circle, a Rectangle, a Triangle, & a Diamond:
1.
2.
3.
4.
5.
6.
7.
Translate
Rotate
Scale
Line thickness
Lines style
Outline color
Fill color
These are all shapes
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance
What is different between a Circle, a Rectangle, a Triangle, & a Diamond:
1. Different geometric
parameters.
2. Possibly different rotations.
3. Different drawing method
4. Different area formula
5. Different circumference
formula
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (example)
class crv_obj : public attr_obj
{
..
.
public:
crv_obj();
crv_obj( const crv_obj *_obj );
~crv_obj();
..
.
};
class srf_obj : public attr_obj
{
..
.
public:
srf_obj();
srf_obj( const srf_obj *_obj );
~srf_obj();
..
.
};
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (cont.)
class attr_obj : public list_obj
{
protected:
list_obj *attr;
...
public:
attr_obj( size_t s = 0 );
attr_obj( const attr_obj *a, size_t s = 0 );
virtual ~attr_obj();
void
void
void
void
};
..
.
set_attr( string_type nm, object_type *ob );
set_attr( string_type nm, string_type st );
set_attr( string_type nm, real_type fl );
set_attr( string_type nm, int in );
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (cont.)
class list_obj : public object_type
{
list_obj *next;
list_obj *prev;
...
public:
/* Constructor. */
list_obj( size_t s );
list_obj( const list_obj *l, size_t s );
~list_obj();
...
};
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (cont.)
class object_type : public memory_block_type
{
obj_info_type *t_tag;
...
public:
object_type( size_t s = 0 );
/* General functions for all objects. */
boolean_type
has_links() const;
boolean_type
has_attrs() const;
inline obj_info_type
*obj_info() const;
};
/* Polymorphic methods. */
virtual void
dbg_obj( int lvl = 0 ) const;
void
pr_obj( int lvl = 0 ) const;
...
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (cont.)
class memory_block_type
{
static void
*last_allocated_block[4];
static void
*last_freed_block;
unsigned long m_size;
void
set_freed();
public:
memory_block_type( size_t s );
~memory_block_type();
void
void
};
...
*operator new( size_t s );
operator delete( void *mp );
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
Inheritance (cont.)
crv_obj / srf_obj
attr_obj
list_obj
object_type
memory_block_type
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
class Employee {
class Employee *next;
public:
Employee(class Employee *n = NULL): next(n) {};
class Employee *advance() { return next; };
virtual void print() { printf("Employee\n"); };
};
class Engineer: public Employee {
public:
Engineer(class Employee *n = NULL): Employee(n) {};
};
class SalesPerson: public Employee {
public:
SalesPerson(class Employee *n = NULL): Employee(n) {};
virtual void print() { printf("SalesPerson\n"); };
};
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
void main() {
Employee *e,
*e6 = new
*e5 = new
*e4 = new
*e3 = new
*e2 = new
*e1 = new
Employee(),
SalesPerson(e6),
Engineer(e5),
Engineer(e4),
SalesPerson(e3),
SalesPerson(e2);
e = e1;
do {
e -> print();
}
while ((e = e -> advance()) != NULL);
};
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE
class Employee {
class Employee *next;
public:
Employee(class Employee *n = NULL): next(n) {};
class Employee *advance() { return next; };
void print() { printf("Employee\n"); };
};
class Engineer: public Employee {
public:
Engineer(class Employee *n = NULL): Employee(n) {};
};
class SalesPerson: public Employee {
public:
SalesPerson(class Employee *n = NULL): Employee(n) {};
virtual void print() { printf("SalesPerson\n"); };
};
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bEgInSlIdE#include
<iostream>
using namespace std;
class A {
public:
void print(){ cout << "print A" << endl; };
};
class B: public A {
public:
virtual void print(){ cout << "print B" << endl; };
};
class C: public B {
public:
void print(){ cout << "print C" << endl; };
};
int main() {
A *a = new C();
B *b = new C();
a->print(); // will print 'print A'
b->print(); // will print 'print C’
}
Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept.
Technion
bEgInSlIdE
Question
How can we ‘implement’
access and activation of
virtual function(s), in real
time?
Copyright
1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion