Sie sind auf Seite 1von 35

1

M.A.M SCHOOL OF ENGINEERING


Siruganur , Tiruchirappalli.










Department of Computer Science and Engineering
CS1313-Object Oriented Programming Laboratory





LAB MANUAL








M.A.M.SCHOOL OF ENGINEERING
Siruganur Tiruchirappalli.


2





COMPUTER SCIENCE ENGINEERING LABORATORY
CERTIFICATE

Certified that this is the bonafide record of practicals done by
Selvan / Selvi. _________________________________
Reg.No.______________ of First Year B.E., during the
academic Year 2008 2009.

Station:

Date: HOD Staff in - Charge



Submitted for the university practical Examination held on


Date: _________ Internal Examiner External Examiner
















3

CS1313 OBJECT ORIENTED PROGRAMMING LABORATORY
( Common to EEE, EIE and ICE)
L T P C
0 0 3 2



1. Programs Using Functions
- Functions with Default Arguments
- Implementation of Call by Value, Call by Address

2. Simple Classes for understanding objects, member functions and Constructors
- Classes with Primitive Data Members
- Classes with Arrays as Data Members
- Classes with Pointers as Data Members String Class
- Classes with Constant Data Members
- Classes with Static Member Functions

3. Compile Time Polymorphism
- Operator Overloading including Unary and Binary Operators
- Function Overloading

4. Runtime Polymorphism
- Inheritance
- Virtual Functions
- Virtual Base Classes
- Templates

5. File Handling
- Sequential Access
- Random Access
Total: 45















4

INDEX


EXPT
NO

DATE

NAME OF THE EXPERIMENT

PAGE
NO

MARKS
OBTAINED

SIGNATURE
1

Function with default argument

2A Call By Value


2B Call By Reference


2C Call By Address


3A Classes and Objects


3B Creating marklist using array


3C String Manipulation


4A Static Member function


4B Constant Member Function


5A Unary Operator overloading


5B Binary Operator overloading


6 Function overloading


7A Single Inheritance


7B Multiple Inheritance


8A virtual function


8B virtual base class


9A Function Template


9B Class Template


10A Sequential File Access


10B Random File Access





5


1.Function with default argument
Ex No:1
AIM:
To write a c++ program for function with default arguments
ALGORITHM:

1. Start the program.
2. Declare all the header file.
3. Declare printline function and set default values for arguments.
4. Define the printline function.
5. printline function will print character n times.
6. call the function within main().
7. stop the execution.

PROGRAM:

#include<iostream.h>
#include<conio.h>
void printline(char='_',int=70);
void main()
{
clrscr();
printline();
printline('!');
printline('*',40);
printline('r',55);
getch();
}
void printline(char ch,int count)
{
int i;
cout<<endl;
for(i=0;i<count;i++)
cout<<ch;
}



OUTPUT:
______________________________________________________________________
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
****************************************
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

RESULT:

Thus the C++ program for function with default arguments is written and executed.

6




Ex No:2A

Call By Value

AIM:
To write a c++ program to illustrate the concept of call by value.

ALGORITHM:
1. Declare necessary variables.
2. Get the values for swapping.
3. Define a function which swaps value that are passed as arguments to it using temporary
variable.
4. Invoke that function by passing the given values
PROGRAM:

#include<iostream.h>
#include<conio.h>
void swap(int x,int y)
{
int t;
cout<<"value of x&y before exchange:"<<x<<" "<<y<<endl;
t=x;
x=y;
y=t;
cout<<" value of x & y after exchange:"<<x<<" "<<y<<endl;
}
void main()
{
clrscr();
int a,b;
cout<<" enter two integers<a,b>";
cin>>a>>b;
swap(a,b);
cout<<"value of a and b on swap (a,b) in main():"<<a<<" "<<b;
getch();
}
OUTPUT:
enter two integers<a,b>1 2
value of x&y before exchange:1 2
value of x & y after exchange:2 1
value of a and b on swap (a,b) in main():1 2

RESULT:

Thus the C++ program for call by value executed and output is verified.

