Sie sind auf Seite 1von 51

R.M.

K ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY CS2312 /OBJECT ORIENTED PROGRAMMING LAB Aim: To develop object-oriented programming skills using C++ and Java 1. Function overloading, default arguments in C++ 2. Simple class design in C++, namespaces, objects creations 3. Class design in C++ using dynamic memory allocation, destructor, copy constructor 4. Operator overloading, friend functions 5. Overloading assignment operator, type conversions 6. Inheritance, run-time polymorphism 7. Template design in C++ 8. I/O, Throwing and Catching exceptions 9. Program development using STL 10. Simple class designs in Java with Javadoc 11. Designing Packages with Javadoc comments 12. Interfaces and Inheritance in Java 13. Exceptions handling in Java 14. Java I/O 15. Design of multi-threaded programs in Java TOTAL: 45 PERIODS REQUIREMENT FOR A BATCH OF 30 STUDENTS S.No. Description of Equipment Quantity required Hardware Required 1. Computers (Pentium-4) 40 Nos with one server 2. Dot matrix printer 3 Nos 3. Laser Printer 2 Nos. 4. UPS (5 KVA) 2 Software Required 5. Turbo C++ 40 Nodes 6. (Java 2 SDK) JDK 5.0 update 6 (1.5.0 - Internal Version No.) 40

Ex No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Title Students Result Processing using class concept Employee Payroll Processing using array of objects Calculate the volume of different shapes using Function overloading. Interest Calculation with rate of interest as a default value Matrix manipulation using constructors, destructors,dynamic memory allocation of matrix and assignment operator overloading Complex number arithmetic using operator overloading Vector matrix multiplication using friend function Conversion between meter and centimeter using constructor and operator function Student result processing using inheritance Finding area and volume of various shapes using virtual function and typeid (rtti) Insertion sorting using function template Stack operation using class template Operation on stack with exception handling Complex number arithmetic and storing the result in file Program development using STL Simple class designs in Java with Javadoc Interfaces in Java Inheritance in java Exceptions handling in Java Java I/O Design of multi-threaded programs in Java Designing Packages with Javadoc comments

SIMPLE CLASS DESIGN IN C++, NAMESPACES, OBJECTS CREATIONS EXNO: 1 AIM: To write a C++ program to maintain student detail and compute the grade using class concept. ALGORITHM: Step1: Start the program Step2: Define a class student with the data members name, roll, marks, total, average, Grade . Step3: Declare the member functions getdata, totalmark, avg, grade and printdata. Step4: The function getdata is used to accept name, roll and subject marks. Step5: The function totalmark is used to calculate the total by adding the marks. Step6: The function avg is used to calculate the average by dividing the total mark by no Of subjects. Step7: The function grade is used to compute the grade based on the average marks. Step8: The function printdata is used to display the name, roll, total, average, grade. Step9: In the main function declare an object for the class student and call the member Function one by one. Step10: Stop the program PROGRAM: #include<iostream> #include<string> using namespace std; class student { string name; int i; int roll; int m[6]; int total; int avg; char grade; public: void getdata(); void totalmar(); void avg1(); void grade1(); void display(); 3 STUDENTS RESULT PROCESSING

}; void student::getdata() { cout<<"Enter the name:"; cin>>name; cout<<"\nEnter the roll:"; cin>>roll; cout<<"\nEnter the marks:"; for(i=0;i<6;i++) cin>>m[i];} void student::totalmar() { total=0; for(i=0;i<6;i++) total=total+m[i];} void student::avg1() { avg=total/6;} void student::grade1() { if(avg>90 ) grade='A'; else if(avg >80) grade='B'; else if(avg>70) grade='C'; else if(avg>60) grade='D'; else if(avg>50) grade='E'; else grade='F'; } void student :: display() { cout<<"name:"<<name<<"\n"; cout<<"rollno:"<<roll<<"\n"; cout<<"total:"<<total<<"\n"; cout<<"average:"<<avg<<"\n"; cout<<"grade:"<<grade<<"\n"; } int main() { student s; s.getdata(); s.totalmar(); s.avg1(); s.grade1(); s.display(); return 0;} 4

EXNO: 2 AIM:

EMPLOYEE PAYROLL PROCESSING USING ARRAY OF OBJECTS

To write a C++ program to compute the employee salary using array of objects concept. ALGORITHM: Step1: Start the program Step2: Define a class employee with the data members empid, name, desination, basic, Hra, cca, da,gross. Step3: Declare the member functions getdata, calhra, calcca, calda, calgross,display. Step4: The function getdata is used to accept name, empid,designation,basic. Step5: The function calhra is used to compute hra by using the formula basic*0.12. Step6: The function calcca is used to compute cca by using the formula basic*0.09. Step7: The function calda is used to compute da by using the formula basic*0.57. Step8: The function calgross is used to calculate the net salary by adding the basic, hra, cca, da. Step9: The function display is used to display the empid, name, designation and gross Salary. Step10: In the main function declare an object for the class employee and call the member function by using the object created. Step11: Calculate the gross salary for more than one employee by declaring an array of Objects. Step12: Stop the program. PROGRAM: #include<iostream> #include<string> using namespace std; class employee { string name; int empid; string designation; float basic; float hra; float cca; float da; float gross; public: void getdata(); void calhra(); void calcca(); void calda(); void calgross(); 5

