Академический Документы
Профессиональный Документы
Культура Документы
MT 6612
Laboratory manual
Roll Number
Register Number
Batch Number
Regulation
R 2013
Class/semester
Academic Year
2015 - 2016
List of Experiments
Signature of
S.No
List of Programs
Page No
Date
1(a)
1(b)
2(a)
the Faculty
Member
03
05
08
2(b)
10
2(c)
13
2(d)
16
3(a)
Unary Operators
18
3(b)
Binary Operators
20
3(c)
Function Overloading
22
4(a)
Inheritance
24
4(b)
Virtual Function
27
4(c)
28
4(d)
Templates
33
5(a)
Sequential Access
35
5(b)
Random Access
36
Ex.No:1(a)
Date:
Marks
Aim:
To write a C++ program to find the factorial of a given number using function with
default argument.
Algorithm:
Step 1: Start the program.
Step 2: Define the variable n.
Step 3: Get the value of n from the user.
Step 4: Call the function fact( ).
Step 5: Print the value of variable f.
Step 6: Stop the process.
Program :
#include<iostream.h>
int fact(int n=5)
{
int f=1;
if(n==0)
return f;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
void main( )
{
int n;
clrscr( );
cout<< ENTER THE NUMBER :<<endl;
cin>>n;
cout<< FACTORIAL VALUE :<<fact(n);
cout<< VALUE OF DEFAULT ARGUMENT :<<fact( );
getch( );
}
Output :
Viva Questions:
1.Define function.
4
2.What is an argument?
3.List out the different types of data types.
4.Define inline function.
5.Define friend function.
6.Difference between default arguments and const arguments.
7.What is virtual function.
8.When do we need to use default arguments in a function?
9.What is the significance of a empty paranthesis in a function declaration?
10.Write the different styles of prorotypes.
Result :
Ex.No: 1(b)
Date:
Aim:
To write a C++ program to swap the given values by using call by value, call by
reference, call by address.
Algorithm:
Step1: Start the process.
Step2: Declare the input variables as a,b.
Step3: Call the swapping function.
Step4: Print the values of a,b .
Step5: Terminate the process.
Call By Value:
#include<iostream.h>
#include<conio.h>
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
}
void main( )
{
int a,b;
clrscr( );
cout<<\n CALL BY VALUE :;
cout<<\n ENTER THE TWO VALUES :;
cin>>a>>b;
swap(a,b);
cout<<\n VALUE OF A AND B AFTER SWAPPING :<<endl;
cout<<a=<<a<<endl<<b=<<b;
getch( );
}
Output:
Call By Reference:
#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y)
{
6
int t;
t=x;
x=y;
y=t;
}
void main( )
{
int a,b;
cout<<\n CALL BY REFERENCE:;
cout<<\n ENTER THE TWO VALUES :;
cin>>a>>b;
swap(a,b);
cout<<\n VALUE OF A AND B AFTER SWAPPING :<<endl;
cout<<a=<<a<<endl<<b=<<b;
getch( );
}
Output:
Call By Address:
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
}
void main( )
{
int a,b;
cout<<\n CALL BY ADDRESS :;
cout<<\n ENTER THE TWO VALUES :;
cin>>a>>b;
swap(&a,&b);
cout<<\n VALUES OF A AND B AFTER SWAPPING :<<endl;
7
cout<<a=<<a<<endl<<b=<<b;
getch( );
}
Output:
Viva Questions:
1. What is call by value?
2. What is call by reference?
3. What is call by address?
4. Difference between call by reference and address.
5. What is the main advantages of passing arguments by reference?
6. What is the most significant advantage that you see in using reference instead of using
pointers?
7. Write the syntax for return by reference.
8. What is formal arguments?
9. what is meant by actual arguments?
10. what is the use of a main() function?
Result :
Ex.No:2(a)
Date:
Aim:
To write a c++ program to implement a class using data members.
Algorithm:
Step1: Start the process.
Step2: Create a class AREA and instantiate the necessary variable.
Step3: Get the radius, length, breadth values.
Step4: Create a main () and perform the object instantiation.
Step5: Display the values.
Step6: Terminate the process.
Program :
#include<iostream.h>
#include<conio.h>
#define pi 3.14
class AREA
{
int area1,area2;
public:
void circle(int r);
void rectangle(int l,int b);
};
void AREA::circle(int r)
{
area1=pi*r*r;
cout<<"AREA OF THE CIRCLE : "<<area1;
}
void AREA::rectangle(int l,int b)
{
area2=l*b;
cout<<"AREA OF THE RECTANGLE : "<<area2;
}
void main()
{
int r,l,b;
clrscr();
AREA JJ;
cout<<"\nEnter the radius of the circle : ";
cin>>r;
JJ.circle(r);
cout<<"\n\nEnter the length & breadth of the rectangle : ";
cin>>l>>b;
JJ.rectangle(l,b);
getch(); }
Output
9
Viva Questions:
1.What is class?
2.What is object?
3.Write the syntax for defining class.
4.How will you create object for a class?
5.How many objects are created for a particular class?
6.what are the class specifications?
7.What is calling function?
8.What is called function?
9.Write the declaration for member function.
10.How will you declare static data member function?
Result:
Ex.No:2(b)
Date:
Aim:
To write a C++ program to calculate employee payroll with class using data members.
Algorithm For Class :
Step 1: Start the program.
Step 2: Creating a class and declare the data members and member
function[getdata(), cal(), display()].
Algorithm For Member Functions:
Getdata():
Step 1: Get the number of employee,name,designation and basic pay.
Cal():
Step 1: Calculate the pf [pf=0.12*bp].
Step 2: Calculate the hra [hra=bp*0.005].
Step 3: Calculate the da [da=bp*0.5].
Step 4: Calculate the gross pay=bp+da+hra+pf.
Step 5: Calculate the net pay=gross pay-pf.
Main Function:
Step 1: Start the process.
Step 2: Create an object e1[10].
Step 3: Call the getdata( ) function.
Step 5: Call the display( ) function.
Step 6: Stop the process
Program :
#include<iostream.h>
#include<conio.h>
int i,n;
class employee
{
int bp,gp,np;
float pf,da,hra;
char name[20],designation[20];
public:
void getdata( );
void cal( );
void display( );
};
void employee::getdata( )
{
cout<<ENTER THE NAME :<<endl;
cin>>name;
cout<<ENTER THE DESIGNATION :<<endl;
cin>>designation;
cout<<ENTER THE BASICPAY :<<endl;
cin>>bp;
11
}
void employee::cal( )
{
pf=0.12*bp;
hra=bp*0.05;
da=bp*0.5;
gp=bp+pf+hra+da;
np=gp-pf;
}
void employee::display( )
{
cout<<name<<\t<<designation<<\t<<bp<<\t<<np<<endl;
}
void main( )
{
employee e1[10];
clrscr( );
cout<<ENTER THE NO. OF EMPLOYEE :<<endl;
cin>>n;
for(i=0;i<n;i++)
{
e1[i].getdata( );
e1[i].cal( );
}
cout<<NAME \t WORK \t BASICPAY \t GROSSPAY \t
NETPAY<<endl;
cout<<**************************************<<endl;
for(i=0;i<n;i++)
{
e1[i].display( );
}
getch( );
}
Output:
Viva Questions:
1.What is local class?
12
Result :
Ex.No: 2(c)
Date:
Aim:
To write a C++ program to create with pointers and constant as data members functions.
Algorithm:
Step 1: Start the program.
Step 2: Create a class person and instantiate the pointers variable.
Step 3: Import an inline function with scope resolution operator to display the string.
Step 4: Using strings function and if statement print the required values.
Step 5: Perform object instantiation for the class to access member function using dot
operator.
Step 6: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
Class person
{
Private:
char*name;
char*address;
char*phone;
Public:
Void init();
Void clear();
Void setname(char const * str);
Void setaddress(char const * str);
Void setphone(char const *str);
char const * getname(void)const;
char const * getaddress(void)const;
char const * getphone(void)const;
};
inline void person::init()
{
name =address=phone=0;
}
inline void person::clear()
{
delete name;
delete address;
delete phone;
}
Void person::setname(char const * str)
{
if(name)
14
delete name;
name=new char[strlen(str)+1];
strcpy(name,str);
}
Void person::setaddress(char const * str)
{
if(address)
delete address;
address =new char[strlen(str)+1];
strcpy(address,str);
}
Void person::setphone(char const * str)
{
if(phone)
delete phone;
phone =new char[strlen(str)+1];
strcpy(phone,str);
}
inline char const * person::getname()const
{
return name;
}
inline char const * person::getaddress()const
{
return address;
}
inline char const * person::getphone()const
{
return phone;
}
Void printperson(person const &p)
{
If(p.getname())
{
Cout<<name:<<p.getname()<<endl;
}
If(p.getaddress())
{
Cout<<address:<<p.getaddress()<<endl;
}
If(p.getphone())
{
Cout<<phone:<<p.getphone()<<endl;
}}
15
Void main()
{
Person p1,p2;
Clrscr();
P1.init();
P2.init();
P1.setname(kumar);
P1.setaddress(Tirupur);
P1.setphone(9791882345);
Printperson(p1);
P2.setname(John);
P2.setaddress(Mettupalayam);
P2.setphone(9865022941);
Printperson(p2);
P1.clear();
P2.clear();
getch();
}
Output:
Viva Questions:
1.How will you declare the pointers?
2.How the pointers will be initialized?
3.Write the syntax for manipulation of pointer.
4.What is dereferencing operator?
5.Define null pointer.
6.Define null address.
7.What are the operations that can be performed using pointers?
8.How will you declare pointers in an array?
9.What is meant by arrays of pointers?
10.What is the use of this pointer?
Result:
Ex.No:2(d)
Date:
Aim:
To write a C++ program to implement classes with static and constant data members
Algorithm:
Step 1: Start the program.
Step 2: Create a class myclass and instantiate the member function as static.
Step 3: Using display() the number of cells made through the object.
Step 4: Perform object instantiation for the class myclass and set appropriate value.
Step 5: print the values.
Step 6: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class myclass
{
Private:
static int count;
int number;
Public:
void set(int num)
{
number=num;
++count;
}
Void show()
{
cout<<\n number of cells made to set() through my object:<<count;
}
};
int myclass::count=0;
void main()
{
myclass obj1,obj2,obj3;
clrscr();
obj1.set(100);
obj1.show( );
obj2.set(250);
obj2.show( );
obj3.set(199);
obj3.show( );
getch();
}
Output:
17
Viva Questions:
1.Write the special characteristics of static member variable.
2.Write the syntax for declaration of static data member.
3.List out the properties of declaring static member function.
4.Write the syntax for accessing static function.
5.Write the syntax for declaring static member function.
6.What is the use of declaring const member function?
7.Write the syntax for declaring friend function.
8.What is the use of friend function?
9.How will you pass objects as function arguments?
10.How will you declare the array of objects?
Result:
Ex.No:3(a)
Date:
Unary Operators
18
Aim :
To write a C++ program for overloading increment and decrement operator as member
functions.
Algorithm :
For Class :
Step1: Start the program.
Step2: Creating a class and declare the data members and member
function[default constructor,getval( ),operator++( )
operator--( )].
For Main( ) :
Step1: Start the process.
Step2: Declare an object id1,id2,id3 and id4 and initialize id1and id2.
Step3: Print the index value before any operation is carried out.
Step4: Print the index values after the incrementing id3 and id4.
Step5: Print the index values after the decrementing id3 and id4.
Step6: Stop the process.
Program :
#include<iostream.h>
#include<conio.h>
class index
{
int value;
public:
index( )
{
value=0;
}
int getval( )
{
return value;
}
index operator++( )
{
return index(++value);
}
index operator++(int)
{
return index(value++);
}
19
index operator--( )
{
return index(--value);
}
index operator--(int)
{
return index(value--);
}
};
void main( )
{
index id1(2),id2(2),id3,id4;
clrscr( );
cout<<\n INDEX1=<<id1.getval( )<<\n
<<INDEX2= <<id2.getval( )<<\n
<<INDEX3=<<id3.getval( )<<\n
<<INDEX4=<<id4.getval( )<<\n;
id3=id1++;
id4=++id2;
cout<<THE INCREMENTED OPERATION :<<endl;
cout<<\n INDEX1=<<id1.getval( )<<\n
<<INDEX2= <<id2.getval( )<<\n
<<INDEX3=<<id3.getval( )<<\n
<<INDEX4=<<id4.getval( )<<\n;
id3=id1--;
id4=--id2;
cout<<THE DECREMENTED OPERATION :<<endl;
cout<<\n INDEX1=<<id1.getval( )<<\n
<<INDEX2= <<id2.getval( )<<\n
<<INDEX3=<<id3.getval( )<<\n
<<INDEX4=<<id4.getval( )<<\n;
getch( );
}
Output
20
Results
Ex.No:3(b)
Date:
Binary Operators
21
Aim :
To write a program to add two complex numbers using binary operator overloading.
Algorithm:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator ()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and
operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
22
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
Output
Ex.No:3(c)
Function Overloading
23
Date:
Aim:
To write a C++ program to find volume of square, cylinder and rectangle using
function overloading.
Algorithm :
Step1: Start the program.
Step2: Define the function to calculate the volume of rectangle.
Step3: Define the function to calculate the volume of cylinder.
Step4: Define the function to calculate the volume of cube.
Step5: Read the side of the cube and call the volume function,
print the result..
Step6: Read the radius and height of cylinder and call volume (a,b)
function, print the result.
Step7: Read length, breadth and height of the rectangle and call the
overloaded function and print the result.
Step8: Stop the program.
Program :
#include<iostream.h>
#include<conio.h>
class vol
{
public:
float volume(float l,float b,float h)
{
return(l*b*h);
}
float volume(float a)
{
return(a*a*a);
}
float volume(float h,float r)
{
return(3.14*r*r*h);
}
};
void main( )
{
vol v;
float l,b,h,a,r;
clrscr( );
cout<<ENTER THE VALUES OF RECTANGLE :<<endl;
cin>>l>>b>>h;
cout<<VOLUME OF RECTANGLE IS :<<v.volume(l,b,h)<<endl;
24
Viva Questions:
1.What is meant by operator overloading?
2.List out the operators that cannot be overloaded in C++.
3.Write the syntax for defining operator overloading.
4.List out the steps used in overloading operators.
5.What is meant by binary operator overloading?
6.How will you overload the unary operators?
7.How will you access the overloaded function?
8.How many arguments that can be used in unary opertor?
9.What is the pupose of using overloading function?
10.What is function overloading?
Result :
Ex.No:4(a)
Date:
Inheritance
25
Aim :
To write a C++ program to maintain students mark list using multilevel inheritance.
Algorithm :
For Class Base :
Step1: Start the process.
Step2: Create a class and declare the member function and data
members[name[20], regno[20], get_data( )].
For Class Derived2 :
Step1: Start the process.
Step2: Create a class and declare the data members and member
function[total, avg, cal( )].
For Main( ) :
Step1: Start the process.
Step2: Declare n,i as integer type.
Step3: Declare an object st[10] for derived2.
Step4: Get the number of students as n and call the functions
get_data( ), get_marks( ) and cal( ) n times.
Step5: Stop the process.
Program :
#include<iostream.h>
#include<conio.h>
class base
{
protected:
char name[20],regno[20];
public:
void get_data( )
{
cout<<"\n ENTER THE NAME :";
cin>>name;
cout<<"\n ENTER THE REGNO :";
cin>>regno;
}
};
class derived1:public base
{
protected:
int m1,m2,m3,m4,m5,m6;
public:
void get_marks( )
{
cout<<"\n ENTER THE MARKS :\n";
cin>>m1>>m2>>m3>>m4>>m5>>m6;
26
}
};
class derived2:public derived1
{
protected:
int total,avg;
public:
void cal( )
{
total=m1+m2+m3+m4+m5+m6;
avg=total/6;
cout<<"\n"<<name<<"\t"<<regno<<"\t"<<total<<"\t"<<avg<<"%";
if(m1>35||m2>35||m3>35||m4>35||m5>35||m6>35)
{
cout<<"\t PASS ";
}
else
{
cout<<"\t FAIL ";
}
}
};
void main( )
{
int n,i;
clrscr( );
derived2 st[10];
cout<<"\n ENTER NUMBER OF STUDENTS :";
cin>>n;
for(i=0;i<n;i++)
{
st[i].get_data( );
st[i].get_marks( );
}
cout<<"\n";
cout<<"\n STUDENTS MARKLIST";
cout<<"\n-----------------------------------";
cout<<"\n NAME \t REGNO \t TOTAL \t AVG \t RESULT";
cout<<"\n*********************************************";
cout<<"\n";
for(i=0;i<n;i++)
{
st[i].cal( );
}
27
getch( );
}
Output :
Viva Questions:
1.What is meant by inheritance?
2.How will you achieve reusability using inheritance?
3.What are the different types of inheritance?
4.Write the syntax for defining derived class.
5.List out the different types of visibility mode.
6.Write the syntax for defining single inheritance.
7.What is the use of declaring the access specifier as protected?
8. Write the syntax for defining multiple inheritance.
9. Write the syntax for defining multilevel inheritance.
10.Write the syntax for defining hierarchical inheritance.
Result :
Ex.No:4(b)
Date:
Virtual Function
28
Aim :
To write a C++ program for implement the virtual function
Algorithm :
Step1: Start the program.
Step2: Create a class and instantiate the necessary variables and functions.
Step3: virtual keyword is used for implementation of overriding function.
Step4: call the super class constructor using sub class and display the values.
Step5: perform object instantiation for the sub class in main function.
Step6:stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<\n display base;
}
virtual void show()
{
cout<<\n show base \n;
}
};
class derived::public base
{
public:
void display()
{
cout<<\n display derived;
}
void show()
{
cout<<\n show derived;
}
};
int main()
{
clrscr();
base b;
derived d;
base*bptr;
cout<<\nbptr points to base \n;
29
bptr=&b;
bptr->display();
bptr->show();
cout<<\n bptr points to derived;
bptr=&d;
bptr->display();
bptr->show();
getch();
return 0;
}
Output:
Ex.No:4(c)
Date:
Aim :
To write a C++ program to implement virtual base class in maintaining the employee
details.
Algorithm :
For Employee Class :
Step1: Start the process.
Step2: Create a class and declare the data members and
member function[name, des, getdata( )]
For Function Getdata( ) :
Step1: Get the name, designation and experience of the employee.
For Class Amt :
Step1: Start the process.
Step2: Create a class and declare the data members and member
function[bp, pf, hra, np, gp, da, get( ), cal( )]
For Get( ) :
Step1: Get the basic pay of the employee.
For Cal( ) :
Step1: Calculate the pf [pf=0.12*bp].
Step2: Calculate the hra [hra=bp*0.05].
Step3: Calculate the da [da=bp*0.5].
Step4: Calculate the grosspay(gp) and netpay(np).
30
{
protected:
int incent;
int sal;
public:
void getval( );
void calc( );
};
class total:public amt,public incent
{
protected:
int totpay;
public:
void tot( )
{
totpay=np+incent;
}
void display( );
};
void employee::getdata( )
{
cout<<"\n ENTER THE EMPLOYEE NAME :";
cin>>name;
cout<<"\n ENTER THE DESIGNATION :";
cin>>des;
cout<<"\n ENTER THE EXPERIENCE :";
cin>>exp;
}
void amt::get( )
{
cout<<"\n ENTER THE BASIC PAY :";
cin>>bp;
}
void amt::cal( )
{
pf=0.12*bp;
hra=bp*0.05;
da=bp*0.5;
gp=bp+pf+hra+da+(exp*50);
np=gp-pf;
}
void incent::getval( )
{
cout<<"\n ENTER THE NUMBER OF PRODUCTS SOLD :";
32
cin>>sal;
}
void incent::calc( )
{
if(sal<=100)
{
incent=sal*1;
}
else if(sal>100&&sal<=200)
{
incent=sal*1.5;
}
else if(sal>200&&sal<=300)
{
incent=sal*2;
}
else
{
incent=sal*2.5;
}
}
void total::display( )
{
cout<<"\n"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<gp<<"\t"<<incent<<"\t"<<totpay;
}
void main( )
{
total d[12];
int i,n;
clrscr( );
cout<<"\n ENTER THE NUMBER OF EMPLOYEE :";
cin>>n;
for(i=0;i<n;i++)
{
d[i].getdata( );
d[i].get( );
d[i].cal( );
d[i].getval( );
d[i].calc( );
d[i].tot( );
}
cout<<"\n NAME \t DESIGNATION \t BASICPAY \t GROSSPAY \t INCENTIVE \t
TOTALPAY";
cout<<"\n*************************************";
33
for(i=0;i<n;i++)
d[i].display( );
getch( );
}
Output :
Viva Questions:
1.What is virtual function?
2.What is pure virtual function?
3.Write the syntax for defining virtual constructor and destructor.
4.Where we are using virtual base class?
5.Write the syntax for declaring multipath inheritance.
6.What is abstract class?
7.Can you able to create an object for abstract class?
8.What is the use of defining constructor and write its syntsx?
9.What is destructor?
10.In which order the constructor will be invoked in inheritance.
Result :
Ex.No:4(d)
Date:
Templates
34
Aim :
To write a C++ program to find the maximum of three integers and three float numbers
using function templates.
Algorithm :
For Function Template :
Step1: Receive three values a, b, c of type T.
Step2: Declare a variable big of type T and assign big=a.
Step3: If b is greater than big, assign big=b.
Step4: If c is greater than big, assign big=c.
For Main( ) :
Step1: Start the program.
Step2: Get the three integer values and call the function template.
Step3: Print the biggest of three integer.
Step4: Get the three float values and call the function template.
Step5: Print the biggest of float values.
Step6: Stop the program.
Program :
#include<iostream.h>
#include<conio.h>
template<class T>
T returnbig(T a,T b,T c)
{
T big;
big=a;
if(b>big)
big=b;
if(c>big)
big=c;
return big;
}
void main( )
{
int a,b,c;
float d,e,f;
clrscr( );
cout<<\n FUNCTION TEMPLATES <<endl;
cout<<ENTER THE THREE INTEGER VALUES :<<endl;
cin>>a>>b>>c;
cout<<BIGGEST OF THREE INTEGER VALUES IS :
<<returnbig(a,b,c);
cout<<\n ENTER THE THREE FLOAT VALUES :<<endl;
cin>>d>>e>>f;
cout<<THE BIGGEST OF THREE FLOAT VALUES IS :
<<returnbig(d,e,f);
35
getch( );
}
Output :
Viva Questions:
1.How will you define generic class?
2.Write the general format of class templates.
3.What is meant by instantiation?
4.How will you declare class template with multiple parameters?
5.Write general format for defining function template.
6. How will you declare function template with multiple parameters?
7.Can we able to overload template function?
8.What is meant by arguments template?
9.How will you declare nontype template arguments?
10.Define template.
Result :
Ex.No:5(a)
Date:
Sequential Access
36
Aim :
To write a C++ program to implement sequential access
Algorithm :
Step1: Start the program
Step2: Get the string values using get line function
Step3: Using for loop, print the string values
Step4:Implement seekg function in order to specify the string value
Step5: Sequential access file is used to access a particular data in the file
Step6:print the output
Step7: stop the program
Program:
#include<fstream.h>
#include<conio.h>
void main( )
{
char c,string[75];
clrscr( );
fstream file(sounder.txt:,ios::in|ios::out);
cout<<enter string:;
cin.getline(string,74);
for(int i=0;string[i];i++)
file.put(string[i]);
file.seekg(0);
cout<<output string:;
while(file)
{
file.get(c);
cout<<c;
}
getch();
}
Output:
Ex.No:5(b)
Date:
Random Access
37
Aim :
To write a C++ program to implement the random access
Algorithm :
Step1: Start the program
Step2: import the preprocessor directive and fstream which is used for file operation
Step3: create the file student.txt contains the entered string
Step4: Read the size of the file by using seekp and seekg functions
Step5: The random file allows to access a specific data without accessing all data items
Step6: Stop the program
Program:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define [READ_SIZE+1];
clrscr();
fstream fstr(mts.dat,ios::binary|ios::in|ios::out);
for(int i=0;i<10;i++)
fstr<<i;
fstr.seekp(2);
fstr<<hello;
fstr.seekg(4);
fstr.read(reader,READ_SIZE);
reader[READ_SIZE]=0;
cout<<reader<<endl;
getch( );
}
Output:
Viva Questions:
1.What is meant by file?
38
Result:
39