Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Unit 5 preparation

What is the Inheritance in C++?


The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object
Oriented Programming. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.

C++ Base Class Access Control


When one class inherits another, the members of the base class become members of the derived class.

The access status of the base class members inside the derived class is determined by the access specifier used for inheriting the base class.
The base class access specifier must be public, private, or protected.
If the access specifier is not used, then it is private by default if the derived class is a class.
If the derived class is a struct, then public is the default in the absence of an explicit access specifier.

Protected member

When a member of a class is declared as protected, that member is not accessible to other, non-member elements of the program.
With one important exception, access to a protected member is the same as access to a private member; it can be accessed only by other
members of the class of which it is a part.
The sole exception to this rule is when a protected member is inherited.
A private member of a base class is not accessible by any other part of your program, including any derived class.
When a base class is inherited as public, protected members in the base class become protected members of the derived class, and are
accessible to the derived class.
Therefore, by using protected, you can create class members that are private to their class, but that can still be inherited and accessed by a
derived class.
.

Protected Base Class Inheritance

When a base class is inherited as protected, all public and protected members of the base class become protected members of the derived
class.
Here is an example:
Copy
// Demonstrate inheriting a protected base class.
#include <iostream>
using namespace std;

class base {
int i;
protected:
int j;
public:
int k;
void seti(int a) { i = a; }
int geti() { return i; }
};

// Inherit base as protected.


class derived : protected base {
public:
void setj(int a) { j = a; } // j is protected here
void setk(int a) { k = a; } // k is also protected
int getj() { return j; }
int getk() { return k; }
};

int main()
{
derived ob;

// ob.seti(10); // illegal because seti() is


// a protected member of derived, which makes it
// inaccessible outside of derived.

// cout << ob.geti(); // illegal -- geti() is protected


// ob.k = 10; // illegal because k is protected

// these next statements are OK


ob.setk(10);
cout << ob.getk() << ' ';
ob.setj(12);
cout << ob.getj() << ' ';

return 0;
}

Output: 10 12

Inheriting Multiple Base Class


It is possible for a derived class to inherit two or more base classes.
For example, in this short program, derived inherits both base1 and base2:
Copy
// An example of multiple base classes.
#include <iostream>
using namespace std;

class base1 {
protected:
int x;
public:
void showx() { cout << x << "\n"; }
};

class base2 {
protected:
int y;
public:
void showy() { cout << y << "\n"; }
};

// Inherit multiple base classes.


class derived: public base1, public base2 {
public:
void set(int i, int j) { x = i; y = j; }
};

int main()
{
derived ob;

ob.set(10, 20); // provided by derived


ob.showx(); // from base1
ob.showy(); // from base2

return 0;
}

Constructor
C++ Passing Parameters to Base Class
Constructors
PreviousNext

When only the constructor of the derived class requires one or more arguments, you use the standard parameterized constructor syntax.
To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments
along to one or more base class constructors.
The general form of this expanded declaration is shown here:
Copy
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list), ...
baseN(arg-list);
{
body of derived constructor
}

Here, base1 through baseN are the names of the base classes inherited by the derived class.
Notice that a colon separates the constructor declaration of the derived class from the base classes, and that the base classes are separated
from each other by commas, in the case of multiple base classes.
Consider this sample program:
Copy
#include <iostream>
using namespace std;

class base {
protected:
int i;
public:
base(int x) { i = x; cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};

class derived: public base {


int j;
public:
// derived uses x; y is passed along to base.
derived(int x, int y): base(y)
{ j = x; cout << "Constructing derived\n"; }

~derived() { cout << "Destructing derived\n"; }


void show() { cout << i << " " << j << "\n"; }
};

int main()
{
derived ob(3, 4);

ob.show(); // displays 4 3

return 0;
}
Virtual base class in C++
An ambiguity can arise when several paths exist to a class from the same base class. This means
that a child class could have duplicate sets of members inherited from a single base class.
C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary
care is taken so that the duplication is avoided regardless of the number of paths that exist to the child
class.
When two or more objects are derived from a common base class, we can prevent multiple
copies of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved
by preceding the base class’ name with the word virtual.
Example for the virtual base class

class A
{
public:
int i;
};

class B : virtual public A


{
public:
int j;
};

class C: virtual public A


{
public:
int k;
};

class D: public B, public C


{
public:

int sum;
};

int main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << “Value of i is : ”<< ob.i<<”\n”;
cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value of k is :”<< ob.k<<”\n”;
cout << “Sum is : ”<< ob.sum <<”\n”;

return 0;
}
C++ Virtual Functions
Virtual Function
A virtual function is a member function which is declared within a base class and is re-efined (Overriden)
by a derived class. When you refer to a derived class object using a pointer or a reference to the base
class, you can call a virtual function for that object and execute the derived class’s version of the
function.

reference (or pointer) used for function call.

-time.

Rules for Virtual Functions


1. Virtual functions cannot be static and also cannot be a friend function of another class.
2. Virtual functions should be accessed using pointer or reference of base class type to achieve run
time polymorphism.

3. The prototype of virtual functions should be same in base as well as derived class.
4. They are always defined in base class and overridden in derived class. It is not mandatory for
derived class to override (or re-define the virtual function), in that case base class version of
function is used.
5. A class may have virtual destructor but it cannot have a virtual constructor.

// CPP program to illustrate concept of Virtual Functions (runtime behavior)


#include <iostream.h>
class base
{
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
class derived : public base {
public:
void print()
{
cout << "print derived class" << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print(); // virtual function, binded at runtime
bptr->show(); // Non-virtual function, binded at compile time
}

C++ Pure Virtual Function


PreviousNext

Functions can be pure virtual by specifying the = 0; at the end of the function declaration.
Pure virtual functions do not have definitions and are also called interfaces.
Pure virtual functions must be re-defined in the derived class.
Classes having at least one pure virtual function are called abstract classes and cannot be instantiated.
They can only be used as base classes.
Example:
Copy
#include <iostream>

class MyAbstractClass
{
public:
virtual void dowork() = 0;
};

class MyDerivedClass : public MyAbstractClass


{
public:
void dowork()
{
std::cout << "Hello from a derived class." << '\n';
}
};

int main()
{
MyAbstractClass* o = new MyDerivedClass;
o->dowork();
delete o;
}

Early Binding and Late binding


The compiler performs a process called binding when an object is assigned to an object variable. The early binding (static binding) refers to compile
time binding and late binding (dynamic binding) refers to runtime binding.

Early Binding (Static binding)

When perform Early Binding, an object is assigned to a variable declared to be of a specific object type. Early binding objects are basically a strong
type objects or static type objects. While Early Binding, methods, functions and properties which are detected and checked during compile time and
perform other optimizations before an application executes. The biggest advantage of using early binding is for performance and ease of
development.
Late binding (Dynamic binding)

By contrast, in Late binding functions, methods, variables and properties are detected and checked only at the run-time. It implies that the compiler
does not know what kind of object or actual type of an object or which methods or properties an object contains until run time. The biggest
advantages of Late binding is that the Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects

You might also like