Sunday, January 1, 2012

Hybrid inheritence in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class stud
{
protected:
char rno[10],nm[20],co[20];
int sm;
    
public:

void getdata()
{
    cout<<"ENTER ROLLNO:";
    cin>>rno;
    cout<<"ENTER NAME:";
    cin>>nm;
    cout<<"ENTER  COURSE:";
    cin>>co;
    cout<<"SEMESTER ::";
cin>>sm;
}

    };

class result:public stud
{
protected:
int total,m1,m2,m3;
float per;
public:
void insert()
{
cout<<"\nENTER MARKS OF THREE SUB:";
cin>>m1>>m2>>m3;
}
void display()
{
      total=m1+m2+m3;
      per=total/3;
      cout<<"\n===========STUDENT DETAIL=========\n";
      cout<<"ROLLNO   NAME   COURSE    SEMESTER      "<<endl;
      cout<<rno<<"\t"<<nm<<"\t"<<co<<"\t"<<sm<<endl;
      cout<<"\n=====STUDENT MARKS==============\n";
      cout<<"  MARKS1  MARKS2  MARKS3  TOTAL  PER"<<endl;
      cout<<rno<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<total<<"\t"<<per<<endl;
}
};
class sport
{
protected:
char gm[30],gr[10];
public:
void getdata1()
{
cout<<"\n\nEnter the game:";
cin>>gm;
cout<<"Enter the status:";
cin>>gr;
}
};
class report:public result,public sport
{
public:
void display()
{
result::display();
cout<<"\n======STUDENT GAME REPORT======\n";
cout<<" GAMENAME   STATUS"<<endl;
cout<<gm<<"\t\t"<<gr;
}
};




int main()
{
clrscr();
report s;
s.getdata();
s.insert();
s.getdata1();
clrscr();
s.display();
getch();
return 0;
}

Multilevel inheritence in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class stud
{
protected:
char nm[10],add[20],city[10],rno[10];
public:
void getdata()
{
cout<<"ENTER ROLLNO, NAME, ADDRESS, AND CITY  OF STUDENT::";
cin>>rno>>nm>>add>>city;
}
    };
class lib:public stud
{
      // protected:
int bkno,edt;
char bknm[20],athr[10];
public:
void getchar()
{
cout<<"\nENTER BOOKNO,BOOKNM,AUTHOR AND EDITION OF BOOK:";
cin>>bkno>>bknm>>athr>>edt;
}
void display()
{
cout<<"ROLLNO:"<<rno<<endl;
cout<<"NAME:"<<nm<<endl;
cout<<"ADDRESS:"<<add<<endl;
cout<<"CITY:"<<city<<endl;
cout<<"BOOKNO:"<<bkno<<endl;
cout<<"BOOK NAME:"<<bknm<<endl;
cout<<"AUTHOR:"<<athr<<endl;
cout<<"EDITION:"<<edt<<endl;


}
    };
class studreview:public lib
{
public:
void result()
{
cout<<"=================STUDENT/BOOK DETAIL============\n";
display();
cout<<"\n===============================================";
}
};
int main()
{
clrscr();
studreview s;
lib l;
s.getdata();
s.getchar();
s.result();
getch();
return 0;
}

Hirarchical inheritence in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class emp
{
protected:
char nm[10],rno[10];
int hra,da,bs;
public:
void getdata()
{
cout<<"ENTER EMPNO, NAME  OF EMPLOYEE::";
cin>>rno>>nm;
}
void getallowance()
{
cout<<"\nENTER HRA,DA AND BS OF EMPLOYEE:";
cin>>hra>>da>>bs;
}
    };
class res1:public emp
{
public:
void putdata()
{
cout<<"\n\nEmpno:"<<rno<<endl<<"Empname:"<<nm;
}
   };
class res2:public emp
{
float incom;
public:
void result()
{
incom=(hra+da+bs)*0.12;
cout<<"\nHRA:"<<hra<<endl<<"DA:"<<da<<endl<<"BS:"<<bs;
cout<<"\nINCOME TAX:"<<incom;
}
};
int main()
{
clrscr();
res1 r;
res2 r1;
r.getdata();
r1.getallowance();
r.putdata();
r1.result();
getch();
return 0;
}
/*===============================================================
output
ENTER EMPNO, NAME  OF EMPLOYEE::E101
ANISH


ENTER HRA,DA AND BS OF EMPLOYEE:2222
333
4444




Empno:E101
Empname:ANISH
HRA:2222
DA:333
BS:4444
INCOME TAX:839.880005
*/

