Practical File
Practical File
Practical File
file
Semester: 3rd
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Syllabus
1.[Classes and Objects]Write a program that uses a class where the member functions are
defined inside a class.
2.[Classes and Objects]Write a program that uses a class where the member functions are
defined outside a class.
3.[Classes and Objects]Write a program to demonstrate the use of static data members.
4.[Classes and Objects]Write a program to demonstrate the use of const data members.
5.[Constructors and Destructors]Write a program to demonstrate the use of zero argument and
parameterized constructors.
22.[File Handling]Write a program to copy the contents of a file to another file byte by byte. The
name of the source file and destination file should be taken as command-line arguments,
23.[File Handling]Write a program to demonstrate the reading and writing of mixed type of data.
List of Practical
*Learning Beyond Syllabus Write a program Concept of Abstraction, Data Hiding, and
Encapsulation.
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 1
AIM: Write a program that uses a class where the member functions are defined inside a
class.
#include <iostream.h>
#include<conio.h>
class book
{
Private:
int bookno;
char bname[30];
char auname[30];
float bprice;
public:
void getdata()
{
cout<<”enter the details”<<endl;
cout<<”enter the book no”;
cin>>bookno;
cout<<”enter the book name”;
cin>>bname;
cout<<”enter author name”;
cin>>auname;
cout<<”enter the book price”;
cin>>bprice;
}
void display()
{
cout<<”enter the record”<<endl;
cout<<”book no”<<book no<<endl;
cout<<”book name”<<bname<<endl;
cout<<”author name”<<auname<<endl;
cout<<”book price”<<bprice<<endl;
}
};
void main()
{
class book obj;
clrscr();
obj.getdata();
obj.display();
getch();
}
Output:
Experiment 2
AIM: Write a program that uses a class where the member functions are defined outside a
class.
#include <iostream.h>
#include<conio.h>
class book
{
private:
int bookno;
char bname[30];
char auname[30];
float bprice;
public:
void getdata();
void display();
};
Output:
Experiment 3
AIM: Write a program to demonstrate the use of static data members.
#include <iostream.h>
#include<conio.h>
class item
{
private:
public:
void in(int x)
{
n=x;
count++;
}
void displaycount()
{
cout<<”count=”;
cout<<count<<endl;
}
};
int item::count;
void main()
{ item p1,p2;
clrscr();
p1.displaycount();
p2.displaycount();
p1.in(7);
p2.in(8);
cout<<”after input value”<<endl;
p1.displaycount();
p2.displaycount();
getch();
}
Output:
count=0
count=0
after input value
count=1
count=2
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 4
#include <iostream.h>
#include<conio.h>
class date
{
private:
Output:
1/10/2013
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 5
AIM: Write a program to demonstrate the use of zero argument and parameterized
constructors.
#include <iostream.h>
#include<conio.h>
class demo
{
private:
int m,n;
public:
void display()
{
cout<<” The values are”;
cout<<m<<endl<<n;
}
};
void main()
{
Demo obj (5,6);
obj.display();
}
Output:
Experiment 6
AIM: Write a program to demonstrate the use of dynamic constructor.
#include <iostream.h>
#include<conio.h>
class dyncons
{
private:
int *p;
public:
dyncons()
{
p=new int;
*p=10;
}
dyncons(int v)
{
p= new int;
*p=v;
}
int dis()
{
return(*p);
}};
void main()
{
clrscr();
dyncons o,o1(9);
cout<<”the value of object o’s p is:”;
cout<<o.dis();
cout<<”\n the value of object of o1’s p is:”<<o1.dis();
getch();
}
Output:
Experiment 7
AIM: Write a program to demonstrate the use of explicit constructor.
#include <iostream.h>
#include<conio.h>
class explicit
{
Int data;
public:
int main()
{
explicit a1=37;
return 0;
getch();
}
Output:
A::constructor………..
Value of data:=37
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 8
AIM: Write a program to demonstrate the use of initializer list.
#include <iostream.h>
#include<conio.h>
class point
private:
int x,y;
public:
int main()
clrscr();
point t1(10,15);
cout<<”x=”<<ti.getx()<<”,” ;
cout<<”y=”<<t1.gety();
getch();
return 0;
Output:
x=3
y =27
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 9
AIM: Write a program to demonstrate the overloading of increment and decrement
operators.
#include<iostream>
#include<conio.h>
class overloading
{
int value;
public:
int main()
{
overloading obj1,obj2,result;
int a,b;
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
obj1.setValue(a);
obj2.setValue(b);
result = obj1+obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
getch();
return 0;
}
Output:
Enter the value of Complex Numbers a,b:10
5
Input Values:
10
5
Result:15
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 10
AIM:Write a program to demonstrate the overloading of binary arithmetic operators.
#include<iostream>
#include<conio.h>
class airthmatic
{
float n;
public:
void get()
{
cout<”\n enter number:\n”;
cin>>n;
}
arithmetic operator +(airthmatic &a)
{
aritjmatic t;
t.n=n+a.n;
return t;
}
arithmetic operator -(airthmatic &a)
{
aritjmatic t;
t.n=n-a.n;
return t;
}
arithmetic operator *(airthmatic &a)
{
aritjmatic t;
t.n=n*a.n;
return t;
}
arithmetic operator /(airthmatic &a)
{
aritjmatic t;
t.n=n/a.n;
return t;
}
void display()
{
cout<<n;
}
};
void main()
{
arithmetic a1,a2,a3;
clrscr();
a1.get();
a2.get();
a3 = a1+a2;
cout<<”\n addition of two number:”;
a3.display();
a3 = a1-a2;
cout<<”\n subtraction of two number:”;
a3.display();
a3 = a1*a2;
cout<<”\n multiplication of two number:”;
a3.display();
a3 = a1/a2;
cout<<”\n division of two number:”;
a3.display();
getch();
}
Output:
Enter number 12
Enter number 3
Addition of two number : 15
Subtraction of two number : 9
Multiplication of two number : 36
Division of two number : 4
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 11
AIM: Write a program to demonstrate the overloading of memory management operators.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
class op
{
public:
void *operator new(size_tsize,char const*file, int line);
void operator delete(void *p);
}
void *op::operator new (size_tsize,char const*file, int line)
{
void *p=malloc(size);
cout<<”\n new called:”<<file<<”\n line”<<line;
cout<<”\n size”<<size<<”\n p:”<<p<<endl;
return p;
}
void op::operator delete (void *p)
{
cout<<”\n deletecalled:”<<p<<endl;
free(p);
}
void main()
{
clrscr();
op *x= new(--file,--,--Line--)op;
delete x;
getch();
}
Output:
Experiment 12
AIM: Write a program to demonstrate the typecasting of basic type to class type.
#include<iostream>
#include<conio.h>
class distance
{
int feet,inch;
public:
distance()
{
m=m*100;
m=m*393700787;
inch=int(m)%12;
feet= m/12;
}
operator float()
{
float m;
m=((feet*12)+inch)*2.54;
m=m/100;
return(m);
}
void putdata()
{
cout<<feet<<”feet”;
cout<<inch<<”inch”;
}
};
void main()
{
clrscr();
float meter;
cout<<”\n enter the length in meter:”;
cin>>meter;
distance d1;
d1=meter;
cout<<”\n basic to class conversion”;
d1.putdata();
getch();
}
Output:
Experiment 13
AIM: Write a program to demonstrate the typecasting of class type to basic type.
#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inch;
public:
distance()
{
feet=0;
inch=0;
}
void getdata()
{
cout<<"\nEnter the feet and inch ";
cin>>feet>>inch;
}
operator float()
{
float m;
m=((feet*12)+inch)*2.54;
m=m/100;
return(m);
}
};
void main()
{
clrscr();
distance d2;
d2.getdata();
float meter=d2;
cout<<"\nClass to Basic conversion\nlength= "<<meter;
getch();
}
Output:
Experiment 14
AIM: Write a program to demonstrate the typecasting of class type to class type.
# include <iostream.h>
# include <conio.h>
class in1
{
int code,items;
float price;
public:
in1(int a,int b,int c)
{
code=a;
items=b;
price=c;
}
void putdata()
{
cout<<"CODE= "<<code<<endl;
cout<<"ITEMS= "<<items<<endl;
cout<<"VALUE= "<<price<<endl;
}
int getcode()
{
return code;
}
int getitems()
{
return items;
}
int getprice()
{
return price;
}
operator float ()
{
return items*price;
}
};
class in2
{
int code;
float value;
public:
in2()
{
code=0;
value=0;
}
in2(int x,float y)
{
code=x;
value=y;
}
void putdata()
{
cout<<"CODE= "<<code<<endl;
cout<<"VALUE= "<<value<<endl;
}
in2(in1 p)
{
code=p.getcode();
value=p.getitems()*p.getprice();
}
};
void main()
{
clrscr();
in1 s1(100,51,145.0);
float tot_value;
in2 d1;
tot_value=s1;
d1=in1(s1);
cout<<"PRODUCT DETAILS INVENT-1 TYPES:->"<<endl;
s1.putdata();
cout<<"STOCK VALUE"<<endl;
cout<<"VALUE= "<<tot_value<<endl;
cout<<"PRODUCT DETAILS INVENT-2 TYPES:->"<<endl;
d1.putdata();
getch();
return 0;
}
Output:
Experiment 15
#include<iostream.h>
#include<conio.h>
class top //base class
{
public :
int a;
void getdata()
{
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is :::\t"<<a;
}
};
class middle :public top // class middle is derived_1
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
}
};
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"\n\nCube :::\t"<<c;
}
};
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
return 0;
}
Output:
Enter first number:12
Squae is: 144
Cube is:1728
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 16
AIM: Write a program to demonstrate the multiple inheritance.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Experiment 17
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Output:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Experiment 18
#include<iostream.h>
#include<conio.h>
class Account
{
protected:
int acc_no;
public:
Account(int ac)
{
acc_no = ac;
}
virtual void display()
{ } //Empty Virtual Function
};
void Current::display()
{
cout<<"The Current Account No : "<<acc_no<<endl;
cout<<"The Current Account Amount : "<<cur_amount<<endl;
}
void main()
{
Saving sav(01, 5000);
Current cur(02, 10000);
clrscr();
Account *acc; //Base Class Pointer
acc = &sav;
acc->display(); //display() From Saving Class
acc = &cur;
acc->display(); //display() From Current Class.
getch();
}
Output:
Experiment 19
AIM: WAP to demonstrate the exception handling.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"Answer is infinite because a-b is:"<<i;
}
getch();
}
Output:
Experiment 20
AIM: WAP to demonstrate the use of function template.
#include<iostream.h>
#include<conio.h>
template<class t>
Output:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
Experiment 21
AIM: WAP to demonstrate the use of class template.
#include<iostream.h>
#include<conio.h>
template<class t>
class cuboid
{
private:
t a;t b;t c;
public:
cuboid(t x,t y,t z)
{
a=x;
b=y;
c=z;
}
void volume()
{
t v;
v=a*b*c;
cout<<"Volume is : "<<v<<endl;
}
};
void main()
{
clrscr();
cuboid <int> c1(2,3,4);
c1.volume();
cuboid<float>c2(5.6,1.4,3.2);
c2.volume();
getch();
}
OUTPUT:
Volume is : 24
Volume is : 25.087999
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 22
AIM: WAP to copy the contents of a file to another file.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<ctype.h>
#include<fstream.h>
void main( )
{
ofstream outfile;
ifstream infile;
char fname1[10],fname2[20];
char ch,uch;
clrscr( );
cout<<"Enter a file name to be copied ";
cin>> fname1;
cout<<"Enter new file name";
cin>>fname2;
infile.open(fname1);
if( infile.fail( ) )
{
cerr<< " No such a file Exit";
getch();
exit(1);
}
outfile.open( fname2);
if(outfile.fail( ))
{
cerr<<"Unable to create a file";
getch();
exit(1);
}
while( !infile.eof( ) )
{
ch = (char) infile.get( );
uch = toupper(ch);
outfile.put(uch);
}
infile.close( );
outfile.close( );
getch( );
}
OUTPUT:
Input file
Asbcdefghijklmnopqrstuvwxyz
Output file
ASBCDEFGHIJKLMNOPQRSTUVWXYZ
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 23
AIM:WAP to demonstrate the read and write of file operation.
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char c,u;
char fname[10];
clrscr();
ofstream out;
cout<<"Enter File Name:";
cin>>fname;
out.open(fname);
cout<<"Enter the text(Enter # at end)\n"; //write contents to file
while((c=getchar())!='#')
{
u=c-32;
out<<u;
}
out.close();
ifstream in(fname); //read the contents of file
cout<<"\n\n\t\tThe File contains\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
Output:
Enter File Name: two.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 24
AIM: WAP to demonstrate the reading and writing of objects.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
int roll_no;
char name[20];
public:
void read()
{
cin>>roll_no>>name;
}
void show()
{
cout<<roll_no<<name;
}
};
void main()
{
clrscr();
student s1;
s1.read();
ofstream out("stu.txt",ios::binary);
out.write((char*)&s1,sizeof(s1));
ifstream in("stu.txt",ios::binary);
in.read((char*)&s1,sizeof(s1));
s1.show();
getch();
}
Output:
1140831
radha
1140831 radha
SRI SUKHMANI INSTITUTE OF ENGINEERING & TECHNOLOGY
Affiliated to PTU, & Approved by AICTE
Experiment 25
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<process.h>
class quadratic
{
private:
double a,b,c;
public:
void read();
void funcroots();
};
void quadratic::read()
{
cout<<"Enter coffiecients of Quadratic Eq. a,b,c";
cin>>a>>b>>c;
}
void quadratic::funcroots()
{
if(a==0)
{
cout<<"\nEquations is not quadratic";
exit(0);
}
else
{
double disc;
disc=b*b-4 *a*c;
if(disc<0)
cout<<"\nRoots are Imaginary";
else if(disc==0)
{
double x=-b/(2*a);
cout<<"\nRoots are Equal,x= u"<<x;
}
else
{
double m,n,t;
t=sqrt(disc);
m=( -b+t)/(2*a);
n=(-b-t)/(2*a);
cout<<"\nRoots are Real and Unequal"<<endl;
cout<<"m="<<m<<endl;
cout<<"n="<<n;
}
}
}
void main()
{
clrscr();
quadratic q1 ;
q1.read();
q1.funcroots();
getch();
}
Output: