Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

Sunday, September 25, 2016

Hybrid Inheritance

Hybrid Inheritance is that type in which we combine two or more types of inheritance.

Program To Illustrate the Concept of Hybrid Inheritance:

#include<iostream>
using namespace std;
class Student
{
int rollno;
public:
void getnum(int a)
{
rollno=a;
}
void putnum()
{
cout<<"Roll Number is "<<rollno;
}
};
class Marks:public Student
{ protected:
float sub1,sub2;
public:
void getmarks(float a,float b)
{
sub1=a;
sub2=b;
}
void display()
{
cout<<"\nSubject 1="<<sub1;
cout<<"\nSubject 2="<<sub2;
}

};
class sports
{protected:
int sport;
public:
void getsport(int a)
{
sport=a;
}
void displaysport()
{
cout<<"\nMarks In Sports="<<sport;
}
};
class result:public sports,public Marks
{
float total;
public:
void output()
{total=sub1+sub2+sport;
cout<<"\nTotal Marks="<<total;
}
};
main()
{
result r;
r.getnum(712299);
r.getmarks(98.5,89.6);
r.getsport(89);
r.putnum();
r.display();
r.displaysport();
r.output();
}

Also See Hierarchical Inheritance



Hierarchial Inheritance

Program to Illustrate The Concept Of Hierarchical Inheritance:


#include<iostream>
using namespace std;
class Number
{
int num;
public:
void getnum()
{
cout<<"\nEnter any Number ";
cin>>num;
}
int returnum()
{
return num;
}
};
class Square:public Number
{
int sqr;
public:
void square()
{
int num=returnum();
sqr=num*num;
cout<<"Square of "<<num<<" is "<<sqr;
}
};
class Cube:public Number
{
int cube;
public:
void cuube()
{
int num=returnum();
cube=num*num*num;
cout<<"Cube of "<<num<<" is "<<cube;
}
};
main()
{
Square a;
a.getnum();
a.square();
Cube b;
b.getnum();
b.cuube();
}
Also See Multiple Inheritance

Saturday, September 24, 2016

Multiple Inheritance

Program To Illustrate The Concept Of Multiple Inheritance:


#include<iostream>
using namespace std;
class base1
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata()
        {
            cout<<"Enter The Values Of A,B,C\n";
            cin>>a>>b>>c;
        }
        void putdata()
        {
            cout<<"Values Are\n";
            cout<<"\nA="<<a;
            cout<<"\nB="<<b;
            cout<<"\nC="<<c;
                }
};
class base2
{
    int d;
    protected:
        int e;
    public:
        void input()
        {
            cout<<"Enter D and E\n";
            cin>>d>>e;
        }
        void output()
        {
            cout<<"\nD="<<d;
            cout<<"\nE="<<e;
        }
};
class derived:public base1,public base2
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        {
            cout<<"Enter F and G\n";
            cin>>f>>g;
        }
        void putfg()
        {
            cout<<"\nF="<<f;
            cout<<"\nG="<<g;
        }
};
main()
{
    derived p;
    p.getdata();
    p.input();
    p.getfg();
    p.putdata();
    p.output();
    p.putfg();
}

Output:

Ambiguity:

In Multiple Inheritance There is an Ambiguity That If both Base Classes have same function then which function will be invoked by the object of derived class.
Because both functions are the members of derived class.
Therefore there will be an error message stating that the call is ambiguous.

Solution: 

We can solve the above problem by using scope resolution operator.

p.base1::putdata();

p.base2::putdata();

Error Message:







Multilevel Inheritance

Multilevel:

It is a type of inheritance in which the derived class is used as base class to derive another class.
Thus the second derived class has features of both base and 1 st derived class.

 Program to Illustrate The Multilevel Inheritance:


#include<iostream>
using namespace std;
class base
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata();
        void display();
};
void base:: getdata()
{
    cout<<"Enter The Values Of A,B,C\n";
    cin>>a>>b>>c;
}
void base::display()
{
    cout<<"Values Are\n";
    cout<<"A="<<a;
    cout<<"\nB="<<b;
    cout<<"\nC="<<c;
}
class derive:public base
{
    int d;
    protected:
        int e;
    public:
    void displaay()
    {
        cout<<"\nD="<<d;
        cout<<"\nE="<<e;
    }
            void input()
            {
                cout<<"\nEnter D AND E\n";
                cin>>d>>e;
       
            }
};
class derive1:public derive
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        { cout<<"Enter F And G\n";
            cin>>f>>g;
        }
        void putfg()
        {
        cout<<"\nF="<<f;
        cout<<"\nG="<<g;
        }
};

main()
{
    derive1 p;
    p.getdata();
    p.input();
    p.getfg();
    p.display();
    p.displaay();
    p.putfg();
}

Single Inheritance

Program To Illustrate The Single Inheritance :
#include<iostream>
using namespace std;
class base
{
int a;
protected:
int b;
public:
int c;
void getdata();
   void putdata();
};
void base:: getdata()
{
cout<<"Enter The Values Of A,B,C\n";
cin>>a>>b>>c;
}
void base::putdata()
{
cout<<"Values Are\n";
cout<<"A="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
class derive:public base
{
int d;
protected:
int e;
public:
void display()
{
cout<<"\nD="<<d<<"\nE="<<e;
}
void input()
{
cout<<"\nEnter D AND E\n";
cin>>d>>e;
}
};
main()
{
derive p;
p.getdata();
p.input();
p.putdata();
p.display();
}


Ambiguity:

When we use same name for member function in both base class and derived class then there is an ambiguity that which function will be invoked by the object of derived class in main function.

Solution:

So After checking it i found that the function in derived class is invoked by the object.Only if it is defined,if it is not defined then it will call the base class function.
We can Also Solve This Ambiguity By Using Scope Resolution Operator As Follows:

p.base::display();
p.derive::display();


Also See Our C program To print Alphabet pattern