0% found this document useful (0 votes)
6 views24 pages

Lecture 6 Class Hierarchies

Oobject oriented programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Lecture 6 Class Hierarchies

Oobject oriented programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Defining Class Hierarchy

Defining a simple SubClass

OOP, Muhammad Ehsan


Subclasses and Superclasses
• Subclass: A class that inherits from another class (superclass)
• Superclass: A class that is inherited from by another class (subclass)
• Single inheritance: A subclass inherits from one superclass
• Multiple inheritance: A subclass inherits from multiple superclasses

OOP, Muhammad Ehsan


Code Example Superclass
#include <iostream>
using namespace std; • This code defines a class named
class Super {
private:
Super
int storage;
public:
• The class has a private member
void put(int val) {
storage = val;
variable named storage
}
int get() {
• The class has two public member
return storage; functions: put() and get()
}
};
int main() {
• The put() function stores a value in
Super object; the storage variable
object.put(100);
object.put(object.get() + 1); • The get() function returns the value
cout << object.get() << endl;
return 0; stored in the storage variable
}

OOP, Muhammad Ehsan


Subclasses and Inheritance Syntax
• In object-oriented programming, a subclass inherits properties and
methods from a superclass. This concept is called inheritance and
allows for code reuse and creating hierarchical relationships between
classes. This slide introduces the syntax for defining a subclass in C++.

OOP, Muhammad Ehsan


