CPS 235 Object Oriented Programming Paradigm: Inheritance
CPS 235 Object Oriented Programming Paradigm: Inheritance
Oriented
Programming
Paradigm
Inheritance
Computer Science
Contents
• Composition (or containership)
– Objects as Members of Classes
• Concept of Inheritance
• Levels of access control
Class A
Features: a,b,c
Features: a,b,d,e
c d
e
Class B Class C
Features: a,b,d,e,f
f
Class D
6
CPS235:Inheritance
Computer Science
Inheritance and
Encapsulation
• private member
– Is accessible only via the base class
• public member
– Is accessible everywhere (base class, derived
class, othe classes)
• protected member
– Is accessible by the base class and derived classes
Computer Science
Inheritance Concept
class Rectangle{
Polygon private:
int width, length;
public:
void set(int w, int
Rectangle l);
Triangle int area();
};
where
– access-level specifies the type of derivation
• private by default, or
• public or
• protected (used very rarely)
class Point{
protected: int x, y;
public: void set(int
a, int b);
d e r i v e d c l a s };s / s u b c l a s s /
c h i l d c l a s class
s Circle : public
Point{
… …
};
Computer Science CPS235:Inheritance 15
Member Access Control
• The type of inheritance defines the minimum access level for the
members of derived class that are inherited from the base class
• With public inheritance, the derived class follows the same
access permission as in the base class
• With protected inheritance, only the public members inherited
from the base class can be accessed in the derived class as
protected members
• With private inheritance, none of the members of base class is
accessible by the derived class
Computer Science
Access Rights of Derived
Classes
• Take these classes as examples:
class B { /*...*/ };
class D_priv : private B { /*...*/ };
class D_prot : protected B { /*...*/ };
class D_publ : public B { /*...*/ };
class UserClass { B b; /*...*/ };
• None of the derived classes can access anything that is private
in B
• In D_priv, the public and protected parts of B are private
• In D_prot, the public and protected parts of B are protected
• In D_publ, the public parts of B are public and the protected
parts of B are protected (D_publ is-a-kind-of-a B)
• class UserClass can access only the public parts of B, which
"seals off" UserClass from B
Computer Science CPS235:Inheritance 18
protected vs. private
So why not always use protected instead of private?
Computer Science
Encapsulation
class Figure Circle::Circle(int x, int
{ y, int radius)
protected: {
int x_, y_; x_ = x;
}; y_ = y;
radius_ = radius;
class Circle : public }
Figure
{ int main()
private: {
int radius_; Circle a(0,0,10);
public: }
Circle(int x, int
y, int radius);
};
Computer Science
Encapsulation
class Figure Circle::Circle(int x, int
{ y, int radius)
private: {
int x_, y_; x_ = x;
}; y_ = y;
radius_ = radius;
class Circle : public }
Figure
{ int main()
private: {
int radius_; Circle a(0,0,10);
public: }
Circle(int x, int
y, int radius);
};
Computer Science
Encapsulation class Circle : public
class Figure
Figure
{ {
private: private:
int x_, y_; int radius_;
public: public:
void SetX(int x); Circle(int x, int
void SetY(int y); y, int radius);
};
void Figure::SetX(int x) };
{ Circle::Circle(int x,
x_ = x; int y, int radius)
} {
void Figure::SetY(int y) SetX(x);
{ SetY(y);
y_ = y; radius_ = radius;
}
}
int main()
{
Circle a(0,0,10);
Computer Science
}
What to Inherit?
• In principle, every member of a base class is
inherited by a derived class
– just with different access permission