void display(); }; void employee::getdata(){ cout<<"Enter the employee name:"; cin>>name; cout<<"\nEnter the employee id:"; cin>>empid; cout<<"\nEnter the employee designation:"; cin>>designation; cout<<"\nEnter the basic salary:"; cin>>basic; } void employee::calhra(){ hra=0.12*basic; } void employee::calda(){ da=0.57*basic; } void employee::calcca(){ cca=0.09*basic; } void employee ::calgross(){ gross=basic+cca+da+hra; } void employee :: display() { cout<<"Employee name:"<<name<<"\n"; cout<<"Employee id:"<<empid<<"\n"; cout<<"Employee designation"<<designation<<"\n"; cout<<"gross salary:"<<gross<<"\n"; } int main(){ int i,n; cout<<"\nEnter the no of employees:"; cin>>n; employee e[n]; for(i=0;i<n;i++) { e[i].getdata(); e[i].calhra(); e[i].calda(); e[i].calcca(); e[i].calgross(); e[i].display(); }return 0; } 6

FUNCTION OVERLOADING, DEFAULT ARGUMENTS IN C++ EXNO: 3 FUNCTION OVERLOADING AIM: To Calculate the volume of different shapes using the function overloading concept. PROGRAM: #include<iostream> using namespace std; #define pi 3.141 class shape { public: void volume(int); void volume(int,float); void volume(int,float,double); }; void shape::volume(int r) { int vol=pow(r,3); cout<<"\n\n Volume of Cube is:\t"<<vol; } void shape::volume(int r,float h) { float vol=pi*r*h; cout<<"\n\n Volume of Cylinder is :\t"<<vol; } void shape::volume(int l,float b,double h) { double vol=l*b*h; cout<<"\n\n volume of Rectangle is :\t"<<vol; } main () { shape s; int r; float h,l; double b; cout<<"\n Enter the value for r:\t"; cin>>r; s.volume(r); cout<<"\n\n Enter the value for r & h:\t"; cin>>r>>h; s.volume(r,h); cout<<"\n\n Enter the value for l,b,b:\t"; cin>>l>>b>>h; s.volume(l,h,b); } 7

EXNO: 4 INTEREST CALCULATIONS WITH RATE OF INTEREST AS A DEFAULT VALUE AIM: To write a C++ program to calculate the simple interest and compound interest with rate of interest as the default argument. ALGORITHM: Step1: Start the program Step2: Define a class interest with the data members principal(p), years(n),rate(r),simple interest (si) and compound interest(ci). Step3: Declare the member functions simple, compound, printline, print. Step4: The function simple is used to compute simple interest using the formula (p*n*r)/100 Step5: The function compound is used to calculate the compound interest using the formula p(1+r/100)n . Step6: The function printline is used to display character for a finite number of times. Step7: The function print calls the function printline and displays simple interest and compound interest. Step8: In the main function declare an object for the class interest and get the input for principal, number of years and rate of interest. Step9: If the same default rate of interest has to be used type zero otherwise enter a new rate of interest. Step10: Stop the program PROGRAM: #include<iostream> #include<cmath> using namespace std; class interest { public: float p,n,r,si,ci; void simple(float p,float n,float r=15); void compound(float p,float n,float r=10); void printline(char='*',int len=40); void print(); }; void interest::simple(float p,float n,float r) { si=p*n*r/100; } 8

void interest::compound(float p,float n,float r) { ci=p*pow(1+r/100,n)-p; } void interest::printline(char ch,int len) { for(int i=1;i<=len;i++) cout<<ch; cout<<"\n"; } void interest::print() { printline(); cout<<"simple interest:"<<si<<"\n"; cout<<"compound interest:"<<ci<<"\n"; printline('='); } int main() { interest i; cout<<"enter the principal:"; cin>>i.p; cout<<"\nEnter the no of year:"; cin>>i.n; cout<<"\nenter 0 if same rate else enter the rate of interest"; cin>>i.r; if(i.r==0) { i.simple(i.p,i.n); i.compound(i.p,i.n); i.print(); } else { i.simple(i.p,i.n,i.r); i.compound(i.p,i.n,i.r); i.print(); } return 0; } CLASS DESIGN IN C++ USING DYNAMIC MEMORY ALLOCATION, DESTRUCTOR, COPY CONSTRUCTOR, OVERLOADING ASSIGNMENT OPERATOR EXNO: 5 MATRIX MANIPULATION WITH CONSTRUCTORS DYNAMIC MEMORY ALLOCATION OF MATRIX 9

FOR

AIM: To write a C++ program to perform matrix manipulation using constructor, destructor, copy constructor, overloaded assignment operator and dynamic memory allocation concept. ALGORITHM: Step1: Start the program Step2: Define a class matrix with r,c and two dimensional array as data member. Step3: A constructor is used to assign value for r,c. Memory is allocated dynamically for the two dimensional array. Step4: A copy constructor is defined to copy the content of one matrix object to another. Step5: The function read is used to get the matrix elements and display function to display the matrix. Step6: A destructor is also defined to destroy the objects created. Step7: The function operator= is used to overload the assignment operator. It is used to assign all the values of one matrix to another matrix. Step8: The function add is used to add two matrices and subtract function is used to find the difference between two matrices. Step9: The function multiply is used to find the product of two matrices. Step10: In the main function, create objects for matrix class. Read the elements of matrix and perform the matrix manipulation. Step11: Stop the program PROGRAM: #include<iostream> using namespace std; class Matrix { int r,c; int **element; public: Matrix(int r1,int c1) { r=r1; c=c1; element =new int *[r]; for(int i=0;i<r;i++) { element[i]=new int[c]; } 10

} void operator=(Matrix &N) { r=N.r; c=N.c; element =new int *[r]; for(int i=0;i<r;i++) { element[i]=new int[c]; } for(int i=0;i<r;i++) for(int j=0;j<c;j++) element[i][j]=N.element[i][j]; } Matrix(Matrix &M) { r=M.r; c=M.c; element =new int *[r]; for(int i=0;i<r;i++) { element[i]=new int[c]; } for(int i=0;i<r;i++) for(int j=0;j<c;j++) element[i][j]=M.element[i][j]; } void Read() { for(int i=0;i<r;i++) for(int j=0;j<c;j++) cin>>element[i][j]; } void Print() { cout<<"\n Resultant Matrix\n"; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) cout<<element[i][j]<<"\t"; cout<<"\n"; } } void Add(Matrix m1, Matrix m2) 11

{ if((m1.r==m2.r)&&(m1.c==m2.c)) { for(int i=0;i<m1.r;i++) for(int j=0;j<m1.c;j++) element[i][j]=m1.element[i][j]+m2.element[i][j]; } else cout<<"Matrix Addition Not Possible"; } void Subtract(Matrix m1, Matrix m2) { if((m1.r==m2.r)&&(m1.c==m2.c)) { for(int i=0;i<m1.r;i++) for(int j=0;j<m1.c;j++) element[i][j]=m1.element[i][j]-m2.element[i][j]; } else cout<<"Matrix Subtraction Not Possible"; } void Multiply(Matrix m1, Matrix m2) { if(m1.c==m2.r) { for(int i=0;i<m1.r;i++) for(int j=0;j<m2.c;j++) { element[i][j]=0; for(int k=0;k<m1.c;k++) element[i][j]=element[i][j]+m1.element[i] [k]*m2.element[k][j]; } } else cout<<"Matrix Addition Not Possible"; } ~Matrix() { delete element; cout<<"\nobject is deleted"; } }; int main() { 12

int r,c; cout<<"\nrows size of matrix1 :"; cin>>r; cout<<"\ncolumn size of matrix1 :"; cin>>c; Matrix A(r,c); A.Read(); cout<<"\nrows size of matrix2 :"; cin>>r; cout<<"\ncolumn size of matrix2 :"; cin>>c; Matrix B(r,c); B.Read(); Matrix C(r,c); C.Add(A,B); Matrix out1=C; C.Subtract(A,B); Matrix out2=C; C.Multiply(A,B); Matrix out3(r,c); out3=C; cout<<"Result of Addition\n"; out1.Print(); cout<<"Result of Subtraction\n"; out2.Print(); cout<<"Result of Multiplication\n"; out3.Print(); C.Read(); C.Print(); out3.Print(); } OPERATOR OVERLOADING EXNO: 6 COMPLEX NUMBER OVERLOADING AIM: To write a C++ program to perform arithmetic operations on complex numbers using operator overloading. ALGORITHM: Step1: Start the program 13 ARITHMETIC USING OPERATOR

Step2: Define a class complex with data members real and img and operator overloading functions for the operator +,-,*,/,<<,>>,==,+=,-+,*=,/=. Step3: The function operator+ is used to add two complex numbers and return the sum. Step4: The function operator- is used to find the difference between two complex numbers and return the result. Step5: The function operator* is used to find the product of two complex numbers and return the product. Step6: The function operator/ is used to perform the division operation on the complex numbers and return the result. Step7: The function operator== is used to check whether the two complex numbers are equal. Step8: The operator function >> and << are used to read a complex number and display a complex number respectively. Step9: In the main function, create objects for complex class. Read two complex number and perform complex number manipulation. Step10: Stop the program PROGRAM: #include<iostream> using namespace std; class complex { float real; float img; public: complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); int operator==(complex); void operator+=(complex); void operator-=(complex); void operator*=(complex); void operator/=(complex); friend istream & operator>>(istream &, complex &); friend ostream & operator<<(ostream &,complex); }; complex complex::operator+(complex c) { complex temp; temp.real=real+c.real; temp.img=img+c.img; return temp; } 14

complex complex::operator-(complex c) { complex temp; temp.real=real-c.real; temp.img=img-c.img; return temp; } complex complex::operator*(complex c) { complex temp; temp.real=real*c.real-img*c.img; temp.img=real*c.img+img*c.real; return temp; } complex complex::operator/(complex c) { complex res; float temp=c.real*c.real+c.img * c.img; res.real=(real*c.real+img*c.img)/temp; res.img=(img*c.real-real*c.img)/temp; return res; } void complex::operator+=(complex c) { real=real+c.real; img=img+c.img; } void complex::operator-=(complex c) { real=real-c.real; img=img-c.img; } void complex::operator*=(complex c) { real=(real*c.real)-(img*c.img); img=(real*c.img)+(img*c.real); } void complex ::operator/=(complex c) { 15

float temp=c.real*c.real+c.img*c.img; real=(real*c.real+img*c.img)/temp; img=(img*c.real-real*c.img)/temp; } int complex::operator==(complex c) { if((real==c.real)&&(img==c.img)) return 1; else return 0; } istream &operator>>(istream &in,complex &c) { cout<<"\n\n Enter the real part"; in>>c.real; cout<<"enter the imaginary part"; in>>c.img; return(in); } ostream &operator<<(ostream &out,complex c) { if(c.img<0) out<<c.real<<"-i"<<-c.img; else out<<c.real<<"+i"<<c.img; return(out); } int main() { complex c1,c2,c3; cout<<"read the first complex number"; cin>>c1; cout<<"\n\n read the second complex number"; cin>>c2; if(c1==c2) cout<<"\n c1 and c2 are equal"; else cout<<"\n c1 and c2 are not equal"; c3=c1+c2; cout<<"\n\n addition result"; cout<<c3; c3=c1-c2; 16

cout<<"\n\n subtrACTION result"; cout<<c3; c3=c1*c2; cout<<"\n\n multiplication result"; cout<<c3; c3=c1/c2; cout<<"\n\n division result"; cout<<c3; c1+=c2; cout<<"\n\n +=result"; cout<<c1; c1-=c2; cout<<"\n\n -=result"; cout<<c1; c1*=c2; cout<<"\n\n *=result"; cout<<c1; c1/=c2; cout<<"\n\n /=result"; cout<<c1; } FRIEND FUNCTIONS EXNO: 7 AIM: To write a C++ program to perform vector-matrix multiplication using friend function. ALGORITHM: Step1: Start the program Step2: Define a class vector with an integer array and integer variable as data member. Step3: Declare the member functions read and display. Step4: Declare a friend function multiplyVM. Step5: Declare a class matrix with a two dimensional integer array and two integer Variables as data members and read, display as member function and multiplyVM as friend function. Step6: The function read of class vector is used to get the size of the vector and the elements of the vector. Step7: The function display of the class vector is used to print the vector elements. 17 VECTOR MATRIX MULTIPLICATION USING FRIEND FUNCTION

Step8: The function read of class matrix is used to get the row and column size of the matrix and the elements of the matrix. Step9: The display function of the class matrix is used to display the resultant matrix. Step10: In the main function, create objects for vector and matrix class. Read the elements of matrix and vector and perform the multiplication and store in a matrix object. Step11: Stop the program PROGRAM: #include<iostream> using namespace std; class matrix; class vector { int a[10]; int n; public: void read(); void display(); friend vector MultiplyVM(vector v, matrix m); }; class matrix { public: int a[10][10]; int m,n; public: void read(); void display(); friend vector MultiplyVM(vector v, matrix m); }; void vector::read() { cout<<"Enter the size of the vector:" ; cin>>n; cout<<"\nEnter the Vector elements:"; for(int i=0;i<n;i++) cin>>a[i]; } void matrix::read() { cout<<"Enter the row size and column size of the matrix:" ; cin>>m>>n; cout<<"\nEnter the matrix elements:"; 18

for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>a[i][j]; } void vector::display() { for(int i=0;i<n;i++) cout<<a[i]<<"\t"; } void matrix::display() { cout<<"Vector-matrix multilpication:\n"; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) cout<<a[i][j]<<"\t"; cout<<"\n"; } } vector MultiplyVM(vector v, matrix m) { vector temp; temp.n=m.n; for (int i=0;i<m.n;i++) { temp.a[i]=0; for(int j=0;j<m.m;j++) temp.a[i]=temp.a[i]+(m.a[i][j]*v.a[j]); } return temp; } int main() { matrix m; vector r,v; m.read(); v.read(); r=MultiplyVM(v,m); r.display(); return 0; }

19

TYPE CONVERSIONS EXNO: 8 CONVERSION BETWEEN METER AND CENTIMETER USING CONSTRUCTOR AND OPERATOR FUNCTION AIM: To write a C++ program to do the conversion between meter to centimeter and vice-versa using constructor and casting operator function. ALGORITHM: Step1: Start the program Step2: Define a class meter with data member lengthm and member functions getlen, getlength and showlength. Step3: Define a default constructor to assign zero to the data member lengthm. Step4: Define a parametrized constructor meter(float length) for converting basic type to user defined type. Step5: The function getlen returns the lengthm and the function getlength is used to accept length in meters and the function showlength is used to display the length. Step6: Define a class centimeter with lengthcm as data member. Step7: Define member functions operator meter, operator float, getlength and showlength. Step8: Define a default constructor to assign zero to the data member lengthcm. Step9: The casting operator function operator float is used for converting user defined type to float type(basic) and the function operator meter is used for converting user defined type to meter(user defined) type. Step10: In the main function create objects for meter class and centimeter class and to the conversions. Step11: Stop the program PROGRAM: #include<iostream> using namespace std; class meter { float lengthm; public: meter() { lengthm=0.0; } // Float to meter Conversion using Constructor 20

meter(float length) { lengthm=length; } float getlen() { return lengthm; } void getlength() { cout<<"\nenter length in meters:"; cin>>lengthm; } void showlength() { cout<<"\nlength in meters:"; cout<<lengthm<<"\n"; } }; class centimeter { float lengthcm; public: centimeter() { lengthcm=0.0; } // Float to Centimeter Conversion using Constructor centimeter(float mlength) { lengthcm=mlength*100.00; } // Meter to Centimeter Conversion using Constructor centimeter(meter met) { lengthcm=met.getlen()*100.00; } // Centimeter to Meter Conversion using operator Function operator meter() { return(meter(lengthcm/100.00)); 21

} // Meter Centimeter to Float Conversion using operator Function operator float() { float lengthm; lengthm=lengthcm/100.00; return lengthm; } void getlength() { cout<<"\nenter length in centimeters:"; cin>>lengthcm; } void showlength() { cout<<"\nlength in centimeters:\n"; cout<<lengthcm; } }; int main() { meter meter1,meter2; centimeter cmeter1,cmeter2,cmeter3,cmeter4; float length1; cout<<"\nbasic to user defined"; cout<<"\nenter length in meters:"; cin>>length1; cmeter1=length1; cmeter1.showlength(); cout<<"\nuser defined to basic"; float length2; cmeter4.getlength(); length2=cmeter4; cout<<"\nlength in meters:"<<length2; cout<<"\none object(meter) type to another(centimeter) type"; meter1.getlength(); cmeter2=meter1; cmeter2.showlength(); cout<<"\none object(centimeter) type to another(meter)"; cmeter3.getlength(); meter2=cmeter3; meter2.showlength();} 22

INHERITANCE, RUN-TIME POLYMORPHISM EXNO: 9 AIM: To write a C++ program to process the student results using the concept of inheritance. ALGORITHM: Step1: Start the program Step2: Define the class student with data members regno, name and member functions read and display. Step3: The function read accepts the register number and name. Step4: The function display prints the register number and name Step5: Define another class studentmarks and inherit some of the properties of the already existing base class student. Step6: Along with the inherited members from the class student add data members m1, m2, m3, m4, m5, m6 and member functions read and display for the class studentmarks Step7: The function read accepts six subject marks, register number, name and display function prints the six subject marks, register number and name. Step8: Define another class result and inherit some of the properties of the already existing class studentmarks thereby exhibiting multilevel inheritance. Step9: Along with the inherited properties from the class studentmarks add the data members total, average, grade and member functions compute and display for the class result. Step10: The function compute calculates total, average and grade for the student. Step11: The function display prints the rgister number, name, six subject marks, total, average, and grade. Step12: In the main function create object for the derived class result and invoke the read, compute and display function. Step13: Stop the program PROGRAM: #include<iostream> using namespace std; class student { int regno; char name[20]; public: void read() 23 STUDENT RESULT PROCESSING USING INHERITANCE

{ cout<<"Enter the Register Number :"; cin>>regno; cout<<"Enter the name :"; cin>>name; } void display() { cout<<"\nRegno :"<<regno; cout<<"\nName :"<<name; } }; class studentmarks: private student { protected : int m1,m2,m3,m4,m5,m6; public: void read() { student::read(); cout<<"Enter 6 subject marks :"; cin>>m1>>m2>>m3>>m4>>m5>>m6; } void display() { student::display(); cout<<"\nMarks:"<<m1<<" "<<m2<<" "<<m3<<" "<<m4<<" "<<m5<<" "<<m6; } }; class Result: public studentmarks { int total; float avg; char grade; public: void compute() { total=m1+m2+m3+m4+m5+m6; avg=total/6.0; if(avg>90) grade='S'; else if(avg>80) grade='A'; else if(avg>70) grade='B'; 24

else if(avg>60) grade='C'; else if(avg>50) grade='D'; else grade='U'; } void display() { studentmarks::display(); cout<<"\nTotal : "<<total; cout<<"\nAverage : "<<avg; cout<<"\n Grade : "<<grade; } }; void main() { Result r; r.read(); r.compute(); r.display(); } EXNO: 10 FINDING AREA AND VOLUME OF VARIOUS SHAPES USING VIRTUAL FUNCTION AND TYPEID (RTTI) AIM: To write a C++ program to find the area and volume of various shapes using virtual function and RTTI. ALGORITHM: Step1: Start the program Step2: Define a class shape which is the base class with read, area and volume as virtual functions. Step3: Define a class square which publicly derives class shape. The member function read is used to accept the side and area is the function used to compute area. Step4: Define a class rectangle which publicly derives class shape. The member function read is used to accept the length and breadth of the rectangle and area is the function used to compute area. Step5: Define a class triangle which publicly derives class shape. The member function read is used to accept the height and base of the triangle and area is the function used to compute area.

25

Step6: Define a class circle which publicly derives class shape. The member function read is used to accept the radius and area is the function used to compute area. Step7: Define a class sphere which publicly derives class shape. The member function read is used to accept the radius. Area and volume function is used to compute area and volume of the sphere respectively. Step8: Define a class cylinder which publicly derives class shape. The member function read is used to accept the radius and height. Area and volume function is used to compute area and volume of the cylinder respectively. Step9: Define a class cone which publicly derives class shape. The member function read is used to accept the radius and height. Area and volume function is used to compute area and volume of the cone respectively. Step10: In the main function, objects of all the derived class are created and also a pointer to the base class. Step11: Based on the shape selected the derived class object is assigned to the base class pointer. Step12: Using RTTI which gives the type of the object, the respective function to calculate the area and volume of different shapes are invoked. Step13: Stop the program PROGRAM: #include<iostream> #include<typeinfo> #include<stdlib.h> using namespace std; class shape { public: virtual void read() {} virtual void area() {} virtual void volume() {} }; class square: public shape { int s; int a; public: void read() { cout<<"Enter the side:"; cin>>s; } void area() { 26

