Skip to main content

Multiple Inheritance

Multiple Inheritance

In multiple inheritance a class can access the property of more than one class. This inheritance is use for multiple accessing of properties. In multiple inheritance ambiguty is 

occured when same name function is declare in both class and the object is confuse with function is called. 
syntax −

class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following 

example −

#include <iostream.h>
#include <conio.h> 
// Base Class
class Shape 
{
   public:
      void getwidth(int w) {
         width = w;
      }
      void getheight(int h) {
         height = h;
      }
      
   protected:
      int width;
      int height;
};

// Base class Cost
class cost 
{
   public:
      int getCost(int area) {
         return area * 120;
      }
};

// Derived class
class Rectangle: public Shape, public Cost 
{
   public:
      int getArea() 
     {
         int a=width * height;
             return a;
      }
};

void main() 
{
   Rectangle Rect;
   int area;
    
   Rect.getwidth(5);
   Rect.getheight(7);

   area = Rect.getArea();
   

   cout << "Total area: " << Rect.getArea() << endl;

   cout << "Total cost: Rs." << Rect.getCost(area) << endl;

   getch();
}

Run it

Comments

Popular posts from this blog

O Level Practical Queestion Paper (MS-OFFICE, MSDOS, MS-EXCEL, ICT Resources)

MSDOS 1. Write command to display all files having extension ‘jpg’. 2. Write command to display all files of all types starting with letter ‘m’. 3. Write command to display all files having names up to ten characters with second last letter as ‘n’, e.g. intelligent. 4. Write a command to copy the file mars of type exe from the directory ‘space’ of drive U to another directory “universe” in drive S. 5. Write a command to delete the directory as well as the files of the directory ‘world’ on drive E. 6. Write command to display all file names on the disk in drive A and having an extension of no more than two characters.   7. Write command to copy all the files beginning with ‘m’ and whose file names has a ‘txt’ extension from drive A to the ‘\document’ directory on drive C. 8. Write set of commands to create a subdirectory of the root directory called ‘exercise’, and then move into it 9. Perform the following tasks using DOS commands in the DOS sh...

Learn Programming in UNIX

Q: - Write a program to input two numbers and add them?             gedit add.sh             clear             echo “enter two numbers:”             read a             read b             sum=`expr$a+$b`             echo $sum Syntax for if-else:- (1)          if test condition             then                 ---------                 --...

IICS College 15th August Celebration Day

IICS COLLEGE  15th August Celebration Day