Subclass Syntax Breakdown
• class Y : visibility_specifier X { ... };
• This syntax defines a class named Y as a subclass of a superclass
named X.
• The colon (:) separates the subclass name from the superclass.
• Optionally, a visibility specifier can be used to control access to
inherited members (we'll discuss this later).

OOP, Muhammad Ehsan


Multiple Superclasses
• class A : X, Y, Z { ... };
• You can inherit from multiple superclasses by listing them separated
by commas.
• This is called multiple inheritance and can be a complex concept.

OOP, Muhammad Ehsan


Simple Subclass Example
class Sub : Super { };

int main() {
Sub object;

object.put(100);
object.put(object.get() + 1);
cout << object.get() << endl;
}
• This code defines a class named Sub that inherits from a class named Super.
• The Sub class doesn't introduce any new variables or functions.
OOP, Muhammad Ehsan
Private Inheritance by Default
• Compilation Errors
• Compiling the previous code without a visibility specifier results in
errors.
• Why? Private inheritance is assumed by default.

OOP, Muhammad Ehsan


Specifying Public Inheritance
• class Sub : public Super { };
• Use the public visibility specifier to inherit publicly.
• This preserves the access specifiers of the Super class members.

OOP, Muhammad Ehsan


Private Members Remain Private
• Public inheritance doesn't change private members
• Private members of the Super class remain private in the Sub class.

OOP, Muhammad Ehsan


Subclass Access and Protected Members
• Subclasses inherit properties and methods from their superclasses.
However, by default, they lose access to private members of the
superclass. This slide introduces the concept of protected members,
which offer a way for subclasses to access inherited members that are
not publicly exposed.

OOP, Muhammad Ehsan


Protected Access Specifier
• protected keyword
• Members declared as protected are accessible:
• Within the subclass itself
• Within any class publicly derived from the subclass
• Not accessible from outside the class hierarchy

OOP, Muhammad Ehsan


#include <iostream>
Protected Member Example
using namespace std;

class Super {
int main() {
protected:
Sub object;
int storage;

public:
object.put(100);
void put(int val) {
object.put(object.get() + 1);
storage = val;
object.print();
}
}
int get() {

return storage; • This code demonstrates the use of the


} protected access specifier.
};

class Sub : public Super {


• The Super class has a protected
public: member variable named storage.
void print() {
• The Sub class, which inherits publicly
cout << "storage = " << storage << endl;

}
from Super, can access the storage
}; variable through its member functions.
OOP, Muhammad Ehsan
Protected vs Private Members
• Protected members: Accessible within the subclass hierarchy
• Private members: Only accessible within the class itself

OOP, Muhammad Ehsan


Restrictions on Protected Members
• Protected members remain inaccessible from outside the class
hierarchy
• You cannot directly access a protected member of a superclass from
the main function or other unrelated code.

OOP, Muhammad Ehsan


Benefits of Protected Members
• Controlled Access: Subclasses can access and potentially modify
inherited data while maintaining encapsulation.
• Code Reusability: Protected members promote code reuse by
allowing subclasses to leverage functionality from the superclass.

OOP, Muhammad Ehsan


Summary of Class Member Visibility and
Inheritance
• We've previously discussed public and private member visibility
within a class. This slide highlights that these keywords (public,
private, protected) can also be used to specify how inherited
members are accessed in a subclass. The provided table summarizes
the different combinations of member visibility and inheritance, along
with the resulting access level in the subclass.

OOP, Muhammad Ehsan


Understanding the Table
• The table shows all possible combinations of
member visibility (public, protected, private)
and inheritance model (public, protected,
private).
• Each row represents a scenario where a
member is declared with a specific visibility
within a superclass and then inherited by a
subclass with a specific inheritance model.
• The third column ("Resulting Access in
Subclass") shows how the member is
accessed within the subclass.
• Read the table like this: "If a component is
declared as public and its class is inherited
as public, the resulting access is public."

OOP, Muhammad Ehsan


Understanding the Table
• Public/Public: Inherited members remain
public in the subclass.
• Protected/Public: Inherited members
become protected in the subclass.
• Private/Public: Inherited members become
private in the subclass (essentially
inaccessible).
• Public/Protected, Protected/Protected:
Inherited members become protected in the
subclass.
• Private/Protected, Public/Private,
Protected/Private, Private/Private: Inherited
members become private in the subclass
(essentially inaccessible).

OOP, Muhammad Ehsan


Importance of the Table
• This table is a valuable tool for understanding how inheritance and
member visibility interact.
• It helps predict how members will be accessed within a subclass
based on their declaration in the superclass and the inheritance
model used.
• By understanding this table, you can effectively design class
hierarchies that manage member visibility appropriately.

OOP, Muhammad Ehsan


Multi-Inheritance
• Multi-inheritance allows a
subclass to inherit properties
and methods from multiple
superclasses. While it can offer
some flexibility, it can also lead
to complexity and ambiguity in
class hierarchies. This slide
introduces the concept of multi-
inheritance and highlights the
potential drawbacks associated
with it.
OOP, Muhammad Ehsan
Multi-Inheritance: A Cautionary Tale
• Multi-inheritance is considered error-prone and can obfuscate class
hierarchies.
• It can lead to the "diamond problem," where a subclass inherits the
same member from two different superclasses, potentially causing
conflicts.
• Modern programming languages often avoid multi-inheritance
altogether.

OOP, Muhammad Ehsan


Simple Multi-Inheritance Example
• This code demonstrates a simple
example of multi-inheritance.
• We define two superclasses, SuperA
and SuperB, each with protected
member variables and public member
functions.
• A subclass named Sub inherits publicly
from both SuperA and SuperB.
• The Sub class has a print() function
that accesses and prints the inherited
member variables from both
superclasses.
OOP, Muhammad Ehsan
#include <iostream>

using namespace std; class Sub : public SuperA, public SuperB {


class SuperA { public:
protected: void print() {
int storage; cout << "storage = " << storage << endl;

public: cout << "safe = " << safe << endl;

void put(int val) { }

storage = val; };

}
int main() {
int get() {
Sub object;
return storage;

}
object.put(1);
};
object.insert(2);
class SuperB {
object.put(object.get() + object.takeout());
protected:
object.insert(object.get() + object.takeout());
int safe;
object.print();
public:
}
void insert(int val) {

safe = val;

int takeout() { Sample Output


return safe;

} storage = 3
OOP, Muhammad Ehsan
}; safe = 5

You might also like