7




Ex No:2B
Call By Reference

AIM:
To write a c++ program to illustrate the concept of call by reference.

ALGORITHM:
1. Declare necessary variables.
2. Get the values for swapping.
3. Define a function which swaps value using their reference which are passed as
arguments to it .
4. Invoke that function with or without arguments.
PROGRAM:
#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
}
void main()
{
clrscr();
int a,b;
cout<<" enter two integers<a,b>";
cin>>a>>b;
swap(a,b);
cout<<"value of a & b on swap(a,b) in main():"<<a<<" "<<b;
getch();
}

\\output\\

enter two integers<a,b>8 7
value of a & b on swap(a,b) in main():7 8



RESULT:

Thus the C++ program for call by reference executed and output is verified.


8






Ex No:2C

Call By Address

AIM:
To write a c++ program to illustrate the concept of call by address.

ALGORITHM:
1. Declare necessary variables.
2. Get the values for swapping.
3. Define a function which swaps address by using temporary variable t.
4. Invoke that function by passing address of variable.

PROGRAM:
#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<<"enter two integers:";
cin>>a>>b;
swap(&a,&b);
cout<<"value of a and b on swap(a,b):"<<a<<b;
getch();
}
OUTPUT:
enter two integers:3 4
value of a and b on swap(a,b):4 3

RESULT:

Thus the C++ program for call by address executed and output is verified.




9

Ex No:3A

Classes and Objects

AIM:
To write a c++ program to illustrate the concept of Classes and objects.

ALGORITHM:
1. Declare all the header files.
2. Declare the class and define all the data types.
3. Define sub function of getdata( ) and putdata( ).
4. Define main() function, declare the values for getdata( ).
5. Obtain Output and display using putdata( ).
6. Stop the execution.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class item
{
int number;
float cost;
public:
void getdata (int a,float b);
void putdata()
{
cout<<"num:"<<number;
cout<<"\n cost:"<<cost;
}};
void item::getdata(int a,float b)
{
number=a;
cost=b;}
void main()
{
clrscr();
item x;
cout<<"\n x";
x.getdata(24,100.20);
x.putdata();
getch();
}
OUTPUT:

xnum:24
cost:100.199997

RESULT:
Thus the C++ program using classes and object was executed and output verified.

10

Ex No:3B
Creating marklist using array

AIM:
To create a mark list using arrays in c++ programming language.

ALGORITHM:
1. Declare all the header files.
2. Declare the class and define the data types with arrays.
3. Define the functions and get the details.
4. Create student mark list.
PROGRAM:

#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int i,m;
int a[5],tot;
float avg;
public:
void get();
void cal();
};
void student::get()
{
cout<<"give the name";
cin>>name;
cout<<"\n give no. of subjects";
cin>>m;cout<<"\n enter the marks:";
for(i=1;i<=m;i++)
cin>>a[i];
}
void student::cal()
{
int tot=0;
for(i=1;i<=m;i++)
tot=a[i]+tot;
avg=tot/m;
cout<<"\n tot="<<tot<<"\n avg="<<avg;
}
void main()
{
student s;
clrscr();
s.get();
s.cal();

11

getch();
}
OUTPUT:
give the nameisac

give no. of subjects5

enter the marks:89
87
84
84
90

tot=434
avg=86
RESULT:

Thus the C++ program for creating mark list using arrays was executed and output verified.

Ex No:3C

String Manipulation
AIM:
To write a c++ program to perform operation on string class.

ALGORITHM:
1. Create a string class with string as its data members and getdata(), display(),
string length, string copy as its member functions.
2. getdata() member function is used to get the values of the string.
3. display() member function is used to display the string.
4. string length() member function calculate the length of the string by inspecting its
characters one by one till NULL(\0).
5. string copy() member function copies the string value which is passed as an
argument to the invoking object.
PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<string.h>
class str
{
private:
char str[40];
char*n;
public:
void get();
void disp();
int strlen();

12

void strcpy(char*n);
};
void str::get()
{
cout<<"enter the name:";
cin>>str;
}
void str::disp()
{
cout<<"the name is:"<<str<<"\n\n";
}
int str::strlen()
{
int count=0;
for(int i=0;str[i]!='\0';i++)
count=count+1;
return count;
}
void str::strcpy(char *n)
{
for(int i=0;n[i]!='\0';i++)
str[i]=n[i];
str[i]='\0';
cout<<str;
}
void main()
{
char*n;
str s;
clrscr();
s.get();
s.disp();
int strlen=s.strlen();
cout<<"length:"<<strlen<<"\n\n";
n=new char[25];cout<<"enter the name:"<<"\n\n";
cin>>n;
cout<<"the copied string:";
s.strcpy(n);
getch();
}
OUTPUT:
enter the name:ashok
the name is:ashok
length:5
enter the name:

hakeem
the copied string:hakeem

13


RESULT:

Thus the C++ program for string manipulation was executed and output verified.


Ex No:4A
Static Member function

AIM:
To write a c++ program to implement static member function.

ALGORITHM:
1. Create a class with static data member and static member function.
2. Static member function only accesses the static data members.
3. Static member function displays the static data member (path of the directory).
4. Invoke that function with object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class directory
{
public:
static char path[];
static void spath(char const*newpath);
};
void directory::spath(char const*newpath)
{ strcpy(path,newpath);}
char directory::path[199]="/user/ashok";
void main()
{
clrscr();
cout<<"path:"<<directory::path<<endl;
directory::spath( "/user");
cout<<"path"<<directory::path<<endl;
directory dir;
dir.spath("\etc");
cout<<"path:"<<dir.path;
getch();
}
OUTPUT:
path:/user/ashok
path/user
path:etc
RESULT:

Thus the C++ program using ststic member function has been executed and output verified.

14


Ex No:4B
Constant Member Function

AIM:
To write a c++ program to display the details of a person using constant member function.

ALGORITHM:
1. Create a class person with name, address and phone as pointer variable in the
private section and constant function to get and display the details.
2. Gets the name of the person using constant function.
3. Gets the address of the person using get address function.
4. Get the phone of the person using get phone function.
5. Print the details using print function.
6. Invoke that function with creating object.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
char *name;
char *add;
char *ph;
public:
void init();
void clear();
void sname(char const *str);
void sphone(char const *str);
void sadd(char const *str);
char const *gname(void)const;
char const *gadd(void)const;
char const *gphone(void)const;
};
inline void person::init()
{
name=add=ph=0;
}
inline void person::clear()
{
delete name;
delete add;
delete ph;
}
void person ::sname(char const *str)
{
if (name)

15

delete name;
name=new char[strlen (str)+1];
strcpy(name,str);
}
void person::sadd(char const *str)
{
if(add)
delete add;
add=new char[strlen(str)+1];
strcpy(add,str);
}
void person::sphone(char const *str)
{
if(ph)
delete add;
ph=new char[strlen(str)+1];
strcpy(ph,str);
}
inline char const *person::gphone()const
{return ph;
}
inline char const *person::gadd()const
{return add;
}
inline char const *person::gname()const
{
return name;
}
void print(person const &p)
{
if(p.gname())
cout<<"\n\t name:"<<p.gname()<<endl;
if(p.gadd())
cout<<"\n\t address:"<<p.gadd()<<endl;
if(p.gphone())
cout<<"\n\t ph:no:"<<p.gphone()<<endl;
}
void main()
{
person p1,p2;
clrscr();
p1.init();
p2.init();
p1.sname("isac");
p1.sadd("theni");
p1.sphone("923942090");
print(p1);
p2.sname("newton");

16

p2.sadd("madurai");
p2.sphone("9239232090");
print(p2);
p1.clear();
p2.clear();
getch();
}
OUTPUT:

name:isac

address:theni

ph:no:923942090

name:newton

address:madurai

ph:no:9239232090
RESULT:

Thus the program to implement using contant member function was executed and output verified.

Ex No:5A
Unary Operator overloading

AIM:
To write a c++ program to illustrate the concept of unary operator overloading.

ALGORITHM:
1. Create a class space which has the data member a, b, c.
2. Declare the operator function by void operator-( ).
3. Define operator function which is used to change the sign of data members.
4. Invoke that operator function inside the main().
PROGRAM:

#include<iostream.h>
#include<conio.h>
class s
{
int a,b,c;
public:
void getdata(int x,int y, int z);
void display(void);
void operator-();
};
void s::getdata(int x,int y,int z)

17

{
a=x;b=y;c=z;
}
void s::display()
{cout<<a<<"\t";
cout<<b<<"\t";
cout<<c<<"\t";
}
void s::operator-()
{
a=-a;
b=-b;
c=-c;
}
void main()
{
s a1;
clrscr();
a1.getdata(10,50,80);
a1.display();
-a1;
a1.display();
getch();
}

OUTPUT:
10 50 80
-10 -50 -80

RESULT:

Thus the C++ program to implement unary operator overloading was executed and output verified.


Binary Operator overloading
Ex No:5B

AIM:
To write a c++ program to illustrate the concept of Binary operator overloading.

ALGORITHM:
1. Create a class num which has the data member a, b, c,d.
2. Declare the operator function by num operator+(num).
3. Define operator function which is used to add two objects
4. Display the result of the object.
PROGRAM:

#include<iostream.h>

18

#include<conio.h>
class num
{
private:
int a,b,c,d;
public:
void input (void);
void show(void);
num operator+(num);
};
void num ::input()
{
cout<<" enter the values for a,b,c,d";
cin>>a>>b>>c>>d;
}
void num::show()
{
cout<<" \tA="<<a<<"\tB="<<b<<"\tC="<<c<<"\tD="<<d<<"\n";
}
num num::operator+(num t)
{
num temp;
temp.a=a+t.a;
temp.b=b+t.b;
temp.c=c+t.c;
temp.d=d+t.d;
return(temp);
}
void main()
{
clrscr();
num x,y,z;
cout<<"\n object x";
x.input();
cout<<"\n object y ";
y.input();
z=x+y;
cout<<"\n x:";
x.show();
cout<<"y=";
y.show();
cout<<"\z:";
z.show();
getch();
}
\\output\\
object x enter the values for a,b,c,d: 5 6 9 8


19

object y enter the values for a,b,c,d: 5 6 4 9

x: A=5 B=6 C=9 D=8
y: A=5 B=6 C=4 D=9
z: A=10 B=12 C=13 D=17

RESULT:

Thus the C++ program to implement Binary operator overloading was executed and output verified.

Ex No:6
Function overloading

AIM:
To write a c++ program to illustrate the concept of function overloading.

ALGORITHM:
1. Declare necessary variables.
2. Create a class with add(int,int) ,add(float,float) as member functions and
necessary variable.
3. add(int,int) is used to add two integer values.
4. add(float,float) is used to add two float values.
5. Using object call the required function with corresponding input.
6. Display the output.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class fover
{
public:
int a,b,c;
float x,y,z;
fover()
{
cout<<" object is created:";
}
void add(int a,int b)
{
c=a+b;
cout<<"\n the addtion values:" <<c;
}
void add(float x,float y)
{
z=x+y;

cout<<"\n the addtion values"<<z;

20

}
~fover()
{
cout<<"\n the object is deleted";
}
};
void main()
{
int a,b;
float x,y;
clrscr();
fover f;
cout<<"\n enter the integer value";
cin>>a>>b;
f.add(a,b);
cout<<"\n enter the float values:";
cin >>x>>y;
f.add(x,y);
getch();
}

OUTPUT:
object is created:
enter the integer value3 4

the addtion values:7
enter the float values:2.4
2.6

the addtion values5
the object is deleted
RESULT:

Thus the C++ program using function overloading was executed and output verified.

Ex No:7A

Multiplication of Postive numbers using Single Inheritance

AIM:
To write a c++ program tomultiply the positive numbers using single inheritance.

ALGORITHM:
1. Define a Nase class B
a. Class B has one private data member ,one public data member and three
member functions.
2. Define Derived class D.All the Properties of B is privately inherited.
3. Create a derived class object.

21

4. Access the base class data members .
5. Do the multiplication.
6. And display the results.




DIAGRAM:
B



D

PROGRAM:
#include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
void getab();
int geta(void);
void showall(void);
};
class D:private B
{
int c;
public:
void mul(void);
void display(void);
};
void B::getab()
{
cout<<"\n enter the values for a&b:";
cin>>a>>b;
}
int B::geta()
{
return a;
}
void B::showall()
{
cout<<"a="<<a<<"\n";
}
void D::mul()
{

22

getab();
c=b*geta();
}
void D::display()
{
showall();
cout<<"b="<<b<<"\n"<<"c="<<c<<"\n";
}
void main()
{
clrscr();
D d;
d.mul();
d.display();
getch();
}



OUTPUT:
enter the values for a&b:3
4
a=3
b=4
c=12
RESULT:

Thus the C++ program to implement Single inheritance was executed and output verified.


Ex No:7B

Employee details using Multiple Inheritance
AIM:
To write a c++ program using multiple inheritance for collecting employee details.



ALGORITHM:
1. Define a person class .
2. Define a Employee class.
3. Inherit all the properties of employee, person class into manager class,in public
mode.
4. Display Manager information.

DIAGRAM:



23

EMPLOYEE PERSON






MANAGER


PROGRAM:

#include<iostream.h>
#include<conio.h>
class person
{
int age,empno;
char name[15];
public:
void getdata()
{
cout<<"\n enter the name:";
cin>>name;
cout<<"\n enter the age:";
cin>>age;
}
void display()
{
cout<<"\n Age:"<<age;
cout<<"\n Name:"<<name;
}};
class employee
{
public:
int empno;
float salary;
void get()
{
cout<<"empno:"<<endl;
cin>>empno;
cout<<"salary:";
cin>>salary;
}
void disp()
{
cout<<"empno:"<<empno<<endl;
cout<<"salary:"<<salary<<endl;
}};

24

class manager:public person,public employee
{
char position[26];
public:
void read()
{
get();
getdata();
cout<<"\n enter the position:"<<endl;
cin>>position;
}
void write()
{
disp();
display();
cout<<"\n position:"<<position;
}};
void main()
{
clrscr();
manager m;
m.read();
m.write();
getch();
}


\\output\\

empno:
23
salary:2500

enter the name:kannan

enter the age:26

enter the position:
supervisor
empno:23
salary:2500

Age:26
Name:kannan
position:supervisor
RESULT:

Thus the C++ program to implement Multiple inheritance was executed and output verified.

25



Ex No:8A

Calculation of area of shapes using virtual function
AIM:
To write a c++ program for calculation of area of shapes using virtual functions.



ALGORITHM:
1. Define a shape class .
2. Define a virtual function show().
3. Define Rectangle class and inherit the base class.
4. Create a pointer and object for base class.
5. Display the rectangle and triangle information.
PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<string.h>
template<class t>
t max(t a,t b)
{
if (a>b)
return a;
else
return b;
}
void main()
{
clrscr();
char ch1,ch2,ch;
cout<<"enter two characters(ch1,ch2):";
cin>>ch1>>ch2;
ch=max(ch1,ch2);
cout<<"max(ch1,ch2):"<<ch<<endl;
int a,b,c;
cout<<"enter 2 integers";
cin>>a>>b;
c=max(a,b);
cout<<"max(a,b):"<<c<<endl;
float f1,f2,f3;
cout<<"enter 2 floats<f1,f2>:";
cin>>f1>>f2;
f3=max(f1,f2);
cout<<"max(f1,f2):"<<f3<<endl;
getch();

26

}
OUTPUT:
enter two characters(ch1,ch2):a s
max(ch1,ch2):s
enter 2 integers2 4
max(a,b):4
enter 2 floats<f1,f2>:2.6 4.5
max(f1,f2):4.5
RESULT:

Thus the C++ program to implement virtual function was executed and output verified.



Ex No:8B
Student marklist using virtual base class

AIM:
To write a c++ program for a student mark list processing using virtual base class.



ALGORITHM:
1. Define a student class and make this as virtual.
2. Define marks class and Inherit the student class virtually.
3. Define sports class and inherit properties of student virtually.
4. Define result class and inherit marks, sports into result.
5. Display student information with marks.


DIAGRAM:


STUDENT



TEST SPORTS


RESULT

PROGRAM:

#include<iostream.h>
#include<conio.h>
class student
{

27

public:
char name[10];
int rollno;
void gets()
{
cout<<"student name:\t";
cin>>name;
cout<<"enter student roll no:\t";
cin>>rollno;
}};
class marks:virtual public student
{
public:
int m1,m2,m3;
void details()
{
cout<<"\n enter the 3 subjects marks:\t";
cin>>m1>>m2>>m3;
}};
class sportsmarks:virtual public student
{
public:
int m4;
void puts()
{
cout<<"enter the sports marks:\t";
cin>>m4;
}};
class result:virtual public marks,virtual public sportsmarks
{
public:
int tot;
float avg;
void display()
{
tot=m1+m2+m3+m4;
avg=tot/4;
}
void demo()
{
cout<<"\n m1="<<m1<<"m2="<<m2<<"m3="<<m3<<"m4="<<m4;
cout<<" the sports marks";
cout<<"total=\t"<<tot;
cout<<"\n avg is\t"<<avg;
}};
void main()
{
clrscr();

28

result r;
r.gets();
r.details();
r.puts();
r.display();
r.demo();
getch();
}

//output//

student name: isac
enter student roll no: 10

enter the 3 subjects marks: 78
78
82
enter the sports marks: 65

m1=78m2=78m3=82m4=65
the sports markstotal= 303
avg is 75


RESULT:

Thus the C++ program to implement virtual base class was executed and output verified.


Ex No:9A

Function Template

AIM:
To write a c++ program using function template to find the maximum of two datas.



ALGORITHM:
1. Create the function template, to find the maximum of two datas.
2. Get different data type values like integer, float and character.
3. Find maximum of those data using template function.
4. Finally print the result.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>

29

template<class t>
t max(t a,t b)
{
if (a>b)
return a;
else
return b;
}
void main()
{
clrscr();
char ch1,ch2,ch;
cout<<"enter two characters(ch1,ch2):";
cin>>ch1>>ch2;
ch=max(ch1,ch2);
cout<<"max(ch1,ch2):"<<ch<<endl;
int a,b,c;
cout<<"enter 2 integers";
cin>>a>>b;
c=max(a,b);
cout<<"max(a,b):"<<c<<endl;
float f1,f2,f3;
cout<<"enter 2 floats<f1,f2>:";
cin>>f1>>f2;
f3=max(f1,f2);
cout<<"max(f1,f2):"<<f3<<endl;
getch();
}

OUTPUT:
enter two characters(ch1,ch2):a s
max(ch1,ch2):s
enter 2 integers2 4
max(a,b):4
enter 2 floats<f1,f2>:2.6 4.5
max(f1,f2):4.5
RESULT:

Thus the C++ program to implement Function Template was executed and output verified.


Ex No:9B
Class Template

AIM:
To write a c++ program using class template to find the greatest of the given two datas.



30


ALGORITHM:
1. Create the class template, to find the greatest of two datas.
2. Get different data type values like integer, float and character.
3. Use a function to find the maximum of two values for both int, float .
4. Find maximum of those data using template function.

5. Finally print the result.

PROGRAM
#include<iostream.h>
#include<conio.h>
#include<string.h>
template<class t>
class demo
{
public:
int i;
t a,b;
void gets()
{
cout<<"\n enter 2 integers:";
cin>>a>>b;
}
void gets1()
{
cout<<"\n enter 2 floats:";
cin>>a>>b;
}
void operation()
{
if(a>b)
cout<<"\n a is greater:"<<a;
else
cout<<"\n b is greater:"<<b;
}
void compare()
{
char a1[10],b1[10];
cout<<"\n enter 2 strings:";
cin>>a1>>b1;
if(strcmp(a1,b1)>0)
{
cout<<"a1 has the higest length"<<a1;
}
else
{
cout<<"b1 has the highest length"<<b1;

31

}}};
void main()
{
clrscr();
demo<int>d1;
d1.gets();
d1.operation();
demo<float>d2;
d2.gets1();
d2.operation();
demo<char>d3;
d3.compare();
getch();
}
OUTPUT:

enter 2 integers:2 3

b is greater:3
enter 2 floats:5 4

a is greater:5
enter 2 strings:

5 6
b1 has the highest length6

RESULT:

Thus the C++ program to implement class template was executed and output verified.


Ex No:10 A


Sequential File Access

AIM:
To write a c++ program for creating student data using sequential file access.


ALGORITHM:
1. Define student class and create the function get and show.
2. Using ofstream and ifstream get and display the file information.
3. Invoke function using sequential file access.

PROGRAM
#include<iostream.h>

32

#include<conio.h>
#include<fstream.h>
class student
{
protected:
char name[10];
int rollno;
public:
void get()
{
cout<<"\n enter the name:";
cin>>name;
cout<<"\n enter roll no:";
cin>>rollno;
}
void show()
{
cout<<"\n name:"<<name;
cout<<"\n roll no:"<<rollno;
}
};
void main()
{
char ch;
student s;
ofstream out;
clrscr();
out.open("file1.txt");
do
{
cout<<"\n enter student data";
s.get();
out.write((char*)&s,sizeof(s));
cout<<"\n enter another student data(y/n)?";
cin>>ch;
}while(ch=='y');
out.close();
ifstream in;
in.open("file1.txt");
in.seekg(0);
in.read((char*)&s,sizeof(s));
while(!in.eof())
{
cout<<"\n student:";
s.show();
in.read((char*)&s,sizeof(s));
}
getch();

33

}

OUTPUT:

enter student data
enter the name:ashok

enter roll no:06

enter another student data(y/n)?y

enter student data
enter the name:hakeem

enter roll no:11

enter another student data(y/n)?n

student:
name:ashok
roll no:6
student:
name:hakeem
roll no:11



RESULT:

Thus the C++ program to create student data using sequential file access was executed and output
verified.


Ex No:10 B
Random File Access


AIM:
To write a c++ program for creating student data using random file access.


ALGORITHM:
1. Define student class and create the function get and show.
2. Using operator overloading function the stream operation for displaying the file
information is performed.
3. 3.Invoke function using random file access.


34

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
protected:
char name[10];
int rollno;
public:
void get()
{
cout<<"\n enter the name:";
cin>>name;
cout<<"\n enter roll no:";
cin>>rollno;
}
void show()
{
cout<<"\n name:"<<name;
cout<<"\n roll no:"<<rollno;
}
};
void main()
{
char ch;
student s;
ofstream out;
clrscr();
out.open("file2.txt");
do
{
cout<<"\n enter student data";
s.get();
out.write((char*)&s,sizeof(s));
cout<<"\n enter another student data(y/n)?";
cin>>ch;
}while(ch=='y');
out.close();
ifstream in;
in.open("file2.txt");
in.seekg(0,ios::end);
int endpos=in.tellg();
int n=endpos/sizeof(student);
cout<<"there are"<<n<<"person in file"<<endl;
cout<<"\n enter person no:";
cin>>n;

35

int pos=(n-1)*sizeof(student);
in.seekg(pos);
in.read((char*)&s,sizeof(s));
s.show();
getch();
}
OUTPUT:

enter student data
enter the name:ashok

enter roll no:5

enter another student data(y/n)?y

enter student data
enter the name:hakeem

enter roll no:11

enter another student data(y/n)?n
there are2person in file

enter person no:2

name:hakeem
roll no:11

RESULT:

Thus the C++ program to create student data using Randoml file access was executed and output
verified.

Das könnte Ihnen auch gefallen