Oops (1)

.pdf
School
Harvard University**We aren't endorsed by this school
Course
COM 161
Subject
Computer Science
Date
Dec 19, 2024
Pages
16
Uploaded by MagistrateHornetPerson1303
2/3/2016C++ labOOPS With C++ Programming :LabProgram 1: Given that an EMPLOYEE class contains following members:Data members: Employee_Number, Employee_Name, Basic, DA, IT, Net_Sal. Member Functions: to read the data, to calculate Net_Sal and to print data members. Write a C++ program to read the data of N employees and compute Net_Sal of each employee (DA= 52% of Basic and Income Tax(IT)=30% ofthe gross salary).#include<iostream.h>#include<conio.h>class employee{public:int id;char name[20];float net,da,gross,it,basic;public:void accept(); void cal(); void display();void employee :: accept(){cout<<"Enter the employee ID:"<<endl; cin>>id;cout<<"Enter the employee Name:"<<endl; cin>>name;cout<<"Enter the basic salary of the "<<name<<endl; cin>>basic;}void employee :: cal(){da = 0.52 * basic; gross = basic + da;it = 0.30 * gross; net = gross it;}void employee :: display(){cout<<"\n"<<id<<"\t"<<name<<"\t"<<basic<<"\t"<<net<<"\t"<<gross<<"\t"<<da<<"\t"<<it<<endl;}void main(){file:///D:/notes/c++/c++lab.htmlDownloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labemployee emp[5]; clrscr();for(int i=0;i<2;i++){emp[i].accept();emp[i].cal();}cout<<"\nID\tName\tBasic\tNet\tGross\tDA\tIT"<<endl;cout<<""<<endl;for(i=0;i<2;i++){emp[i].display();}getch();}OUTPUTEnter the employee ID: 1Enter the employee Name: naveenEnter the basic salary of the naveen: 20000Enter the employee ID: 2Enter the employee Name: pradeepEnter the basic salary of the pradeep: 25000IDName Basic Net Gross DA IT1naveen 20000 21280 30400 10400 91202pradeep 25000 26600 38000 13000 11400 Program 2: Define a STUDENT class with USN, Name and marks in 3 testsof a subject, declare an array of 10 student objects find the average of two better marks for each student. Print USN, Name and average marks of all the students.#include<iostream.h>#include<conio.h>class student{public:char usn[15]; char name[20];file:///D:/notes/c++/c++lab.html2/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labint marks[20];public:void accept(); void sort(); void display();void student :: accept(){cout<<"\nEnter the Student USN:"; cin>>usn;cout<<"\nEnter the name of the student"<<endl; cin>>name;cout<<"\n";for(int i=1;i<=5;i++){cout<<"Enter the marks of Subject "<<i<<endl; cin>>marks[i];}}void student :: sort(){for(int i=1;i<=5;i++){for(int j=1;j<=4;j++){if(marks[j]<marks[j+1]){int temp; temp=marks[j]; marks[j]=marks[j+1];marks[j+1]=temp;}}}}void student :: display(){sort(); float avg;avg = (marks[1]+marks[2])/2; cout<<usn<<"\t\t"<<name<<"\t\t"<<avg<<endl<<endl;}void main(){clrscr();file:///D:/notes/c++/c++lab.html3/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labstudent s[20]; for(int i=1;i<=2;i++){s[i].accept();}cout<<"\nUSN\t\t\tName\t\tAverage"<<endl;cout<<""<<endl;for(i=1;i<=2;i++){s[i].display();}getch();}OUTPUTEnter the Student USN:1rn03it06Enter the name of the studentPramodEnter the marks of Subject 1 40Enter the marks of Subject 2 36Enter the marks of Subject 3 40Enter the marks of Subject 4 46Enter the marks of Subject 5 20Enter the Student USN:1rn03it07Enter the name of the studentAshvinEnter the marks of Subject 1 45Enter the marks of Subject 2 48Enter the marks of Subject 3 36Enter the marks of Subject 4 47Enter the marks of Subject 5 40USNName Average1rn03it06Pramod 43file:///D:/notes/c++/c++lab.html4/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ lab1rn03it07Ashvin 47Program 3: Write a C++ program to create a class called COMPLEX andimplement the following overloading functions ADD that return a COMPLEX number. ADD(a, s2) where a is an integer (real part) and s2 is a complex number. ADD(s1, s2) where s1 and s2 are complexnumbers.#include<iostream.h>#include<conio.h>class complex{public:int real,img;public:void accept(int,int); void add(int,complex);void add(complex,complex);void complex :: accept(int i,int j){real=i;img=j;}void complex :: add(int i,complex c1){c1.real=i+c1.real;cout<<"\n The result is:"<<endl; cout<<""<<endl; cout<<"\n"<<c1.real<<"+"<<c1.img<<"i"<<"\n"<<endl;}void complex :: add(complex c1,complex c2){complex c3; c3.real=c1.real+c2.real; c3.img=c1.img+c2.img;cout<<c3.real<<"+"<<c3.img<<"i"<<endl;}void main(){complex c1,c2; clrscr();cout<<"\n Enter the integer value:"<<endl; int n;cin>>n;cout<<"\n Enter the real and imaginary parts of the first complex number:"<<endl;file:///D:/notes/c++/c++lab.html5/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labint real,img,real1,img1;cin>>real>>img;cout<<"\n Enter the real and imaginary parts of the second complex number:"<<endl; cin>>real1>>img1;c1.accept(real,img);c2.accept(real1,img1);c1.add(n,c1);c2.add(c1,c2);getch();}OUTPUTEnter the integer value: 10Enter the real and imaginary parts of the first complex number: 2 5Enter the real and imaginary parts of the second complex number: 5 2The result is:12+5i7+7iProgram 4: Write a C++ program to create a class called LIST (linked list) with member functions to insert an element at thefront as well as to delete an element from the front of thelist. Demonstrate all the function after creating a listobject. Write a C++ program to create a template functionfor Quick Sort and demonstrate sorting of integers and doubles.#include<iostream.h>#include<conio.h>#include<process.h>struct node{int data;struct node *link;typedef struct node *NODE;class list{file:///D:/notes/c++/c++lab.html6/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labevery operation display the results by overloading the operator <<.1.)no_of_days=d1d2; where d1 and d2 are DATE objects. d1>=d2 and no_of_days is an integer. 2)d2=d1no_of_days; where d1 is a DATE object and no_of_days is an integer.#include <iostream.h> #include<conio.h>class DATE{private: int dd; int mm;int yy;public: void getdate();int operator (DATE); DATE operator +(int);void DATE :: getdate(){cout<<"Enter a valid date(dd mm yy)....: "; START:cin>>dd>>mm>>yy; if((mm==2) && (dd>29)){cout<<"Wrong input!!! "; cout<<"\nEnter the date again...:"; goto START;}if((mm>12) || (dd>31)){cout<<"Wrong input!!! "; cout<<endl<<"Enter the date again...:"; goto START;}if((mm==4 || mm==6 ||mm==9 ||mm==11) && (dd>30)){cout<<"Wrong input!!! "; cout<<endl<<"Enter the date again...:"; goto START;}if((yy % 4)!=0 && (mm ==2) && (dd>28)){cout<<"Wrong input!!! "; cout<<endl<<"Enter the date again...:"; goto START;}} //End of getdate() member function file:///D:/notes/c++/c++lab.html19/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labint DATE:: operator (DATE d2){int i,nod1,nod2,nody,lc, no_of_days; nod1 = nod2 = lc =0;for(i=1;i< mm;i++){if(i==1 || i== 3 || i==5 ||i==7 || i==8 || i==10 || i==12){nod1 = nod1 + 31;}else if(i==2)nod1 = nod1 + 28; elsenod1 = nod1 + 30;}nod1 = nod1 + dd;for(i=1;i< d2.mm;i++){if(i==1 || i== 3 ||i==5 ||i==7 ||i==8 ||i==10 ||i==12){nod2 = nod2 + 31;}else if(i==2)nod2 = nod2 + 28; elsenod2 = nod2 + 30;}nod2 = nod2 + d2.dd; nody = (yy d2.yy) * 365;for(i = d2.yy; i<yy; i++){if((i % 4) ==0){lc = lc +1;}}int y4 = yyd2.yy;while(y4 > 400){lc = lc + 1; y4 = y4 400;}if((mm >2) && (yy % 4)==0){lc = lc +1;}if((d2.mm >2) && (d2.yy % 4)==0)file:///D:/notes/c++/c++lab.html20/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ lab{lc = lc 1;}no_of_days = nody + nod1 nod2 + lc;if (no_of_days > 0){cout<<"Total number of days between these dates is = "; return (no_of_days);}else{return(no_of_days);}} //End of opertor functionDATE DATE:: operator +(int nd){DATE dd3;while(nd > 365){yy = yy+1; nd = nd365;}while(nd > 30){if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12){nd = nd31; mm = mm+1;}else if(mm == 2){nd = nd28 ; mm= mm+1;}else{nd = nd30; mm = mm+1;}if(mm > 12){yy = yy+1;mm = 1;}file:///D:/notes/c++/c++lab.html21/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ lab}dd = dd + nd;if(dd > 30){if(mm==4 || mm==6 ||mm==9 ||mm==11){mm = mm+1;dd = dd 30;}else if(mm == 2){mm = mm+1;dd = dd 28;}else if(dd > 31){mm = mm+1;dd = dd 31;}}cout<<"New date is : "; cout<<dd<<"-"<<mm<<""<<yy<<endl; return (dd3);} //End of operator + function void main(){DATE dd1,dd2; int res,num; clrscr();BEGIN:dd1.getdate();dd2.getdate();res = dd1 dd2;if(res < 0){cout<<"\nThe first date should be greater than second date\n"; cout<<"So enter the dates again..:"<<endl;goto BEGIN;}cout<<res;cout<<"\nEnter the number of days to be added to FIRST date : "; cin>>num;dd2 = dd1 + num; getch();file:///D:/notes/c++/c++lab.html22/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ lab}OUTPUTEnter a valid date(dd mm yy)....: 12 01 2001 Enter a valid date(dd mm yy)....: 12 01 2000 Total number of days between these dates is = 366Enter the number of days to be added to the FIRST date : 17 New date is : 2912001Enter a valid date(dd mm yy)....: 29 02 1999 Wrong input!!!Enter the date again...:28 02 1999Enter a valid date(dd mm yy)....: 32 03 1998 Wrong input!!!Enter the date again...:31 03 1998Total number of days between these dates is = 334Enter the number of days to be added to the FIRST date : 3 New date is : 331999Enter a valid date(dd mm yy)....: 12 12 2000 Enter a valid date(dd mm yy)....: 12 01 2001The first date should be greater than second date So enter the dates again..:Enter a valid date(dd mm yy)....: 12 01 2001 Enter a valid date(dd mm yy)....: 12 12 2000Total number of days between these dates is = 31Enter the number of days to be added to the FIRST date : 20 New date is : 122001Do you want to continue [y/n]? nProgram 8: Write a C++ program to create a class called MATRIX using a two dimensional array of integers. Implement the following operations by overloading the operator== which checks the compatibility of two matrices to be added and subtracted. Perform the addition and subtraction by overloading the operators + and – respectively. Display the results by overloading the operator<<.if(m1==m2){m3=m1m2;m4=m1+m2;}else display error#include<iostream.h>#include<conio.h>class matrix{file:///D:/notes/c++/c++lab.html23/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labpublic:long m[5][5]; int row,col;public:matrix(int col1,int row1){col=col1;row=row1;}matrix(){}void getdata(int n){cout<<"Enter the elements of "<<n<<" matrix"<<endl; for(int i=0;i<row;i++){for(int j=0;j<col;j++){cin>>m[i][j];}}}int operator ==(matrix); matrix operator +(matrix); matrix operator (matrix);friend ostream& operator<<(ostream&, matrix&); };int matrix::operator ==(matrix cm){int ch; if(row==cm.row&&col==cm.col){return 1;}return 0;}matrix matrix::operator +(matrix am){matrix temp;for(int i=0;i<row;i++){for(int j=0;j<col;j++){temp.m[i][j]=m[i][j]+am.m[i][j];}temp.row=row;temp.col=col;}file:///D:/notes/c++/c++lab.html24/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labreturn temp;}matrix matrix::operator (matrix sm){matrix temp;for(int i=0;i<row;i++){for(int j=0;j<col;j++){temp.m[i][j]=m[i][j]sm.m[i][j];}temp.row=row;temp.col=col;}return temp;}ostream& operator <<(ostream& cout,matrix& d){for(int i=0;i<d.row;i++){for(int j=0;j<d.col;j++){cout<<d.m[i][j]; cout<<" }cout<<"\n";}}void main(){matrix m3,m4;int col1,row1,col2,row2; clrscr();cout<<"\n Enter the Order of the 1 matrix"<<endl; cin>>col1>>row1;cout<<"\n Enter the order of the 2 matrix"<<endl; cin>>col2>>row2;matrix m1(col1,row1),m2(col2,row2); if(m1==m2){m1.getdata(1);m2.getdata(2);m3=m1+m2;m4=m1m2;cout<<"Addition:\n";cout<<m3;cout<<"Subtraction:\n";cout<<m4;file:///D:/notes/c++/c++lab.html25/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ lab}else{cout<<"order of input matrices are not identical...\n";}getch();}OUTPUTEnter the Order of the 1 matrix 2 3Enter the order of the 2 matrix 2 3Enter the elements of 1 matrix 5 5 1 6 4 2Enter the elements of 2 matrix 1 54 8 9 4 5Addition6 59 9 15 8 7Subtraction4 49 7 3 0 -3Program 9: Write a C++ program to create a class called OCTAL which has the characteristics of an octal number. Implement the following operations by writing an appropriate constructor and an overloaded operator+. OCTAL h=x; where x is an integer.int y=h+k; where h is an OCTAL object and k is an integer. Display the OCTAL results by overloading the operators <<. Also display the values ofh and y.#include<iostream.h>#include<conio.h>class octal{int oct, dec, ten;file:///D:/notes/c++/c++lab.htmlDownloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labpublic:octal(){oct = 0;ten = 1;}void operator =(int);int operator +(int);friend ostream & operator<<(ostream& , octal& c);void octal :: operator=(int x){intdec = x;while(x != 0){r = x % 8;x = x / 8;oct = oct + ten * r; ten = ten * 10;}}int octal :: operator+(int k){return(dec + k);}ostream &operator<<(ostream &s,octal &c){s<<c.oct;return s;}void main(){octal h;int n,k;clrscr();cout<<"Enter a Integer to change to octal : "; cin>>n;cout<<endl;h = n;cout<<"The octal value of "<<n<<" is = "<<h<<endl; cout<<"Enter any integer k to be added to previous octal : "; cin>>k;cout<<endl; int y = h + k;cout<<"result of octal h + int k is : "<<y;getch();}file:///D:/notes/c++/c++lab.html27/50Downloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image
2/3/2016C++ labOUTPUTEnter a Integer to change to octal : 10The octal value of 10 is = 12Enter any integer k to be added to previous octal : 10Result of octal h + int k is : 20Enter a Integer to change to octal : 12The octal value of 10 is = 14Enter any integer k to be added to previous octal : 10Result of octal h + int k is : 22Program 10: Write a C++ program to create a class called QUEUE with member functions to add an element and to delete an element from the queue Using these member functions, implementa queue of integer and double. Demonstrate the operations by displaying the content of the queue afterevery operation.#include <iostream.h> #include <iomanip.h>template <class T> class queue{public :queue(int max = 4); // Max size of queue is 4~queue(){ delete [] pqptr; };void insert(const T&) ;int qfull() const{ return rear == nsize ; }int qempty() const{ return (front >= rear || rear == 0) ; }T del() ;void display() ;private :int nsize;int rear;int front;T *pqptr;} ;template <class T>queue <T> :: queue(int nx)file:///D:/notes/c++/c++lab.htmlDownloaded by msdgoasng sgpamapgp (m.fwasgw@gmail.com)lOMoARcPSD|50743603
Background image