Pages

Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Tuesday, October 11, 2011

4.Inheritance

WAP USING INHERITANCE TO SHOW THE TECHNIQUE OF CONTAINERSHIP



// Containership

#include<iostream.h>
#include<conio.h>
class staff
{
int staff_id;
char name[30];
public:
void getdata()
{
cout<<"\n\nEnter staff_id & name::";
cin>>staff_id>>name;
}
void disp()
{
cout<<"\nStaff_id::"<<staff_id;
cout<<"\nName of staff::"<<name;
}
};

class lecturer
{
char subject[20],dept[20];
public:
staff s;
void getdata()
{
s.getdata();
cout<<"\n\nEnter dept & subject::";
cin>>dept>>subject;
}
void disp()
{
s.disp();
cout<<"\nDepartment::"<<dept;
cout<<"\nSubject::"<<subject;
}
};

class admin_staff
{
char post[20],dept[20];
public:
staff s;
void getdata()
{
s.getdata();
cout<<"\n\nEnter your post & department::";
cin>>post>>dept;
}
void disp()
{
s.disp();
cout<<"\nPost::"<<post;
cout<<"\nDepartment::"<<dept;
}
};

class librarian
{
char shift[20];
public:
staff s;
void getdata()
{
s.getdata();
cout<<"\nEnter your shift::";
cin>>shift;
}
void disp()
{
s.disp();
cout<<"\nShift::"<<shift;
}
};
void main()
{
clrscr();
lecturer l;
admin_staff as;
librarian lb;
l.getdata();
cout<<"\n\nLecturer details::\n";
l.disp();
as.getdata();
cout<<"\n\nAdmin details::\n";
as.disp();
lb.getdata();
cout<<"\n\nLibrarian details::\n";
lb.disp();
getch();
}

3.Inheritance

WAP USING INHERITANCE TO APPLY SHRIDACHARYA'S FORMULAE i.e,Calculate real roots if (d>=0)



#include<iostream.h>
#include<conio.h>
#include<math.h>
class coeff
{
protected:
float a,b,c;
};
class read_coeff:virtual public coeff
{
public:
read_coeff()
{
cout<<"\n\nEnter the value of a,b & c::";
cin>>a>>b>>c;
}
};
class calc_root:virtual public coeff
{
protected:
float d,x1,x2;
public:
calc_root()
{
d=sqrt(b*b-4*a*c);
if(d>=0)
{
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
}
}
};
class disp:public read_coeff,public calc_root
{
public:
disp()
{
cout<<"\nx1::"<<x1;
cout<<"\nx2::"<<x2;
}
};
void main()
{
clrscr();
disp d;
getch();
}

2.Inheritance

WAP USING INHERITANCE TO DISPLAY STAFF DETAILS


#include<iostream.h>
#include<conio.h>
class staff
{
int staff_id;
char name[30];
public:
staff()
{
cout<<"Enter staff_id & name::";
cin>>staff_id>>name;
}
void disp()
{
cout<<"\nStaff_id::"<<staff_id;
cout<<"\nName of staff::"<<name;
}
};

class lecturer:public staff
{
char subject[20],dept[20];
public:
lecturer()
{
cout<<"Enter dept & subject::";
cin>>dept>>subject;
}
void disp1()
{
cout<<"\nDepartment::"<<dept;
cout<<"\nSubject::"<<subject;
}
};

class admin_staff:public staff
{
char post[20],dept[20];
public:
admin_staff()
{
cout<<"Enter your post & department::";
cin>>post>>dept;
}
void disp2()
{
cout<<"\nPost::"<<post;
cout<<"\nDepartment::"<<dept;
}
};

class librarian:public staff
{
char shift[20];
public:
librarian()
{
cout<<"\nEnter your shift::";
cin>>shift;
}
void disp3()
{
cout<<"\nShift::"<<shift;
}
};
void main()
{
clrscr();
lecturer l;
admin_staff as;
librarian l1;
cout<<"\n\nLecturer details::\n";
l.disp();
l.disp1();
cout<<"\n\nAdmin details::\n";
as.disp();
as.disp2();
cout<<"\n\nLibrarian details::\n";
l1.disp();
l1.disp3();
getch();
}

1.Inheritance

WAP TO DISPLAY DETAILS OF A CD COMPANY USING INHERITANCE



#include<iostream.h>
#include<conio.h>
class item
{
protected:
char name[30],comp[30];
int sn;
public:
void disp()
{
cout<<"Name::"<<name<<endl
    <<"Company::"<<comp<<endl
    <<"Serial Number::"<<sn;
}
};
class audio:public item
{
public:
audio()
{
cout<<"\nEnter the name, serial no & company of the Audio cassette::";
cin>>name>>sn>>comp;
}
};
class video:public item
{
public:
video()
{
cout<<"\nEnter the name, serial no & company of the Video cassette::";
cin>>name>>sn>>comp;
}
};
class cd_rom:public item
{
public:
cd_rom()
{
cout<<"\nEnter the name, serial no & company of the Cd-Rom::";
cin>>name>>sn>>comp;
}
};
/*
class add:public item
{
int x;
public:
add()
{
x=no_ac+no_vc+no_cd;
}
};
class disp:public add
{
public:
cout<<"\nNumber of Audio Cassettes::"<<no_ac;
cout<<"\nNumber of Video Cassettes::"<<no_vc;
cout<<"\nNumber of Cd-Roms::"<<no_cd;
cout<<"\nTotal Number::"<<x;
};*/
void main()
{
clrscr();
audio a;
cout<<"Audio cassette details::"<<endl;
a.disp();
video v;
cout<<"Video cassette details::"<<endl;
v.disp();
cd_rom c;
cout<<"Cd_rom details::"<<endl;
c.disp();
getch();
}