Pages

Tuesday, October 11, 2011

6.Operator Overloading

WAP USING OPERATOR OVERLOADING TO ENTER RECTANGULAR CO-ORDINATES AND CONVERT THEM INTO POLAR CO-ORDINATES & VICE-VERSA





#include<conio.h>
#include<iostream.h>
#include<math.h>
class rec;
class polar
{
float rad,ang;
public:
polar()   //without default constructor they is an error message
{         //couldnot find a match for polar::polar()
}

5.Operator Overloading

WAP USING OPERATOR OVERLOADING TO ENTER THE MEMORY IN BYTES AND CONVERT THEM INTO MB,KB & BYTES


#include<iostream.h>
#include<conio.h>
class memory
{
long int b,kb,mb;
public:
memory(long int d)
{
b=d%1024;
d=d/1024;
kb=d%1024;
d=d/1024;
mb=d%1024;
}

4.Operator Overloading

WAP USING CONSTRUCTOR OVERLOADING TO ENTER THE VALUES OF X & Y AND ADD AND SUB THEM

#include<conio.h>
#include<iostream.h>
class vector
{
float x,y;
public:
void getdata()
{
cout<<"\nEnter the value of x & y::";
cin>>x>>y;
}

3.Operator Overloading

WAP USING CONSTRUCTOR OVERLOADING TO ADD ELEMENTS OF TWO MATRICES


#include<conio.h>
#include<iostream.h>
class matrix
{
int m,n;
int arr[100][100];
public:
void getdata(int,int);
void putdata();
friend matrix operator+(matrix &,matrix &);
};

2.Operator Overloading

WAP USING CONSTRUCTOR OVERLOADING USING (>,++ & +=)

#include<iostream.h>
#include<conio.h>
#include<math.h>
class complex
{
float i,r,m;
public:
void getdata()
{
cout<<"\nEnter the real & imaginary part::";
cin>>r>>i;
}
friend void operator++(complex &);
friend void operator>(complex &,complex &);
friend void operator+=(complex &,complex &);

1.Operator Overloading

WAP TO DISPLAY THE FIBONOCCI NUMBERS AS PER USER CHOICE USING OVERLOADING TECHNIQUE

#include<conio.h>
#include<iostream.h>
class fibo
{
int fo,f1,fib;
public:
fibo(); //constructor
void operator++();
void disp();
};

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();
}

CONSTRUCTOR_3

WAP IN C++ TO INPUT A COMPLEX NUMBER AND ADD THEM USING CONTRUCTOR APPLYING THE TECHNIQUE OF COPY CONTRUCTOR





#include<conio.h>
#include<iostream.h>
class complex
{
int rp,ip;
public:
complex(int x,int y)
{
rp=x;
ip=y;
}
complex(complex &c1,complex &c2)
{
rp=c1.rp+c2.rp;
ip=c1.ip+c2.ip;
}
void disp()
{
cout<<rp<<"+i"<<ip<<endl;
}
};
void main()
{
clrscr();
complex c1(2,5);
complex c2(c1);
complex c3(c1,c2);
c1.disp();
c2.disp();
c3.disp();
getch();
}

CONSTRUCTOR_2

WAP IN C++ TO INPUT 2 COMPLEX NUMBER AND ADD THEM USING CONTRUCTOR

#include<conio.h>
#include<iostream.h>
class complex
{
int rp,ip;
public:
complex(int x,int y)
{
rp=x;
ip=y;
}
complex(complex &c1,complex &c2)
{
rp=c1.rp+c2.rp;
ip=c1.ip+c2.ip;
}
void disp()
{
cout<<rp<<"+i"<<ip<<endl;
}
};
void main()
{
clrscr();
complex c1(2,5);
complex c2(4,6);
complex c3(c1,c2);
c1.disp();
c2.disp();
c3.disp();
getch();
}

CONSTRUCTOR_1

WAP IN C++ USING CONSTRUCTOR TO INPUT MARKS AND CALCULATE THE TOTAL


include<conio.h>
#include<iostream.h>
#include<string.h>
class student
{
char name[30];
int roll;
float m1,m2,m3,tot;
public:
student(char a[30],int b,float x,float y,float z)
{
strcpy(name,a);
roll=b;
m1=x;
m2=y;
m3=z;
}
void calculate()
{
tot=m1+m2+m3;
}
void disp()
{
cout<<"Name::"<<name<<endl
    <<"Roll::"<<roll<<endl
    <<"English::"<<m1<<endl
    <<"Maths::"<<m2<<endl
    <<"OOps::"<<m3<<endl
    <<"Total::"<<tot;
}
};
void main()
{
clrscr();
int b;
char a[30];
float x,y,z;
cout<<"Enter his/her name::";
cin>>a;
cout<<"Enter his/her roll::";
cin>>b;
cout<<"Enter his English marks::";
cin>>x;
cout<<"Enter his Maths marks::";
cin>>y;
cout<<"Enter his OOps marks::";
cin>>z;
student s(a,b,x,y,z);
s.calculate();
s.disp();
getch();
}