C++ hybrid Inheritance

C++ hybrid Inheritance

there could be situation where we need to apply two or more types of inheritance to design a program Assume that we have to give weightage for sports before finalising the reults. The weightage of the sports is stored in a separate class called sports. The new inheritance relationship between the various classes would be show in fig below

HYBRID INHERITANCE pic

The sports class might look like this

class sports
{
protected:
float score;
public:
void getscore(float);
void putscore();
};

The result will have both the multilevel and multiple inheritance and its declaration would be as follows

class sports: public test, public sports
{
.............
............
};

where test itself a derived class from student, that is

class test: public student
{
………
………
};

let us take example with both multilevel and multiple inheritance

class sports: public test, public sports
{
………….
…………
};

#include <iostream>
using namespace std;
class student
{
protected:
int roll;
public:
void getnumber(int a)
{
roll=a;
}
void putnumber()
{
std::cout << "roll number" << roll << "\n";
}
};
class test: public student
{
protected:
float part1,part2;
public:
void getmarks( float x, float y)
{
part1=x;
part2=y;
}
void putmarks()
{
std::cout << "marks obtain:" << "\n" << "part1=" << part1 << "\n" << "part2="<< part2 << "\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore()
{
std::cout << "sports wt:" << score << "\n\n";
}
}; class result: public test, public sports
{
float total;
public:
void display();
};
void result::display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout << "total score :" << total << "\n";
}
int main()
{
result student1;
student1.getnumber(1234);
student1.getmarks(27.5,33.0);
student1.getscore(6.0);
student1.display();
return 0;
}

Roll no: 1234
marks obtained:
part1=27.5
part2=33
sports wt:6
total score:66.5

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise