0% found this document useful (0 votes)
179 views

Oops Lab Manual

The document describes several experiments involving C++ programming concepts like parameter passing methods, default arguments, classes, pointers, static class members, friend functions, and constructor overloading. The experiments include code examples to demonstrate how to implement these concepts through simple programs.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Oops Lab Manual

The document describes several experiments involving C++ programming concepts like parameter passing methods, default arguments, classes, pointers, static class members, friend functions, and constructor overloading. The experiments include code examples to demonstrate how to implement these concepts through simple programs.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 44

Expt.No.

1 Program for swapping using Parameter Passing Methods Aim : To write a C++ program for various parameters passing methods.a) Pass by value b) Pass by address (or) pointer c) Pass by reference Algorithm(a): Step1:START the program Step2:Include the header files. Step3:Declare two data members a and b. Step4:Get the values of a and b. Step5:Call the functions by passing the values. Step6:Declare a temporary data member inside the function. Step7:Swap the values of a and b. Step8:Print the output. Step9:STOP the program. Algorithm(b): Step1:START the program Step2:Include the header files. Step3:Declare two data members a and b. Step4:Get the values of a and b. Step5:Call the functions by passing their addresses with the use of pointers. Step6:Declare a temporary data member inside the function. Step7:Swap the values of a and b. Step8:Print the output. Step9:STOP the program.

Algorithm(c): Step1:START the program Step2:Include the header files. Step3:Declare two data members a and b. Step4:Get the values of a and b. Step5:Call the functions by passing the values with the use of reference variables. Step6:Declare a temporary data member inside the function. Step7:Swap the values of a and b. Step8:Print the output. Step9:STOP the program.

Programs: (a).pass by value: #include<iostream.h> #include<conio.h> int swap(int x,int y); void main() { int alb; clrscr(); cout>>Enter the two numbers\n; cin>>a>>b; swap(a,b); getch(); } int swap(int x,int y) { int t; t=x; x=y; y=t; cout<<The interchanged values are\n<<x<<and <<y; return 0; } (b) Pass By Address #include<iostream.h> #include<conio.h> int swap(int *x, int *y); void main() { int a,b; clrscr(); cout<<Enter the two numbers\n; cin>>a>>b; swap(&a,&b); cout<<The interchanged values:\n<<a<<and<<b; getch(); } int swap(int *x, int *y) { int t;

t=*x; *x=*y; *y=t; return 0; } (c) Pass By Reference: #include<iostream.h> #include<conio.h> int swap(int &x, int &y); void main() { int a,b; clrscr(); cout<<Enter the two numbers\n; cin>>a>>b; swap(a,b); cout<<The interchanged values:\n<<a<<and<<b; getch(); } int swap(int &x,int &y) { int t; t=x; x=y; y=t; return 0; }

Expt.No: 2 Date :

Program for Default Arguments

Aim: To write a C++ program to implement the usage of default arguments. Algorithm: Step1: START the program Step2: Include the header files. Step3: Declare the function with character and integer arguments. Step4: Call the function without passing any arguments. Step5: So the function will take the arguments from function prototype. Step6: Call the function again by passing some arguments. Step7: Print the output. Step8: STOP the program. Program: #include<iostream.h> #include<conio.h> void defaultarg(char x, char *, int 3); void main() { defaultarg(); defaultarg($,#); defaultarg(@,^,2); getch(); } void defaultarg(char ch, char ch1,int rep_count) { for(int i=0;i<rep_count;i++) cout<<ch<<ch1; cout<<\n; getch();

Expt.No.3

Program for Creating A Simple Class

Aim: To write a program for creating a simple class program. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is record. Step4:Create the private attributes inside the class. Step5:Create public member functions inside the class. Step6:Create a object for the class. Step7:Access the member functions using the object. Step8:Print the output. Step9:STOP the program.

Program: #include<iostream.h> #include<conio.h> #include<string.h> class record { char name[20]; int m1,m2; float tot,avg; public: void getdata(int a, int b, char n[20]) { m1=a;

m2=b; strcpy(name,n); } void cal() { tot=m1+m2; avg=tot/2; } void disp() { cout<<Enter the name:<<name; cout<<\nMark1:<<m1<<\nMark2:<<m2<<endl; cout<<Total:<<tot<<endl<<Average:<<avg; } }; void main() { clrscr(); record r; r.getdata(99.98,Ram); r.cal(); r.diap(); getch(); }

Expt.No.4 Program for Pointer Data Member within a class

Aim: To write a C++ program using a pointer variable to allocate and deallocate the memory. Algorithm: Step1 Step2 Step3 Step4 :START the program. :Include the header files. :Create a class whose name is vector. :Declare a pointer data member and a integer data member inside it. Step5 :Create public member functions inside the class. Step6 :One of the function is to allocate memory for a specific datatype and for given size. Step7 :Another member function created in order to deallocate the allocated memory in the previous step. Step8 :Create a object for the class inside the main. Step9 :Access the member functions using the objects. Step10:Print the output. Step11:STOP the program. Program: #include<iostream.h> #include<conio.h> class vector { int *v; int sz; public: void vectorsize(int size) { sz=size; v=new int[size]; } void read() { cout<<"Enter the numbers"; int i; for(i=0;i<sz;i++) cin>>v[i]; } void showsum() { int sum=0;

for(int i=0;i<sz;i++) { sum=sum+v[i]; } cout<<"\nThe sum is"; cout<<sum; } void release() { delete v; } }; void main() { clrscr(); vector v1; int count; cout<<"\n Enter the number of elements"; cin>>count; v1.vectorsize(count); v1.read(); v1.showsum(); v1.release(); getch(); }

Expt.No.5(a) Program Using Static Data Member Aim: To write a C++ program using a static data member. Algorithm: Step1:START the program. Step2:Include the header files.

Step3:Create a class whose name is item. Step4:Declare a static data member and a normal integer data member in the class. Step5:Define the static data member outside the class. Step6:Create a object for the class inside the main. Step7:Access the member functions using the objects. Step8:Print the output. Step9:STOP the program. Program: #include<iostream.h> #include<conio.h> class item { static int count; int number; public: void getdata(int a) { number=a; count++; } void getcount() { cout<<"\nThe count is"<<count; } } ; int item::count; void main() { clrscr(); item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); cout<<"\nAfter reading"; a.getcount(); b.getcount(); c.getcount(); getch(); }

Expt.No.5(b) Program Using Static Member Function Aim: To write a C++ program using static member function. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class name is student. Step4:Declare the data members for the class. Step5:Also create a static member function inside the class. Step6:Create the object for the class inside the main. Step7:Access the member function using the object and access the static member function using the class name. Step8:Print the output. Step9:STOP the program. Program: #include<iostream.h> #include<conio.h> #include<string.h> class student { char name[20],grade; int mark1,mark2,mark3,total; float avg; public: void getdata(char n[20], int a,int b,int c) { strcpy(name,n); mark1=a;

mark2=b; mark3=c; } void find() { total=mark1+mark2+mark3; avg=total/3; } void display() { cout<<"The name is:"<<name; cout<<"The marks:"<<mark1<<endl<<mark2<<endl<<mark3; cout<<"\nThe total is"<<total; cout<<"\nThe average is"<<avg; if(avg>=85) cout<<"\nOutstanding"; else if(avg>=75) cout<<"Distinction"; else if(avg>=60) cout<<"\nFirst class"; } static void menu() { cout<<"Student register containing marklist"; } }; void main() { clrscr(); student s; student::menu(); s.getdata("HI",100,100,100); s.find(); s.display(); getch(); }

Expt.No.6(a)

Program Using Friend Function

Aim: To write C++ program using friend function. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is One. Step4:Declare the data members for the class. Step5:Make a non-member function as a friend to the class. Step6:Declare the data members for the class. Step7:Make the non-member function as a friend to that class. Step8:Create objects for both the classes. Step9:Access the non member function by passing the objects. Step10:Print the output. Step11:STOP the program. Program: #include<iostream.h> #include<conio.h> class two; class one { int data1; public: void setdata(int init) { data1=init; } friend int addboth(one a,two b); }; class two { int data2; public: void setdata(int init) { data2=init;

} friend int addboth(one a,two b); }; int addboth(one a1, two b1) { return a1.data1+b1.data2; } void main() { clrscr(); one a; two b; a.setdata(5); b.setdata(10); cout<<"Sum"<<addboth(a,b); getch(); }

Expt.No.6(b) Bridging Between Two Classes using Friend Aim: To write a C++ program for making a bridge between two classes. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Declare the class prototype. Step4:Create a class whose name is boy.. Step5:Declare the data members for the class. Step6:Create member functions for that class.

Step7:Make the class boy as a friend to the class girl. Step8:Create a class whose name is girl. Step9:Declare the data members for the class. Step10:Create member functions using the data members of class boy. Step11:Create the object and access the member function using the object. Step12:Print the output. Step13:STOP the program. Program: #include<iostream.h> #include<conio.h> class girl; class boy { int income1; int income2; public: void setdata(int no1, int no2) { income1=no1; income2=no2; } friend class girl; }; class girl { int income; public: int girlfunc(boy b1) { return b1.income1+b1.income2; } void setdata(int in) { income=in; } void show() { boy b1; b1.setdata(100,200); cout<<"\nBoys income"<<b1.income1; cout<<"\ngirl income"<<b1.income2; }

}; void main() { clrscr(); boy b1; girl g1; b1.setdata(500,1000); g1.setdata(300); cout<<"\nBoy total income:"<<g1.girlfunc(b1); g1.show(); getch(); }

Expt.No.7

Program for Constructor overloading using Friend Function

Aim: To write a constructor overloading program using friend function. Algorithm: Step1:START the program Step2:Include the header files. Step3:Create a class whose name is complex. Step4:Create private data members for the class. Step5:Create one default constructor for that class. Step6:Create a constructor with one argument and with two arguments for same class. Step7:Create a member function for this class. Step8:Also create non member function return type is complex and make it as friend. Step9:Create objects for the class and pass the values to the constructor. Step10:Access the member function using the class. Step11:Print the output. Step12:STOP the program.

Program: #include<iostream.h> #include<conio.h> class complex { float real,imag; public: complex() { } complex(float a) { real=a; imag=a; } complex(float a,float b) [ real=a; imag=b; } void disp() { cout<<real<<"+i"<<imag; } friend complex sum(complex c1,complex c2); }; complex sum(complex c1,complex c2) { complex c3; c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return c3; } void main() { complex a,b,c; a=complex(1,5); cout<<"Complex num for object 'a' is:"; a.disp(); b=complex(2.5,3.5); cout<<"Complex num for object 'b'is:"; b.disp(); c=sum(a,b); cout<<"Addition value is"; c.disp(); getch();

Expt.No.8

Program for Function Overloading

Aim: To write a C++ program to implement function overloading. Algorithm: Step1:START the program. Step2:Declare the prototypes of functions of same name but with different return type an arguments Step3:Define the functions. Step4:Get the values of different datatype values. Step5:Call the functions. Step6:Print the output. Step7:STOP Program: #include<iostream.h> #include<conio.h>] int sum(int a,int b); float sum(float a,float b); double sum(double a,double b); void main() { clrscr(); int a,b,intsum; float x,y,floatsum; double x1,y1,doublesum; cout<<"Enter the integers"; cin>>a>>b; intsum=sum(a,b); cout<<"The value is"<<intsum; cout<<"Enter the float values";

cin>>x>>y; floatsum=sum(x,y); cout<<"The value is"<<floatsum; cout<<"Enter the double values"; cin>>x1>>y1; doublesum=(x1,y1); cout<<"The value is"<<doublesum; getch(); } int sum(int a,int b) { return(a+b); } float sum(float a,float b) { return(a+b); } double sum(double a,double b) { return(a+b); }

Expt.No.9(a) Program for Unary Operator Overloading Aim: To write a C++ program to implement unary operator overloading. Algorithm:

Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is space. Step4:Declare the private data members for the class. Step5:Create member functions inside the public. Step6:Also create a member function with the operator -. Step7:Create a object for this class inside the main(). Step8:Call the function using the object. Steo9:Print the output. Step10:STOP. Program: include<iostream.h> #include<conio.h> class space { int x,y,z; public: void read(int a,int b,int c) { x=a; y=b; z=c; } void disp() { cout<<"\nx="<<x<<"\ny="<<y<<"\nz="<<z; } void operator -() { x=-x; y=-y; z=-z; } }; void main() { clrscr(); space s; s.read(1,2,-3); cout<<"Values before applying minus"; s.disp(); -s; cout<<"\nValues after applying minus"; s.disp();

getch(); }

Expt.No9(b)

Program for Increment And Decrement Operator Overloading

Aim: To write a C++ program to implement increment and decrement operator overloading. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is simple. Step4: Declare the private data members for the class. Step5:Create member functions inside the public. Step6:Also create a member function which performs increment and decrement operation with the operators and ++. Step7:Create a object for the class. Step8:Call the member functions using the objects. Step9:Print the output. Step10:STOP. Program: #include<iostream.h> #include<conio.h> class space { int x,y,z;

public: void read(int a,int b,int c) { x=a; y=b; z=c; } void disp() { cout<<"\nx="<<x<<"\ny="<<y<<"\nz="<<z; } void operator ++() { x=++x; y=++y; z=++z; } void operator --() { x=--x; y=--y; z=--z; } }; void main() { clrscr(); space s; s.read(1,2,-3); cout<<"Values before icrementing"; s.disp(); ++s; cout<<"\nValues after incrementing"; s.disp(); --s; cout<<"\nValues after decrementing"; s.disp(); getch(); }

Expt.No.10

Program for Binary Operator Overloading

Aim: To write a C++ program to implement binary operator overloading. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is complex. Step4:Create a default constructor. Step4:Create two parameterized constructors with one argument and two arguments. Step5:Create the member functions inside the class. Step6:Declare a member functions for the operators +,-,*,/. Step7:Define the member functions with the return type complex. Return the object. Step8:Create objects for the class. Step9:Perform the operations with two objects and assign to another object. Step10:Print the output. Step11:STOP. Program: #include<iostream.h> #include<conio.h> #include<math.h> class complex { float real,img; public: complex() { } complex(float a) { real=a; img=a; } complex(float a,float b) { real=a; img=b;

} void disp() { cout<<real; if(img<0) cout<<"-i"<<fabs(img); else cout<<"+i"<<fabs(img); cout<<"\n"; } complex operator +(complex c2); complex operator -(complex c2); complex operator *(complex c2); complex operator /(complex c2); }; complex complex::operator +(complex c2) { complex c3; c3.real=real+c2.real; c3.img=img+c2.img; return(c3); } complex complex::operator -(complex c2) { complex c3; c3.real=real-c2.real; c3.img=img-c2.img; return(c3); } complex complex::operator *(complex c2) { complex c3; c3.real=real*c2.real; c3.img=img*c2.img; return(c3); } complex complex::operator /(complex c2) { complex c3; c3.real=real/c2.real; c3.img=img/c2.img; return(c3); } void main() { clrscr();

complex c1,c2,c3; c1=complex(2,5); c2=complex(2.5,3.5); c3=c1+c2; c3.disp(); c3=c1-c2; c3.disp(); c3=c1*c2; c3.disp(); c3=c1/c2; c3.disp(); getch(); }

Expt.No:11

Program for Multiple Inheritance

Aim: To write a C++ program to implement multiple inheritance. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is college. Step4:Declare the data members for the class. Step5:Create public member functions for the class. Step6:Create a class whose name is univ. Step7: Declare the data members for the class. Step8: Create public member functions for the class. Step9: Create a class whose name is student. Step10:Inherit the properties of class college and univ to the class student with visibility mode public Step11:Create the object for the derived class. Step12:Call the member function using the objects. Step13:Print the output. Step14:STOP.

Program: #include<iostream.h> #include<conio.h> class college { char name[20],dept[20][20]; int girls[20],boys[20],studno[20],deptno,tot; public: void getcol() { cout<<"Enter the college name and no. of department"; cin>>name>>deptno; } void putcol() { cout<<"\nThe college name is:"<<name; cout<<"The no of dept is:"<<deptno; } void department() { for(int i=0;i<deptno;i++) { cout<<"\nThe dept is: "; cin>>dept[i]; cout<<"\nThe no. of girls: "; cin>>girls[i]; cout<<"\nThe no. of boys: "; cin>>boys[i]; } } void student1() { for(int i=0;i<deptno;i++) { int s=0; cout<<"\nThe dept is: "; cout<<dept[i]; cout<<"\nThe no. girls: "<<girls[i]; cout<<"\nThe no. of boys present: "<<boys[i]; studno[i]=boys[i]+girls[i]; tot=s+studno[i]; cout<<"\nTotal no of students present: "<<tot; } }

}; class univ { protected: char name[20],loc[20]; public: void getuniv() { cout<<"\nEnter the univ name & location: "; cin>>name>>loc; } void putuniv() { cout<<"\nThe university is: "; cout<<name; cout<<"\nThe location is: "<<loc; } }; class student:public college,public univ { char name[20]; int rollno; public: void getstud() { cout<<"\nEnter the student name and rollno.: "; cin>>name>>rollno; } void putstud() { cout<<"\nThe student is: "<<name; cout<<"\nThe roll no: "<<rollno; } }; main() { clrscr(); student s; s.getstud(); s.getcol(); s.getuniv(); s.department(); s.putstud(); s.putcol(); s.putuniv(); s.student1();

getch(); return 0; }

Expt.No.12

Program for Virtual Base Class

Aim: To write a C++ program to implement virtual base class. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a base class whose name is company. Step4:Declare and define data members and member functions of the class. Step5:Create the derived class such as manager and employee with company as its base Class. Step6:Make the base class company as virtual to the derived lass manager and employee. Step7: Declare and define data members and member functions of the derived class. Step8:Derive a class whose name is department with manager and employee as its base class. Step9: Declare and define data members and member functions of the derived class. Step10:create a object for department inside the main(). Step11:Acccess the member functions using the objects. Step12:Print the output. Step13:STOP. Program: #include<iostream.h> #include<conio.h> #include<string.h> class company { protected: char name[20],loc[20]; public: void getcompany() { cout<<"\n Enter the name and location"; cin>>name>>loc;

} void putcompany() { cout<<"\n The name of the comapany is"<<name<<"\nLocation"<<loc; } }; class manager:virtual public company { public: char mname[20],dept[20]; int age; float sal; void getman() { cout<<"\nEnter the manager's name,Department,Age,Salary"; cin>>mname>>dept>>age>>sal; } void putman() { cout<<"\nManager name:"<<mname<<"\nAge"<<age<<"\nDepartment"<<dept<<"\nSalary:"<<sal; } }; class employee:virtual public company { public: char ename[20],eename[20],depart[20]; float salary; void getemp() { cout<<"\nEnter the employee name,Salary,Department"; cin >>ename>>salary>>depart; strcpy(eename,ename); } void putemp() { cout<<"\nEmployee name:"<<eename<<"\nSalary:"<<salary<<"\nDepartment"<<depart; } }; class department:public manager,public employee { public: void getdepartm() { getman();

getemp(); } void find() { char *s1,*s2; strcpy(s1,dept); strcpy(s2,depart); if(strcmp(s1,s2)==0) { cout<<"\n\n\n The employee"<<eename<<"is working under manager"<<mname; } else cout<<"\n\n\n The employee "<<eename<<"is not working under manager"<<mname; } }; void main() { clrscr(); department d; d.getcompany(); d.getdepartm(); d.find(); d.putcompany(); d.putman(); d.putemp(); getch(); }

Expt.No.13

Program for Virtual Functions

Aim: To write a C++ program to implement virtual functions. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a class whose name is university. Step4: Declare and define data members and member functions of the class.

Step5:Create a class with university as its base class. Step6: Declare and define data members and member functions of the derived class. Step7:Also create a member function which has same name in both Base lass and derived class. Step8:Declare the member function which is inside the base class As virtual. Step9:Create an object pointer for the base class and a normal Object for the derived class. Step10:Assign the normal object to the pointer object. Step11:Access the member function using the member access Operator. Step12:STOP Program: #include<iostream.h> #include<conio.h> class univ { char name[20],loc[20]; public: virtual void getdata() { cout<<"Enter the university name and location"; cin>>name>>loc; } virtual void display() { cout<<name<<endl<<loc; } }; class college:public univ { char name[20],loc[20]; public: void getdata() { cout<<"Enter the college name and location"; cin>>name>>loc; } void display() { cout<<name<<endl<<loc; } }; void main() {

clrscr(); univ *u; college c; u=&c; u->getdata(); u->display(); getch(); }

Expt.No.14

Program for Templates

Aim: To write a C++ program to create templates. Algorithm: Step1:START the program. Step2:Include the header files. Step3:Create a template class t. Step4:Create a function with objects as its arguments. Step5:Define the body of the function. Step6:Declare data members inside the main(). Step7:The data members should be of various data types. Step8:Pass the data members to the functions while calling. Step9:Print the output. Step10:STOP Program: #include<iostream.h> #include<conio.h> template<class t> void swap(t a,t b) { t temp; temp=a; a=b; b=temp; cout<<"after interchanging the values are"<<a<<"and"<<b;

} void main() { clrscr(); int g,h; float m,n; double i,j; cout<<"Enter the int"; cin>>g>>h; swap(g,h); cout<<"Enter the float values"; cin>>m>>n; swap(m,n); cout<<"Enter the double values"; cin>>i>>j; swap(i,j); getch(); }

Expt.No.15(a) Program for Sequential Accessing (A) Aim: To write a c++ program for sequential accessing. Algorithm: Step1: fstream class is implemented in the main program Step2: Two files "std .in", "std.out" are created Step3: Connect file object to file Step4: "std.in" file is fail not opened or error Step5: Discard the file finally Program: #include<iostream.h> #include<fstream.h> #include<conio.h> #include<process.h> void main() { clrscr(); fstream infile;

fstream outfile; int i,count,per; char name[30]; infile.open("student.in",ios::in); if(infile.fail()) { cout<<"\nError:cannot open the file in read mode:"; exit(1); } outfile.open("student.out",ios::out); if(outfile.fail()) { cout<<"\nerror:unable to open the file:"; exit(1); } infile>>count; outfile<<"students information processing"<<endl<<endl; cout<<"-----------------------------------"<<endl; for(i=0;i<count;i++) { infile>>name>>per; outfile<<"\n name:"<<name<<endl; outfile<<"\nPercentage:"<<per<<endl; outfile<<"Passed in:"; if(per>=70) outfile<<"\nFirst class with distinction"; else if(per>=45) outfile<<"\nSecond class"; else outfile<<"\nSorry,failed"; } infile.close(); outfile.close(); } getch(); } Expt.No.15(b) Program for Sequential Accessing (B) Aim: To write a c++ program for sequential accessing. Algorithm: Step1:Start the program.

Step2:Include the header file. Step3:Get the required information from the user. Step4:Create an object for the class ifstream. Step5:Open a file explicitly using that object. Step6:Also create an object for the class ofstream. Step7:Open a file explicitly for the object. Step8:Perform the required operations in that file using the objects. Step9:Print the output. Step10:Stop. Program: #include<iostream.h> #include<fstream.h> #include<iomanip.h> #include<conio.h> void main() { clrscr(); char name[20]; char branch[20]; int age; cout<<"enter the name of the student:"; cin>>name; cout<<"enter the branch of the student:"; cin>>branch; cout<<"enter the age of the student:"; cin>>age; ofstream out("student.dat",ios::out|ios::app); out<<name<<" "<<branch<<" "<<age<<endl; out.close(); ifstream in("student.dat",ios::in); while(!in.eof()) { in>>name>>branch>>age; cout<<name<<branch<<age; } in.close(); getch(); } Expt.No.16 Program for Random Acessing Aim: To write a c++ program for random accessing for file.

Algorithm: Step1: In fstream binary mode is declared is main function Step2: Assign number 0 to 9 in the file Step3: Set the write (put) pointer Step4: Set the read (get) pointer Step5: End of file Step6: Finally the result is displayed. Program: #include<iostream.h> #include<conio.h> #include<fstream.h> #define READSIZE 6 Void main() { clrscr(); char reader[READSIZE+1]; fstream fstr(tst.txt,ios::binary|ios::in|ios::out); for(int i=0;i<0;i++) fstr<<i; fstr.seekp(2); fstr<<Hello; fstr.seekg(4); fstr.reader(reader,READSIZE); reader[READSIZE]=0; count.width(10); count.fill(*); cout<<reader; getch(); } Expt.No.17 Program for Constructor Overloading Aim: To write a java program for constructor overloading. Algorithm: Step1:Start the program Step2:Define the class box Step3:Declare the member variables and default constructor Step4:Declare and define the constructor with three and one parameter. Step5:Define the method volume for allocating the volume. Step6:Create another class for main.

Step7:Create objects for passing the values. Step8:Print the volume for each value passed to the constructor. Stop9:Stop the program. Program: class Box { float height,width,depth; Box() { System.out.println("Default constructor invoked for class box"); } Box(float w,float h,float d) { width=w; height=h; depth=d; } Box(float x) { width=height=depth=x; } public float volume() { return width*height*depth; } } class Boxdemo { public static void main(String args[]) { Box b=new Box(); Box b1=new Box(3,4,5); Box b2=new Box(5); float vol; vol=b1.volume(); System.out.println("The volume of box b1 is:"+vol); vol=b2.volume(); System.out.println("The volume of box b2 is:"+vol); } } Expt.No.18 Aim: Program for String Manipulation in java

To write a program for string manipulation in java. Algorithm: Step1: Include a class is created when a name strectraction, strcompare, string modify Step2: String extraction containing the operation to find a character a set. Step3: Strcompare contains the operation to perform that two strings are equal Step4: Str searching is used to find the index of and last index of a particular character Step5: Str modify is used to concatenate and to replace Step6: object are created based on their condition provided each function is invoked. Program: class stringmanip { public void stringextraction() { String str1=new String(); int start=5,end=9,origin=0; char str2[]=new char[end-start]; str1="Welcome to India"; System.out.println("The string is"+str1); System.out.println("to find character at the specified place 3 in str1 is:"); System.out.println(str1.charAt(3)); System.out.println("To extract one or more characters"); str1.getChars(start,end,str2,0); System.out.println(str2); System.out.println("To convert characters in string objects to characters array:"); System.out.println(str1.toCharArray()); } public void strcompare() { String s1="hello welcome"; String s2="hello Goodmorning"; String s3="hello WELCOME"; String ss=new String(s1); String sss=new String(s1); System.out.println("To Check for equal:"+s1.equals(s2)); System.out.println("To Chaeck for equal ignore case:"+s1.equalsIgnoreCase(s3)); System.out.println("To Check for starts with:"+s1.startsWith("hello")); System.out.println("To check for ends with:"+s1.endsWith("come")); System.out.println("To check for'=='is:"+(ss==sss)); } public void strsearching() { String s5="This is a simple test program";

System.out.println("To find first occurance of given characters:"+s5.indexOf('a')); System.out.println("To find last occurance of given characters:"+s5.lastIndexOf('a')); } public void strmodify() { String s6="Hai! How are you"; System.out.println("To concat:"+s6.concat("all")); System.out.println("To repalce:"+s6.replace('H','G')); System.out.println("Hello world".trim()); } public static void main(String args[]) { stringmanip sm=new stringmanip(); int l; l=Integer.parseInt(args[0]); System.out.println(l); if(l==1) { System.out.println("Yes it is one"); sm.stringextraction(); } else if(l==2) { sm.strcompare(); } else if(l==3) { sm.strsearching(); } else if(l==4) { sm.strmodify();}}} Expt.No.19 Aim: To write a program for multiple inheritance using interface concepts. Algorithm: Step1: Interface is same like class where only declaration of function Step2: class can implement from that interface Step3: object for created in each class that is created Step4: function are invoked by using the object Program: Program for Multiple Inheritance using interface

