Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Extending Classes Using Inheritance

The document discusses inheritance in C++. It defines inheritance as allowing a derived class to inherit features from a base class while adding new features. There are different types of inheritance - single, multilevel, multiple, hierarchical and hybrid. It also discusses visibility modes like private, protected, public that control which members are accessible to derived classes. Finally, it explains the concept of virtual base classes which prevent ambiguity when multiple inheritance is used.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Extending Classes Using Inheritance

The document discusses inheritance in C++. It defines inheritance as allowing a derived class to inherit features from a base class while adding new features. There are different types of inheritance - single, multilevel, multiple, hierarchical and hybrid. It also discusses visibility modes like private, protected, public that control which members are accessible to derived classes. Finally, it explains the concept of virtual base classes which prevent ambiguity when multiple inheritance is used.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Extending Classes using Inheritance

 Introduction to inheritance:- Inheritance is one of the key features of


Object-oriented programming in C++. It allows us to create a
new class (derived class) from an existing class (base class).
The derived class inherits the features from the base class and can
have additional features of its own.
When we say derived class inherits the base class, it means, the derived class
inherits all the properties of the base class, without changing the properties of
base class and may add new features to its own. These new features in the
derived class will not affect the base class. The derived class is the specialized
class for the base class.
 Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class.
 Super Class: The class whose properties are inherited by a subclass
is called Base Class or Superclass.

Defying a class-
Visibility modes and Effects- In inheritance whenever the derived class
inherits from the base class than which of the member of the parent
class can be accessible in the child class is controlled by the visibility
mode. By default visibility mode is always set to private.

1. Syntax:class derived_class_name : visibilitymode base_class_name


2. {
3. // body of the derived class.
4. }

 There are total 3 types of visibility mode in C++ that are:

1. Private visibility mode,


2. Protected visibility mode,
3. Public visibility mode

Private visibility mode:

When we inherit a derived class from the base class with private visibility
mode then the public and protected members of the base class become
private members of the derived class.
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : private X{
//As the visibility mode is private none of the member
of base class is inherited to derived class
};
int main()
{
//Nothing can be accessed using Class Y object because
there are no members in class Y.
return 0;
}

. Protected visibility mode:

When we inherit a derived class from a base class with protected


visibility mode the protected and public members of the base class
become protected members of the derived class

#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : protected X{
//As the visibility mode is protected the protected an
d public members of class X becomes the protected members
of Class Y
//protected: int b,int c inherited from class X
};
int main()
{
//As the members in the class Y are protected they can
not be accessed in main using Class Y object.
return 0;
}

. Public mode:

When we inherit a derived class from a base class with public visibility
mode, the public members and protected members of the base class will
be inherited as public members and protected members respectively of
the derived class.

#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : public X{
//As the visibility mode is public the protected membe
rs of class X becomes protected member for class Y and pub
lic members of class X becomes public member for class Y
//protected: int b; inherited from class X
//public: int c; inherited from class X
};
int main()
{
//Only int c can be accessed in main function using Cl
ass Y object as it is public;
Y obj;
std::cout<<obj.c;
return 0;
}
 Types of inheritance-

1)single inheritance- Single inheritance is defined as the inheritance in


which a derived class is inherited from the only one base class.

Where 'A' is the base class, and 'B' is the derived class.

Example of single inheritance.

1. #include <iostream>
2. using namespace std;
3. class Account {
4. public:
5. float salary = 60000;
6. };
7. class Programmer: public Account {
8. public:
9. float bonus = 5000;
10. };
11. int main(void) {
12. Programmer p1;
13. cout<<"Salary: "<<p1.salary<<endl;
14. cout<<"Bonus: "<<p1.bonus<<endl;
15. return 0;
16. }

2)multilevel - Multilevel inheritance is a process of deriving a class from


another derived class.

The example of multi level inheritance in C++.

1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void bark(){
13. cout<<"Barking..."<<endl;
14. }
15. };
16. class BabyDog: public Dog
17. {
18. public:
19. void weep() {
20. cout<<"Weeping...";
21. }
22. };
23. int main(void) {
24. BabyDog d1;
25. d1.eat();
26. d1.bark();
27. d1.weep();
28. return 0;
29. }

3)multiple- Multiple inheritance is the process of deriving a new class that


inherits the attributes from two or more classes.
syntax of the Derived class:

1. class D : visibility B-1, visibility B-2


2. {
3. // Body of the class;
4. }

example of multiple inheritance.

1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. protected:
6. int a;
7. public:
8. void get_a(int n)
9. {
10. a = n;
11. }
12. };
13.
14. class B
15. {
16. protected:
17. int b;
18. public:
19. void get_b(int n)
20. {
21. b = n;
22. }
23. };
24. class C : public A,public B
25. {
26. public:
27. void display()
28. {
29. std::cout << "The value of a is : " <<a<< std::endl;
30. std::cout << "The value of b is : " <<b<< std::endl;
31. cout<<"Addition of a and b is : "<<a+b;
32. }
33. };
34. int main()
35. {
36. C c;
37. c.get_a(10);
38. c.get_b(20);
39. c.display();
40.
41. return 0;
42. }

4)Hierarchical- Hierarchical inheritance is defined as the process of deriving


more than one class from a base class.

Syntax of Hierarchical inheritance:

1. class A
2. {
3. // body of the class A.
4. }
5. class B : public A
6. {
7. // body of class B.
8. }
9. class C : public A
10. {
11. // body of class C.
12. }
13. class D : public A
14. {
15. // body of class D.
16. }

5)hybrid- Hybrid inheritance is a combination of more than one type of


inheritance.

Virtual Base Class in C++

The virtual base class is a concept used in multiple


inheritances to prevent ambiguity between multiple
instances. For example: suppose we created a class “A”
and two classes “B” and “C”, are being derived from
class “A”. But once we create a class “D” which is
being derived from class “B” and “C” .
Virtual Base Class Example Diagram

1. Class “A” is a parent class of two classes “B”


and “C”
2. And both “B” and “C” classes are the parent of
class “D”

The main thing to note here is that the data members


and member functions of class “A” will be inherited
twice in class “D” because class “B” and “C” are the
parent classes of class “D” and they both are being
derived from class “A”.
So when the class “D” will try to access the data
member or member function of class “A” it will cause
ambiguity for the compiler and the compiler will throw
an error. To solve this ambiguity we will make class
“A” as a virtual base class. To make a virtual base
class “virtual” keyword is used.
When one class is made virtual then only one copy of
its data member and member function is passed to the
classes inheriting it. So in our example when we will
make class “A” a virtual class then only one copy of
the data member and member function will be passed to
the classes “B” and “C” which will be shared between
all classes. This will help to solve the ambiguity.
The syntax of the virtual base class is shown in below
code-
#include <iostream>
using namespace std;
class A
{
public:
void say()
{
cout << "Hello world"<<endl;
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
Example-
Class A
{ int a;
Public: void display_a()
{ a=10;
Cout<<”class A”<<a;}
};
Class B: public virtual A
{ int b;
Public: void display_b()
{ b=20;
Cout<<”class B”<<b;}
};
Class c : public virtual A
{ int b;
Public: void display_c()
{ c=30;
Cout<<”class c”<<c; }
};
Class D :public B,public C
{ int d;
Public: void display_d()
{ d=40;
Cout<<”class D”<<d; }
};
Int main()
{ D obj;
Obj.display_a();
Obj.display_b();
Obj.display_c();
Obj.display_d();
}

Abstract class- Abstract class in C++ refer to classes containing at least one pure
virtual function, which cannot be instantiated.

or
An abstract class is a class that is designed to be specifically used as a base class. An abstract
class contains at least one pure virtual function. You declare a pure virtual function by using
a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

The following is an example of an abstract class:


class AB
{
public:
virtual void f() = 0;
};

Characteristics of Abstract Class in C++


1. Abstract Classes must have at least one pure virtual function.

virtual int perimeter() = 0;

2. Abstract Classes cannot be instantiated, but pointers and references of Abstract Class
types can be created. You cannot create an object of an abstract class. Here is an
example of a pointer to an abstract class.

3. Abstract Classes are mainly used for Upcasting, which means its derived classes can
use its interface.
4. Classes that inherit the Abstract Class must implement all pure virtual functions. If
they do not, those classes will also be treated as abstract classes.

Example-
Class Base
{ virtual void disp()=0;
};
Class D:public Base
{
public:
void disp()
{ cout<<”derived class”; }
};
Int main()
{ Base *ptr;
D obj;
Ptr=&obj;
ptr->disp();
}

Constructors in Derived Class in C++

 We can use constructors in derived classes in


C++
 If the base class constructor does not have any
arguments, there is no need for any constructor
in the derived class.
 But if there are one or more arguments in the
base class constructor, derived class need to
pass argument to the base class constructor.
 If both base and derived classes have
constructors, base class constructor is
executed first.

Example-
Class A
{ protected: int a;
Public: A(int x)
{ a=x; }
Void display()
{ cout<<”A=”<<a;
}
};
Class B
{ protected: int b;
Public: B(int y)
{ b=y;}
Void putdata()
{ cout<<”B=”<<b;
}
};
Class C: Public A, public B
{ int c;
Public: C(int p,int q,int r):A(q),B(r)
{ c=r; }
Void show()
{ cout<<”c=”<<c; }
};
Int main()
{ C obj(10 ,20,30);
Obj.display();
Obj.putdata();
Obj.show();
}

You might also like