Single inheritence in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class stud
{
protected:
char rno[10];
int m1,m2,m3;
public:
void getdata()
{
cout<<"ENTER ROLLNO MARKS1 MARKS2 MARKS3 OF STUDENT::";
cin>>rno>>m1>>m2>>m3;
}




    };
class result:public stud
{
int total;
float per;
public:
void display()
{
      total=m1+m2+m3;
      per=total/3;
      cout<<"ROLLNO  MARKS1  MARKS2  MARKS3  TOTAL  PER"<<endl;
 cout<<rno<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<total<<"\t"<<per;
}
};
int main()
{
clrscr();
result s;
s.getdata();
s.display();
getch();
return 0;
}
/*=============================================================
OUTPUT
ENTER ROLLNO MARKS1 MARKS2 MARKS3 OF STUDENT::08mca23
44
55
66
ROLLNO  MARKS1  MARKS2  MARKS3  TOTAL  PER
08mca23 44      55      66      165     55
*/

Multiple inheritence in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class stud
{
protected:
char nm[10],add[20],city[10],rno[10];
public:
void getdata()
{
cout<<"ENTER ROLLNO, NAME, ADDRESS, AND CITY  OF STUDENT::";
cin>>rno>>nm>>add>>city;
}
    };
class lib
{
protected:
int bkno,edt;
char bknm[20],athr[10];
public:
void getchar()
{
cout<<"\nENTER BOOKNO,BOOKNM,AUTHOR AND EDITION OF BOOK:";
cin>>bkno>>bknm>>athr>>edt;
}
    };
class studreview:public stud,public lib
{
public:
void result()
{
cout<<"=================STUDENT/BOOK DETAIL============\n";
cout<<"ROLLNO:"<<rno<<"      \tBOOKNO:"<<bkno<<endl;
cout<<"NAME:"<<nm<<"       \tBOOK NAME:"<<bknm<<endl;
cout<<"ADDRESS:"<<add<<"    \tAUTHOR:"<<athr<<endl;
cout<<"CITY:"<<city<<"      \tEDITION:"<<edt<<endl;
cout<<"\n===============================================";
}
};
int main()
{
clrscr();
studreview s;
s.getdata();
s.getchar();
s.result();
getch();
return 0;
}
/*===========================================================
output
ENTER ROLLNO, NAME, ADDRESS, AND CITY  OF STUDENT::08mca23
XYS
atmiya
ank


ENTER BOOKNO,BOOKNM,AUTHOR AND EDITION OF BOOK:201
cpp
balagurusamy
4
=================STUDENT/BOOK DETAIL============
ROLLNO:         BOOKNO:201
NAME:XYS              BOOK NAME:cpp
ADDRESS:atmiya          AUTHOR:balagurusamy
CITY:ank        EDITION:4


===============================================
*/

Constructor using of dynamic initilization in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class demo3
{
int x,d;
public:
demo3(int a,int b)
{
x=a;
d=b;
}
float display(demo3 y)
{
cout<<"multiplication of two number::"<<y.x*y.d;
}
};
int main()
{
clrscr();
int a,b;


cout<<"Enter two number::"<<endl;
cin>>a>>b;
demo3 d1(a,b),d2(a,b);
d1.display(d2);
getch();
return 0;
}
/*=====================================================================
output
Enter two number::
3
4
multiplication of two number::12
*/

Copy constructor in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class demo1
{
int d,e;
public:
demo1(int a,int b)
{
d=a;
e=b;
}
demo1(demo1 &x)
{
d=x.d;
      e=x.e;
}
void add()
{
cout<<d+e;
}
};
int main()
{
clrscr();
demo1 d(4,5),d3(2,3);
demo1 d1(d);
demo1 d2=d3;


       cout<<"\nadd of d:";d.add();
       cout<<"\nadd of d1:";d1.add();
cout<<"\nadd of d2:";d2.add();
cout<<"\nadd of d3:";d3.add();
getch();
return 0;
}
/*=============================================================
output


add of d:9
add of d1:9
add of d2:5
add of d3:5
*/