import java.io.*; interface Area { public final static float pi=3.14f; public float compute(float x,float y); } class rectangle implements Area { public float compute(float x,float y) { return(x*y); } } class circle implements Area { public float compute(float x,float y) { return(pi*x*y); } } public class InterfaceArea { public static void main(String args[]) { rectangle r=new rectangle(); circle c=new circle(); Area a; a=r; System.out.println("Area of the Rectangle"+a.compute(10,20)); a=c; System.out.println("Area of the circle"+a.compute(10,12)); } } Expt.No.20 Program for Handling Pre-defined Exceptions Aim: To write a program for handling predefined exception Algorithm: Step1: Create a new class exception and package include the file Step2: The operation that are used are try, catch, throw Step3: Two object are created void arithmetic void are exception

Step4: The package included with that file Program: class excep { void arithexception() { int a=10,b=15,R; R=b-a; try { if(R!=0) { System.out.println("The Output is:"+R); } else throw new ArithmeticException("l"); } catch(ArithmeticException e) { System.out.println("The arithmetic exception 'division by zero"is caught"); } } void arrexception() { char c[]={'a'}; try { while(c[10]==3) { System.out.println("no problem"); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("The Array index ob:"+e); } finally { System.out.println("thus the exception is caught"); } } public static void main(String a[]) { excep e1=new excep();

e1.arithexception(); e1.arrexception(); } } Expt.No.21 Program for User-defined Exception Aim: To write a program for user define Exception in java. Algorithm: Step1: Include all the package that are present in java Step2: Create a class, function are involved Step3: The result is printed , until package we can display and data Step4: objects are created for that are defined exception. Program: class IllegalValueAcessException extends Exception { public IllegalValueAcessException(String a) { } } class Usertrial { int val1,val2; public Usertrial(int a,int b) { val1=a; val2=b; } void check()throws IllegalValueAcessException { if((val1<0)||(val2>0)) throw new IllegalValueAcessException("2"); System.out.println("Tha val1 is:"+val1); System.out.println("The value2 is:"+val2); } } class Throwexcep

{ public static void main(String a[]) { Usertrial use=new Usertrial(-1,4); try { use.check(); } catch(IllegalValueAcessException e) { System.out.println("Illegal values are caught"+e); } } } Expt.No.22 Aim: To write a program predefined package in java Algorithm: Step1: Include all the packages that are present in java Step2: Create the class Step3: The value of a & b are get from the user Step4: object are created for predefined package Step5: Function are invoked Program:
package pack; public class Display { public Display() { System.out.println("This is a constructor"); } public String Displaytext() { return"Displayingtext"; } } \\program for userdefined package import pack.Display; public class simple { public static void main(String args[]) { Display disp=new Display(); String str=disp.Displaytext(); System.out.println(str);

Program for creating User-defined Package

System.out.println("Hai"); } }

Expt.No.23 Program for Multi-Threading Aim: To write a program for Multithreading. Algorithm: Step1: To create one class called new thread implement runnable. Step2: Create constructor for the class Step3: Point " child" thread Step4: Assign thread sleep. Step5: Print child thread Step6: Create another class Step7: Print main thread Program:
class NewThread implements Runnable { Thread t; NewThread() { t=new Thread(this,"demo thread"); System.out.println("child thread"+t); t.start(); } public void run() { try { for(int i=5;i>0;i--) { System.out.println("child thread"+i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("childInterrupped"); } System.out.println("exiting child thread"); } } class Threaddemo { public static void main(String a[]) { new NewThread(); try {

for(int i=5;i>0;i--) { System.out.println("mainthread:"+i); Thread.sleep(2000); } } catch(InterruptedException e) { System.out.println("mainthread interrupted"); } System.out.println("mainthreading exiting"); } }

You might also like