0% found this document useful (0 votes)
11 views

Module 3 - Inheritance An Polymorphism

Uploaded by

Keerthi G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 3 - Inheritance An Polymorphism

Uploaded by

Keerthi G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Module 3:

6.2 Constructors
• A constructor is a ‘special’ member // class with a constructor
function whose task is to initialize the class integer
data members of an object of its class. {
• It is special because its name is the int m, n; //data members
same as the class name. public:
• The constructor is invoked whenever integer(void); // constructor declared
an object of its associated class is .....
created. .....
• It is called constructor because it };
constructs the values of data members integer :: integer(void) // constructor defined
of the class. {
m = 0; n = 0;
}
The constructor functions have some special characteristics.They are :

• They should be declared in the public section.


• They are invoked automatically when the objects are created.
• They do not have return types, not even void and therefore, and they cannot return
values.
• They cannot be inherited, (though a derived class can call the base class constructor)
• Like other C++ functions, they can have default arguments.
• Constructors cannot be virtual.(Meaning of virtual will be discussed later in Chapter 9.)
• An object with a constructor (or destructor) cannot be used as a member of a union.
• They make ‘implicit calls’ to the operators new and delete when memory allocation is
required.
Constructor Example
A program that demonstrates default constructors is given as
follows.
#include <iostream> Void DemoDC::display()
using namespace std; {
class DemoDC cout<<"num1 = "<< num1 <<endl;
{ private: cout<<"num2 = "<< num2 <<endl;
int num1, num2 ; }
public: int main()
DemoDC() {
{ DemoDC object;
num1 = 10; num2 = 20; object.display();
} return 0;
void display() ; }
};
Destructors
• A destructor, as the name implies, is used to destroy the objects that have been
created by a constructor.
• Like a constructor, the destructor is a member function whose name is the
same as the class name but is preceded by a tilde.
• For example, the destructor for the class integer can be defined as shown below:
~integer( ) { }
• A destructor never takes any argument nor does it return any value. It will be
invoked implicitly by the compiler upon exit from the program (or block or
function as the case may be) to clean up storage that is no longer accessible.
• It is a good practice to declare destructors in a program since it releases memory
space for future use.
For examples: refer online gdb program
• For Parameterised Constructor and copy constructor concept
• refer online gdb program
*.1 Introduction
• Reusability is yet another important feature of OOP. It is always nice if we could reuse something that already
exists rather than trying to create the same all over again.
• Fortunately, C++ strongly supports the concept of reusability. The C++ classes can be reused in several ways.
Once a class has been written and tested, it can be adapted by other programmers to suit their requirements.
• This is basically done by creating new classes, reusing the properties of the existing ones.
• The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is
referred to
• A derived class with only one base class, is called single inheritance and one with several base classes is
called multiple inheritance.as the base class and the new one is called the derived class or subclass.
• the traits of one class may be inherited by more than one class. This process is known as hierarchical
inheritance.
• The mechanism of deriving a class from another ‘derived class’ is known as multilevel inheritance
Forms of Inheritance
Defining derived class
A derived class can be defined by specifying its relationship with the base class in addition to its own details.
The general form of defining a derived class is:

• The colon indicates that the derived-class-name is derived from the base-class-name. The visibility-mode is
optional and, if present, may be either private or public. The default visibility-mode is private. Visibility
mode specifies whether the features of the base class are privately derived or publicly derived.
Examples
• When a base class is privately inherited by a derived class, ‘public members’ of
the base class become ‘private members’ of the derived class and therefore the
public members of the base class can only be accessed by the member functions
of the derived class. They are inaccessible to the objects of the derived class.
• On the other hand, when the base class is publicly inherited, ‘public members’ of
the base class become ‘public members’ of the derived class and therefore they
are accessible to the objects of the derived class.
• In both the cases, the private members are not inherited and therefore, the private
members of a base class will never become the members of its derived class.
Single Inheritance
• Refer 8.2 Single public inheritance in online gdb
• And Single private inheritance in online gdb
• Program Explanation:
• The class D is a public derivation of the base class B. Therefore, D inherits all the public members of B and
retains their visibility. Thus, a public member of the base class B is also a public member of the derived class
D. The private members of B cannot be inherited by D.
Public derivation vs Private derivation
Private derivation
Program 8.2 Single inheritance : Private

• For explanation and example program, refer online GDB my projects


8.4 Making a private inheritable (using
protected mode)
• We have just seen how to increase the capabilities of an existing class without modifying
it. We have also seen that a private member of a base class cannot be inherited and
therefore it is not available for the derived class directly.
• What do we do if the private data needs to be inherited by a derived class? This can be
accomplished by modifying the visibility limit of the private member by making it public.
• This would make it (private data) accessible to all the other functions (becoming a
public) of the program, thus taking away the advantage of data hiding.
• C++ provides a third visibility modifier, protected, which serve a limited purpose in
inheritance.
• A member declared as protected is accessible by the member functions within its class
and any class immediately derived from it.
• It cannot be accessed by the functions outside these two classes.
Syntax: A class can now use all the three
visibility modes as illustrated below:
class alpha
{
private: // optional
......// visible to member functions
......// within its class
protected:
......// visible to member functions
......// of its own class and that of derived class
public:
......// visible to all functions
......// in the program
};
When a protected member is inherited in public mode, it becomes
protected in the derived class too and therefore is accessible by the
member functions of the derived class. It is also ready for further
inheritance.
A protected member, inherited in the private mode derivation, becomes
private in the derived class.
• Now let us review the access control to the private and protected members of a
class. What are the various functions that can have access to these members? They
could be:
• 1. A function that is a friend of the class.
• 2. A member function of a class that is a friend of the class.
• 3. A member function of a derived class.
• While the friend functions and the member functions of a friend class can have direct access to both the
private and protected data, the member functions of a derived class can directly access only the protected
data (fz2 in below diagram). However, they can access the private data through the member functions of the
base class (fz1 and fz2 can access the private data of x using fx1 and fx2 in below diagram).

• Figure 8.5 illustrates how the access control mechanism works in various situations. A simplified view of
access control to the members of a class is shown in Fig. 8.6.
Figure 8.6: A simple view of control to the members of a class
Friend function and friend class
• A friend class can access private and protected members of other
classes in which it is declared as a friend.
• It is sometimes useful to allow a particular class to access private and
protected members of other classes.

• Refer the program module 3 – friend class in online gdb


Friend Function
• Like a friend class, a friend function can be granted special access to
private and protected members of a class in C++.
• They are the non-member functions that can access and manipulate
the private and protected members of the class for they are declared as
friends.
• A friend function can be:
1.A global function
2.A member function of another class
Syntax of friend function
Syntax:
friend return_type function_name (arguments);
// for a global function
or
friend return_type class_name::function_name (arguments);
// for a member function of another class
• 1. Global Function as Friend Function
• We can declare any global function as a friend function. The following
example demonstrates how to declare a global function as a friend
function in C++:
• Refer online gdb “module 3 – friend function as a global function”
• 2. Member Function of Another Class as Friend Function
• We can also declare a member function of another class as a friend
function in C++. The following example demonstrates how to use a
member function of another class as a friend function in C++:
• Refer online gdb “module 3 – Member Function of Another Class as
Friend Function”
MultiLevel Inheritance
• As shown in Fig. 8.7. The class A serves as a base class for the derived class B, which in turn serves as a base
class for the derived class C. The class B is known as intermediate base class since it provides a link for the
inheritance between A and C.
• The chain ABC is known as inheritance path.
Example for Multi level inheritance
• Let us consider a simple example. Assume that the test results
of a batch of students are stored in three different classes.
Class student stores the roll-number, class test stores the
marks obtained in two subjects and class result contains the
total marks obtained in the test. The class result can inherit
the details of the marks obtained in the test and the roll-
number of students through multilevel inheritance.
• Refer online gdb for example
8.6 Multiple Inheritance
Where Class M and Class N are base classes

Refer online gdb for example


8.7 Hierarchical inheritance
• As an example, Fig. 8.9 shows a hierarchical classification of
• students in a university. Another example could be the classification
• of accounts in a commercial bank as shown in Fig. 8.10. All the
• students have certain things in common and, similarly, all the
• accounts possess certain common features.
• In C++, such problems can be easily converted into class hierarchies. The base class will include all the
features that are common to the subclasses. A subclass can be constructed by inheriting the properties of the
base class. A subclass can serve as a base class for the lower level classes and so on.
Hybrid Inheritance
• There could be situations where we need to apply two or more types
• of inheritance to design a program. For instance, consider the case of processing the student results discussed
in Sec. 8.5. Assume that
• we have to give weightage for sports before finalising the results.
• The weightage for sports is stored in a separate class called sports.
• The new inheritance relationship between the various classes would
• be as shown in Fig. 8.11.

You might also like