a=s*s; cout<<"Area of the square = "<<a; } }; class rectangle: public shape { int l,b; int a; public: void read() { cout<<"Enter the length:"; cin>>l; cout<<"Enter the breadth:"; cin>>b; } void area() { a=l*b; cout<<"Area of the rectangle = "<<a; } }; class triangle: public shape { int b,h; float a; public: void read() { cout<<"Enter the base:"; cin>>b; cout<<"Enter the height:"; cin>>h; } void area() { a=0.5*b*h; cout<<"Area of the triangle = "<<a; } }; class circle: public shape { int r; float a; public: void read() 27

{ cout<<"Enter the radius:"; cin>>r; } void area() { a=3.14*r*r; cout<<"Area of the circle = "<<a; } }; class sphere: public shape { int r; float v; float a; public: void read() { cout<<"Enter the radius:"; cin>>r; } void area() { a=4*3.14*r*r; cout<<"Area of the sphere = "<<a; } void volume() { v=4/3.0*3.14*r*r*r; cout<<"Volume of the sphere = "<<v; } }; class cylinder: public shape { int r,h; float a; float v; public: void read() { cout<<"Enter the radius:"; cin>>r; cout<<"Enter the height:"; cin>>h; } 28

void area() { a=2*3.14*r*h; cout<<"Area of the cylinder = "<<a; } void volume() { v=3.14*r*r*h; cout<<"Volume of the cylinder = "<<v; } }; class cone: public shape { int r,h; float v; public: void read() { cout<<"Enter the radius:"; cin>>r; cout<<"Enter the height:"; cin>>h; } void volume() { v=1/3.0*3.14*r*r*h; cout<<"Volume of the cone = "<<v; } }; int main() { int opt; shape *ptr; square sq; rectangle rt; triangle tr; circle cr; sphere sp; cylinder cy; cone co; while(1) { cout<<"\n1.square\n"; cout<<"2.rectangle\n"; cout<<"3.triangle\n"; cout<<"4.circle\n"; 29

cout<<"5.sphere\n"; cout<<"6.cylinder\n"; cout<<"7.cone\n"; cout<<"8.Exit\n"; cout<<"Select a shape : "; cin>>opt; switch(opt) { case 1: ptr=&sq; break; case 2: ptr=&rt; break; case 3: ptr=&tr; break; case 4: ptr=&cr; break; case 5: ptr=&sp; break; case 6: ptr=&cy; break; case 7: ptr=&co; break; case 8: exit(0); default:cout<<"wrong choice, choose other "; } if(typeid(*ptr)==typeid(cone)) { ptr->read(); ptr->volume(); } else if((typeid(*ptr)==typeid(sphere))||(typeid(*ptr)==typeid(cylinder))) { ptr->read(); ptr->area(); ptr->volume(); } else { ptr->read(); ptr->area(); } } }

30

TEMPLATE DESIGN IN C++ EXNO: 11 AIM: To write a C++ program to perform insertion sort using function template. ALGORITHM: Step1: Start the program Step2: Create a function template for the function insertion sort so that it can operate on various data types. Step3: The function insertion sort has an array as parameter is used to sort the elements of the array. The elements can be of any type. Step4: Create a temporary variable to do insertion sort. Step5: In the main function call the insertion sort function for different data types such as an integer array, float array. Step6: Stop the program PROGRAM: #include<iostream> using namespace std; template <class T> void insertion(T a[],int n) { T tmp; int i, j; for (i = 1; i < n; i++) { j = i; while (j > 0 && a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; j--; } } } INSERTION SORTING AS FUNCTION TEMPLATE

31

int main() { int n,x[10]; float y[10]; cout<<"Enter the size of the array"; cin>>n; cout<<"read integer"; for(int i=0;i<n;i++) cin>>x[i]; cout<<"read float"; for(int i=0;i<n;i++) cin>>y[i]; // Sorting integers insertion(x,n); // Sorting Float values insertion(y,n); cout<<"sorted x-array"; for(int i=0;i<5;i++) cout<<x[i]<<"\t"; cout<<"\n"; cout<<"sorted y-array"; for(int j=0;j<5;j++) cout<<y[j]<<"\t"; cout<<"\n"; } EXNO: 12 AIM: To write a C++ program to do stack implementation using class template. ALGORITHM: Step1: Start the program Step2: Define a class template for the class stack whose data members are top and stack array.The member functions are push and pop. Step3: The class template can be used to operate on any type of data. STACK AS CLASS TEMPLATE

32

Step4: The function push is used to add elements into the stack. It checks for overflow condition. Step5: Overflow condition means when the stack is full no more elements can be pushed inside the stack. Step6: The function pop is used to delete elements from the stack. It checks for underflow condition. Step7: Underflow condition means when the stack is empty no more elements can be removed from the stack. Step8: In the main function two different types of data i.e integer and character are pushed and popped from the stack using the same class template. Step9: Stop the program PROGRAM 1: #include<iostream> using namespace std; template<class T> class stack { int top; T stackarr[10]; public: stack() { top=0; } void push(T value) { if(top==9) { cout<<"Stack overflow"; } else { stackarr[top]=value; cout<<"The element pushed to stack is:"<<stackarr[top]; top++; } } T pop() { if(top==0) cout<<"Stack underflow"; else { top--; return stackarr[top]; 33

} } }; int main() { stack<int>mystack; mystack.push(10); mystack.push(20); cout<<"\nelement popped from stack is "<<mystack.pop()<<"\n"; cout<<"\nelement popped from stack is "<<mystack.pop()<<"\n"; stack<char>mystack1; mystack1.push('a'); mystack1.push('b'); cout<<mystack1.pop()<<"\n"; cout<<mystack1.pop()<<"\n"; } PROGRAM 2: #include<stdlib.h> int main() { stack<int> istack; stack<char> cstack; int opt; int i; char c; while(1) { cout<<1. Push to Integer Stack; cout<<2. Pop from Integer Stack; cout<<3. Push to Character Stack; cout<<4. Pop from Character Stack; cout<<5. Exit; cout<<Enter your option ; cin>>opt; switch(opt) { case 1: cout<<Enter the integer value : cin>>i istack.push(i); break; 34

case 2: cout<<Popped integers is <<istack.pop(); break; case 3: cout<<Enter the characters value : cin>>c cstack.push(c); break; case 4: cout<<Popped character is <<cstack.pop(); break; case 5: exit(0); } } } I/O, THROWING AND CATCHING EXCEPTIONS EXNO: 13 AIM: To write a C++ program to do operation on stack with exception handling. ALGORITHM: Step1: Start the program Step2: Define a class stack whose data members are top and stack array.The member functions are push and pop. Step3: The function push is used to add elements into the stack. It checks for overflow condition. Step4: Overflow condition means when the stack is full no more elements can be pushed inside the stack. Step5: The try block is used inside the push function suspecting an exception. When an overflow condition is encountered try block will throw an exception. Step6: The catch block will be given immediately after the try block to catch exception and handle it. Step7: The function pop is used to delete elements from the stack. It checks for underflow condition. Step8: Underflow condition means when the stack is empty no more elements can be removed from the stack. Step9: The try block is used inside the pop function suspecting an exception. When an underflow condition is encountered try block will throw an exception. Step10: The catch block will be given immediately after the try block to catch exception and handle it. Step11: In the main function objects are created for the class stack and the member functions are invoked. Step12: Stop the program 35 OPERATION ON STACK WITH EXCEPTION

PROGRAM 1: #include<iostream> using namespace std; const int size=5; class stack { int top; int stackarr[10]; public: stack() { top=0; } void push() { try{ if(top==size) { throw 10; } else { cout<<"Enter the data to be pushed:"; int d; cin>>d; stackarr[top]=d; cout<<"The element pushed to stack is:"<<stackarr[top]; top++; } } catch(int) { cout<<"stack overflow!"; } } void pop() { try { if(top==0) { throw 'a'; 36

} else { top--; cout<<"element popped from stack is:"<< stackarr[top]; } } catch(char) {cout<<"stack underflow!"; } } };

int main() { stack mystack; int ch; do { cout<<"\n1.push"; cout<<"\n2.pop"; cout<<"\n3.exit"; cin>>ch; switch(ch) { case 1: mystack.push(); break; case 2: mystack.pop(); break; case 3: break; } }while(ch<=2); } PROGRAM 2: #include<iostream> #include<string.h> using namespace std; 37

const int size=5; class myexception { public: string msg; void display() { cout<<msg; } } class stack { int top; int stackarr[10]; public: stack() { top=0; } void push() { iff(top==size) { /* Creating an object of type myexception with the error message STACK FULL and throw the same object */ Myexception me1; me1.msg=STACK FULL; throw me1; } else { cout<<"Enter the data to be pushed:"; int d; cin>>d; stackarr[top]=d; cout<<"The element pushed to stack is:"<<stackarr[top]; top++; } } void pop() { if(top==0) 38

{ /* Creating an object of type myexception with the error message STACK EMPTY and throw the same object */ Myexception me2; me2.msg=STACK FULL; throw me2; } else { top--; cout<<"element popped from stack is:"<< stackarr[top]; } }; int main() { stack mystack; int ch; try { do { cout<<"\n1.push"; cout<<"\n2.pop"; cout<<"\n3.exit"; cin>>ch; switch(ch) { case 1: mystack.push(); break; case 2: mystack.pop(); break; case 3: break; } }while(ch<=2); } catch(myexception me) { Me.display(); }}

39

EXNO: 14 IN FILE AIM:

COMPLEX NUMBER ARITHMETIC AND STORING THE RESULT

To write a C++ program to perform arithmetic operation on complex number and to store the result in a file. ALGORITHM: Step1: Start the program Step2: Define a class complex with data members real and img and operator overloading functions for the operator +,-,*,/,<<,>>. Step3: The function operator+ is used to add two complex numbers and return the sum. Step4: The function operator- is used to find the difference between two complex numbers and return the result. Step5: The function operator* is used to find the product of two complex numbers and return the product. Step6: The function operator/ is used to perform the division operation on the complex numbers and return the result. Step7: The operator function >> and << are used to read a complex number and display a complex number respectively. Step8: In the main function, open a file filename.dat. Step9: Read two complex numbers and perform addition, subtraction, multiplication, division and store the result in the file created. Step10: Stop the program PROGRAM: #include<iostream.h> #include<fstream.h> class complex { int real,img; public: friend istream & operator>>(istream&,complex&); friend ostream & operator<<(ostream&,complex&); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); }; complex complex::operator+(complex c) { complex t; 40

t.real=real+c.real; t.img=img+c.img; return(t); } complex complex::operator-(complex c) { complex t; t.real=real-c.real; t.img=img-c.img; return(t); } complex complex::operator*(complex c) { complex temp; temp.real=real*c.real-img*c.img; temp.img=real*c.img+img*c.real; return temp; } complex complex::operator/(complex c) { complex res; float temp=c.real*c.real+c.img * c.img; res.real=(real*c.real+img*c.img)/temp; res.img=(img*c.real-real*c.img)/temp; return res; } istream& operator>>(istream &fin,complex &c) { cout<<"Enter the real part:"; fin>>c.real; cout<<"Enter the imaginary part:"; fin>>c.img; return(fin); } ostream& operator<<(ostream &fout,complex &c) { fout<<c.real<<"+i"<<c.img; return(fout); } void main() { complex c1,c2,c3,c4,c5,c6; ofstream fp("1.dat"); char c,ch; 41

do{ cin>>c1; cin>>c2; c3=c1+c2; fp<<c1<<" c4=c1-c2; fp<<c1<<" c5=c1*c2; fp<<c1<<" c6=c1/c2; fp<<c1<<"

+ "<<c2<<" = "<<c3<<endl; - "<<c2<<" = "<<c4<<endl; * "<<c2<<" = "<<c5<<endl; / "<<c2<<" = "<<c6<<endl;

cout<<"Do you want to continue[y/n] :"; cin>>ch; }while(ch=='y'); fp.close(); } EX:NO:15 PROGRAM DEVELOPMENT USING STL Aim: To write a C++ program to perform various operations on the vector by using the concept of Standard Template Library. program: #include<iostream> #include<vector> using namespace std; class vectr { public: void vecsize(vector<int> &v) { cout<<"current size of the vector="<<v.size()<<"\n"; } void read(vector<int> &v,int n) { int x; cout<<"enter"<<n<<" integer values:"<<"\n"; for(int i=0;i<n;i++) { cin>>x; v.push_back(x); } } void insertend(vector<int> &v,int x) 42

{ v.push_back(x); } void insertanywhere(vector<int> &v,int pos,int x) { vector<int>::iterator itr=v.begin(); itr=itr+pos; v.insert(itr,x); } void delet(vector<int> &v,int pos) { v.erase(v.begin()+pos); } void display(vector<int> &v) { for(int i=0;i<v.size();i++) { cout<<v[i]<<" "; }}}; main() { vectr v1; vector<int>v; int ch,n,x,p; cout<<"initialsize of the vector="<<v.size()<<"\n"; do { cout<<"\n"<<"1.display the size of the vector"<<"\n"; cout<<"2.read elements in the vector"<<"\n"; cout<<"3.insert an element to the end of the vector"<<"\n"; cout<<"4.insert an element anywhere in the vector"<<"\n"; cout<<"5.erase an element at any given position"<<"\n"; cout<<"6.display the contents of the vector"<<"\n"; cout<<"Enter ur choice:"<<"\n"; cin>>ch; switch(ch) { case 1: v1.vecsize(v); break; case 2: cout<<"enter the number of elements to be inserted in the vector:"<<"\n"; cin>>n; v1.read(v,n); v1.display(v); 43

break; case 3: cout<<"Enter the element:"<<"\n"; cin>>x; v1.insertend(v,x); v1.display(v); break; case 4: cout<<"Enter the position:"<<"\n"; cin>>p; cout<<"Enter the element:"<<"\n"; cin>>x; v1.insertanywhere(v,p,x); v1.display(v); break; case 5: cout<<"Enter the position to delete:"<<"\n"; cin>>p; v1.delet(v,p); v1.display(v); break; case 6: v1.display(v); break; } }while(ch<=6); } EX:NO:16 SIMPLE CLASS DESIGN IN JAVA WITH JAVADOC. Aim:To write a java program to calculate the area of rectangle and also use the javadoc utility to create the documentation. Program: import java.util.*; class rectangle { public int length,width; public void getdata(int x,int y) { length=x; width=y;} public int rectarea(){ int area=length*width; return area; }} 44

public class rectarea{ public static void main(String[] args){ Scanner in=new Scanner(System.in); rectangle rect1=new rectangle(); System.out.println("\nEnter the length of the rectangle:"); rect1.length=in.nextInt(); System.out.println("\nEnter the width of the rectangle:"); rect1.width=in.nextInt(); rect1.getdata(rect1.length,rect1.width); int area1=rect1.rectarea(); System.out.println("area of the rectangle="+area1); }} EX:NO17:INTERFACES IN JAVA Aim: To write a java program to calculate the area of rectangle and circle using the concept of interfaces. Program: class area { public static void main(String[] args) { Rectangle r1 = new Rectangle(10,10); Circle c1= new Circle(4); double RectArea=r1.area(); double CirArea=c1.area(); System.out.println("The Area of the Rectangle is "+RectArea); System.out.println("The Area of the Circle is "+CirArea); }} interface ComputeArea { double area(); } class Rectangle implements ComputeArea { private double length,breadth; Rectangle(double l,double b) { length=l; breadth=b; } public double area() { return(length*breadth); } class Circle implements ComputeArea { private double radius; private static final double PI=3.14;

45

Circle(double r) { radius=r; } public double area() { return(PI*radius*radius); EX:NO18: INHERITANCE IN JAVA

}}

Aim:To write a java program to calculate the sum of nos using the inheritance concept.

46

EX:NO19: EXCEPTIONS HANDLING IN JAVA Aim:To write a java program to divide two numbers using exception handling concept

47

EX:NO20: JAVA I/O Aim:To write a java program to read the input and store it in a file.

48

EX:NO21: DESIGN OF MULTI-THREADED PROGRAMS IN JAVA Aim:To write a java program to demonstrate multithread concept

EX:NO 22: DESIGNING PACKAGES WITH JAVADOC COMMENTS 49

Aim: To write a java program to perform arithmetic operation by creating packages.

50

51

Das könnte Ihnen auch gefallen