Online classes notes on computer science

😜Inheritance in c++
🔎Definition :- 
• Inheritance is the one of the main feature in Oop. Inheritance is the method by which the object of one class is gets two properties of another class.
• The mechanism that allows us to extend the definition of the class without making any physical changes to the existing class inheritance.

Over view:-
 • Inheritance is a capability of one class to acquire the property and characters from another class is called the parent (or) base or super class. And the class which inherited the properties of another class is called child (or) derived (or) sub class.
• Inheritance ,lets you create a new classes from existing class . Any new class that you create any existing class is called derived class existing class is called base class.
🔍Advantages of Inheritance:-
• Sometimes we need to repeat the code or we need repeat the whole class properties. So, it helps in various ways.

1.It saves the memory space
2.It saves time
3.It will remove frustration
4.It increases the reliability of code
5.Save the development and testing effort.
Use of Inheritance:-
• To increase the responsibility of a code and make future usable for another class we use the concept of inheritance.
Ex:-
Animal
  ↑
  dog

class dog inherits properties from its super class  
       
Syntax:-
class subclass_name:access mode superclass_name

• While we define a subclass like this , the superclass (base class) must be already define or at least declared before the subclass (derived class) declaration
• Access mode is used to specify, the mode in which the properties of superclass will be inherited into subclass public private or protected.


Member Access Specifier How  base class appears in the derived cla
Pravite
  1.  Private member of a base class are inaccessible to the derived class.
2.  Protected member of the base class become private member of the derived class
3. Public member of the base class become a private member of the derived class.
Protected
  1.  Private member of a base class are inaccessible to the derived class.

 2. Protected members of the base class become a protected members of the derived class
3. Public member of the base class become protected member of the derived class
public
  1.  Private member of a base class are inaccessible to the derived class
  2.Protected member of a base class become protected member of the derived class.
 3.Publice  members of the base class become a public members of the derived class



●>>Single Inheritance:-
• In single level inheritance, there is only one base class and has only one derived class.
Syntax:-
class derived:access_mode base
 class base
{
};
class derived:access_mode base
{
};

Ex:-
1.
#include<iostream.h>
#include<conio.h>
class Animal
{
public:
int legs=4;
};
class Dog:public Animal
{
public:
int tail=1;
};
int main()
{
Dog d;
cout<<d.legs<<d.tail;
}
Out put:-
4 1
[Program finished]

🔰🔰🔰🔰🔰🔰🔰🔰🔰🔰
2.
#include<iostream.h>
#include<conio.h>
class A
{
protected: //public:
int a;
public:
void geta()
{
cout<<"Enter a value\n";
cin>>a;
}
};
class B:public A
{
public:
int b,c;
void getb()
{
cout<<"Enter b value\n";
cin>>b;
}
void dis()
{
cout<<"a="<<a<<"\nb="<<b<<endl;
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
B obj;
clrscr();
obj.geta();
obj.getb();
obj.dis();
getch();
}
Out Put
Enter a value
5
Enter b value
7
a=5
b=7
sum=12
[Program finished]

Multiple inheritance
  • A  C++  class can inherit members from more than one class and here is the existing syntax
Syntax:-
class derived_class:access_base1,access base2,.....

  •  Where access is one of the Public ,Protected (or) private and would be given for every base class and they will be separated by comma as shown in above
Ex:-
>>Program on multi inheritance  by accessing of private members of class
1】
#include<iostream.h>
#include<conio.h>
class A//base class
{
protected:
int r;
char n[10];
public:
void f1()
{
cout<<"Enter name and rool number\n";
cin>>n>>r;
}
};
class B //base class
{
protected:
int a;
public:
void f2()
{
cout<<"enter age=";
cin>>a;
}
};
class C//base class
{
protected:
int m,p,c;
public:
void f3()
{
cout<<"Enter m,p,c markes\n";
cin>>m>>p>>c;
}
};
class D:public A,public B,public C //sub-class
{
int t,avg;
public:
void show()
cout<<"Name :"<<n;
cout<<"\nAge :"<<a;
cout<<"\nTotal :"<<t;
cout<<"\n Average :"<<avg;
}
};
int main()
{
clrscr();
D d;
d.f1();
d.f2();
d.f3();
d.show();
getch();
}
Out Put:-
Enter name and rool number
Shashi
194
enter age=19
Enter m,p,c markes
87
84
93
Name :Shashi
Age :19
Total :264
 Average :88
[Program finished]

2】
#include<iostream.h>
#include<conio.h>
class A//base class
{
 int r;
 char n[10];
 public:
 void f1()
 {
  cout<<"Enter name and rool number\n";
  cin>>n>>r;
}
};
class B //base class
{
 int a;
 public:
 void f2()
 {
  cout<<"enter age=";
  cin>>a;
 }
};
class C//base class
{
public:
 int m,p,c;
 void f3()
 {
  cout<<"Enter m,p,c markes\n";
  cin>>m>>p>>c;
 }
};
class D:public A,public B,public C //sub-class
{
 int t,avg;
 public:
 void show()
 {
  A::f1();
  B::f2();
  C::f3();
  A x; B y;
  t=m+p+c;
  avg=t/3;
//  cout<<"Name :"<<n;
//  cout<<"\nAge :"<<a;
  cout<<"\nTotal :"<<t;
  cout<<"\n Average :"<<avg;
 }
};
int main()
{
 clrscr();
 D d;
 d.show();
 getch();
}
Out Put:-
Enter name and rool number
Shashi
194
enter age=19
Enter m,p,c markes
84
83
92

Total :259
 Average :86
[Program finished]

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat