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
*/

Show the use of constructor in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class demo
{
int x;
public:
demo()
{
}


demo(int d,int b=5)
{

cout<<"ADDITION OF TWO NUMBER IS::"<<b+d;

}

};

int main()
{
int p,q;
clrscr();
demo d(3);
getch();
return 0;
}
/*=============================================================
OUTPUT
ADDITION OF TWO NUMBER IS::8
*/

Calculate income tax for each of given calss(doctor,scientist,teacher) object by use of friend function concept. in 'C++'

Example



#include<iostream.h>
#include<conio.h>
#include<math.h>
class doctor;
class scientist;
class teacher
{


float hra,bs,da,c;
public:
    // friend void getdata(teacher t,doctor d,scientist s);
void getdata()
{
cout<<">>>>>ENTER TEACHER SALARY DETAILS"<<endl;
cout<<"------------------------------------"<<endl;
cout<<"Enter house rent allowance:";
cin>>hra;
cout<<"Enter basic salary:";
cin>>bs;
cout<<"Enter dearness allowance:";
cin>>da;
}
friend void calculate(teacher t,doctor d,scientist s);


};
class doctor
{


float hra,bs,da,c;
public:
  // friend void getdata(teacher t,doctor d,scientist s);
void getdata()
{
cout<<">>>>ENTER DOCTOR SALARY DETAILS"<<endl;
cout<<"------------------------------------"<<endl;
cout<<"Enter house rent allowance:";
cin>>hra;
cout<<"Enter basic salary:";
cin>>bs;
cout<<"Enter dearness allowance:";
cin>>da;
}
friend void calculate(teacher t,doctor d,scientist s);


};
class scientist
{


float hra,bs,da,c;


public:
// friend void getdata(teacher t,doctor d,scientist s);
void getdata()
{
cout<<">>>>ENTER SCIENTIST SALARY DETAILS"<<endl;
cout<<"------------------------------------"<<endl;
cout<<"Enter house rent allowance:";
cin>>hra;
cout<<"Enter basic salary:";
cin>>bs;
cout<<"Enter dearness allowance:";
cin>>da;
}
friend void calculate(teacher t,doctor d,scientist s);


};
void calculate(teacher t,doctor d,scientist s)
{
t.c=(t.hra+t.bs+t.da)*0.12;
cout<<"------------------------------------"<<endl;
cout<<"TEACHER TOTAL INCOME TAX IS::"<<t.c<<endl;
d.c=(d.hra+d.bs+d.da)*0.11;
cout<<"DOCTOR TOTAL INCOME TAX IS::"<<d.c<<endl;
s.c=(s.hra+s.bs+s.da)*0.10;
cout<<"SCIENTIST TOTAL INCOME TAX IS::"<<s.c<<endl;
}


int main()
{
teacher t;
doctor d;
scientist s;
clrscr();
t.getdata();
d.getdata();
s.getdata();
       calculate(t,d,s);
getch();
return 0;
}
/*===================================================
OUTPUT
>>>>>ENTER TEACHER SALARY DETAILS
------------------------------------
Enter house rent allowance:1200
Enter basic salary:3340
Enter dearness allowance:200
>>>>ENTER DOCTOR SALARY DETAILS
------------------------------------
Enter house rent allowance:2390
Enter basic salary:5000
Enter dearness allowance:222
>>>>ENTER SCIENTIST SALARY DETAILS
------------------------------------
Enter house rent allowance:5690
Enter basic salary:7000
Enter dearness allowance:2345
------------------------------------
TEACHER TOTAL INCOME TAX IS::568.799988
DOCTOR TOTAL INCOME TAX IS::837.320007
SCIENTIST TOTAL INCOME TAX IS::1503.5
*/

Demo of static member function in 'C++'

Example



#include<iostream.h>
#include<conio.h>
#include<math.h>
class statfun
{
static int count;
int no,no1;
public:
void getdata()
{
  cout<<count++<<endl;
}
static void getcount()
{
cout<<"count:"<<count<<endl;
}
};
int statfun::count;
int main()
{
statfun s1,s2;
clrscr();
s1.getdata();
statfun::getcount();
s2.getdata();
statfun::getcount();
s1.getdata();
s1.getcount();
s1.getcount();

getch();

return 0;
}
/*===================================================================
OUTPUT
0
count:1
1
count:2
2
count:3
count:3
*/

Demo of static data member in 'C++'

Example



#include<iostream.h>
#include<conio.h>
#include<math.h>
class stat
{
static int count;
int no;
public:
void getdata();
void display();
};
int stat::count;
void stat::getdata()
{
count++;
}


void stat::display()
{
cout<<"Total of given number is::"<<count<<endl;


}
int main()
{
stat s1,s2,s3,s4;
clrscr();
s1.getdata();
s1.display();
s2.getdata();
s2.display();
s3.getdata();
s3.display();
getch();
return 0;
}
/*==================================================================
OUTPUT
Total of given number is::1
Total of given number is::2
Total of given number is::3
*/

Inline function in 'C++'.

Example



#include<conio.h>
#include<iostream.h>
#include<math.h>
class area
{
int l,b,c;
public:
void getdata()
{
cout<<"ENTER LENGTH AND BREADTH"<<endl;
cin>>l>>b;
}
inline void square()
{
c=4*l;
cout<<"AREA OF SQUARE:"<<c<<endl;
}
inline void rectangle()
{
cout<<"AREA OF RECTANGLE"<<l*b<<endl;
}
inline void triangle()
{
cout<<"AREA OF TRIRANGLE:"<<0.5*l*b<<endl;
}
};
int main()
{
area a;
clrscr();
a.getdata();
a.square();
a.rectangle();
a.triangle();
getch();
return 0;




}
/*===============================================================
OUTPUT
Enter length and breath::
4
3


AREA OF SQUARE IS:::16
AREA OF RECTANGLE IS:::12
AREA OF TRIANGLE IS:::6
*/

Find total,percentage of given marks.in 'C++'

Example



#include<iostream.h>
#include<conio.h>
#include<math.h>
class report
{
int rno,mks[3],total;
float per;
public:
void getdata();
void putdata();
void calculate();
};
void report::getdata()
{
int i,j,tot=0;
cout<<"Enter rollno:";
cin>>rno;


for(j=0;j<3;j++)
{
cout<<"enter marks:";
cin>>mks[j];


}


}
void report::calculate()
{
     total=0;
for(int j=0;j<3;j++)
{
total=total+mks[j];
}


per=total/3;




     //  return per;


}
void report::putdata()
{


cout<<rno<<"      ";
for(int j=0;j<3;j++)
{
cout<<mks[j]<<"   ";
}
cout<<total<<"   "<<per<<endl;


}
int main()
{
report r[5];
int i,n;
clrscr();
cout<<"Enter total no of student :";
cin>>n;
for(i=0;i<n;i++)
{
r[i].getdata();
}
for(i=0;i<n;i++)
{
r[i].calculate();
}
cout<<"ROLLNO SUB1 SUB2 SUB3 TOTAL PER(%)"<<endl;
cout<<"======================================"<<endl;
for(i=0;i<n;i++)
{
r[i].putdata();
}
getch();
return 0;
}
/*===============================================================
OUTPUT
Enter total no of student :2
Enter rollno:1
enter marks:33
enter marks:44
enter marks:55
Enter rollno:2
enter marks:66
enter marks:55
enter marks:44
ROLLNO SUB1 SUB2 SUB3 TOTAL PER(%)
======================================
1      33   44   55   132   44
2      66   55   44   165   55
*/