Hybrid Inheritance
- Hybrid Inheritance is
a method where one or more types of inheritance are combined together and
used.
- Hybrid inheritance is the
combined form of all the rest of the inheritances (Single,
Multiple,Multilevel and Hierarchical).
Diagram:
Syntax:
class base_class_name
{
private:
…………….
public:
…………….
protected:
…………….
};
class derived_class_name1 : public base_class_name
{
private:
……………
public:
……………
protected:
……………
};
class derived_class_name2 : public base_class_name
{
private:
…………….
public:
…………….
protected:
…………….
};
class derived_class_name3 : public derived_class_name1,public derived_class_name2
{
private:
…………….
public:
…………….
protected:
…………….
};
{
private:
…………….
public:
…………….
protected:
…………….
};
class derived_class_name1 : public base_class_name
{
private:
……………
public:
……………
protected:
……………
};
class derived_class_name2 : public base_class_name
{
private:
…………….
public:
…………….
protected:
…………….
};
class derived_class_name3 : public derived_class_name1,public derived_class_name2
{
private:
…………….
public:
…………….
protected:
…………….
};
Sample Program
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:"<< rollno << "\n";
}
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:"<< rollno << "\n";
}
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{
e=s;
}
void put_extra(void)
{
cout << "Extra Score::" << e << "\n";
}
};
{
protected:
float e;
public:
void get_extra(float s)
{
e=s;
}
void put_extra(void)
{
cout << "Extra Score::" << e << "\n";
}
};
class res : public marks, public extra
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout << "Total:"<< tot;
}
};
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout << "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
Output:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
Output:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12
Advantages:
- Helps in re usability of
code.
- Program development time is
reduced.
- Helps as security
purpose.
Disadvantages:
- In class hierarchy various
data elements remain unused, the memory allocated to them is not
utilized.
Hybrid Inheritance
Reviewed by NEERAJ SRIVASTAVA
on
4:28:00 PM
Rating:
No comments: