Extending Classes Using Inheritance
Extending Classes Using Inheritance
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.
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;
}
#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-
Where 'A' is the base class, and 'B' is the derived class.
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. }
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. }
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. }
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. }
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.
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();
}
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();
}