CPP All Programs
CPP All Programs
1. Write an inline function called zerosmaller() that is passed two int arguments by reference
and then sets the smaller number to 0. Write a main() program to exercise this function.
#include<iostream>
using namespace std;
inline void zerosmaller(int &a,int &b)
{ if(a>b)
b=0;
if(a<b)
a=0;
if(a==b)
cout<<"both nos. are equal";
}
int main()
{ int a,b;
cout<<"enter 2 nos.";
cin>>a>>b;
zerosmaller(a,b);
cout<<endl<<a<<" "<<b;
}
2. INLINE MEMBER FUNCTIONS
Create a distance class with a data member of int type. Include a member function to take input
for distance and another inline member function to compare two distances. Write a main()
program to do the following functions.
#include<iostream>
using namespace std;
class dist
{ int a;
public:
void getd()
{ cout<<endl<<"enter distance in meter: ";
cin>>a;
}
inline int compare(dist d2)
{ return (a>d2.a)?a:d2.a;
}
};
int main()
{ dist d1,d2;
d1.getd();
d2.getd();
cout<<endl<<"the greater of the two distances is :"<<d1.compare(d2);
}
3. inline
#include<iostream.h>
#include<conio.h>
inline void cube(int &a)
{
a=a*a*a;
}
4. inline
#include<iostream.h>
#include<conio.h>
inline int cube(int *a)
{
return((*a)*(*a)*(*a));
}
int main()
{ int a;
clrscr();
cout<<"enter the number: ";
cin>>a;
cout<<"the cube is: "<<cube(&a);
getch();
return 0;
}
FUNCTION OVERLOADING
5. Write a program to find the absolute value of an integer as well as a floating point no using
function over loading.
#include <iostream>
using namespace std;
int absolute(int);
float absolute(float);
int main() {
int a = -5;
float b = 5.5;
cout << "Absolute value of " << a << " = " << absolute(a) << endl;
cout << "Absolute value of " << b << " = " << absolute(b);
return 0;
}
int absolute(int var)
{ if (var < 0)
var = -var;
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
#include<iostream>
#include<iomanip>
using namespace std;
long int sum(int n,int diff=1,int first_term=1 )
{long sum=0;;
for(int i=0;i<n;i++)
{cout<<setw(5)<<first_term+diff*i;
sum+=first_term+diff*i;
}
return sum;
}
int main()
{cout<<endl<<"Sum="<<setw(7)<<sum(10)<<endl;
//first term=1; diff=1,n=10
//sums the series 1,2,3,4,510
cout<<endl<<"Sum="<<setw(7)<<sum(6,3,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 2,5,8,11,14,17
cout<<endl<<"Sum="<<setw(7)<<sum(10,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 1,3,5..19
return 0;
}
7. Raising a number N to the power P is the same as multiplying N by itself P times. Write a
function called power() that takes N (double) and P (int) as input, and returns the result NP as a
double value. Use a default argument of 2 for P, so that if this argument is omitted, the number N
will be squared. Overload power() function, to work with int, long, and float. Overload the power()
function for char datatype also, which should print P times the given character N. Write the
main() program to exercise these overloaded functions with all argument types.
//FUNTION PROTOTYPES
int P;
double func(double N1,int P=2);
int func(int N2,int P=2);
float func(float N3,int P=2);
long int func(long int N4,int P=2);
void func(char N5,int P=2);
void main(void)
{ double N1;
cout<<"enter base";
cin>>N1;
int N2;
cout<<"enter base";
cin>>N2;
cout<<"Square of " <<N2<<"is (default arg 2) "<<func(N2)<<endl;
cout<<N2<<" Power "<<P<<" is "<<func(N2,P)<<endl;
float N3;
cout<<"enter base";
cin>>N3;
cout<<"Square of " <<N3<<"is (default arg 2) "<<func(N3)<<endl;
cout<<N3<<" Power "<<P<<" is "<<func(N3,P)<<endl;
long int N4;
cout<<"enter base";
cin>>N4;
cout<<"Square of " <<N4<<"is (default arg 2) "<<func(N4)<<endl;
cout<<N4<<" Power "<<P<<" is "<<func(N4,P)<<endl;
char N5;
cout<<"enter base";
cin>>N5;
cout<<"Square of " <<N5<<"is (default arg 2) ";
func(N5);
cout<<N5<<" Power "<<P<<" is ";
func(N5,P);
}
double func(double N,int P)
{double pwr=1.0;
int i;
for(i=1;i<=P;i++)
pwr=pwr*N;
return pwr;
}
int func(int N,int P)
{int pwr=1;
int i;
for(i=1;i<=P;i++)
pwr=pwr*N;
return pwr;
}
long int func(long int N,int P)
{long int pwr=1;
int i;
for(i=1;i<=P;i++)
pwr=pwr*N;
return pwr;
}
float func(float N,int P)
}
void disp()
{ cout<<min<<":"<<sec;
}
void add(timex a,timex b)
{ a.sec+=b.sec+a.min*60+b.min*60;
a.min=a.sec/60;
a.sec=a.sec%60;
cout<<a.min<<":"<<a.sec<<endl;
}
};
int add(int a,int b)
{ int c;
c=a+b;
return c;
}
int main()
{ timex a,b,c;
a.getd();
b.getd();
c.add(a,b);//member function called
int x,y;//ordinary int variables
cin>>x>>y;
cout<<"x+y= "<<add(x,y);
}
9. To write a program to find the multiplication values and the cubic values using inline function.
12. A default constructor does not have any parameter, but if you need, a constructor can have parameters. This
helps you to assign initial value to an object at the time of its creation as shown in the following example:
16.Write a C++ program to create a class called Complex and implement the following
overloading functions ADD that returns a complex number:
1. ADD (a,s2)- where a is an integer(real part) and s2 is a complex number.
2. ADD (s1,s2)-where s1 and s2 are complex numbers.
#include<iostream>
using namespace std;
class complex
{ int rp;
int ip;
public:
complex()
{}
complex(int x,int y)
{ rp=x;ip=y;
}
complex add(int a,complex s1)
{ complex c;
c.rp=a+s1.rp;
c.ip=s1.ip;
return c;
}
complex add (complex s1,complex s2)
{ return complex((s1.rp+s2.rp),(s1.ip+s2.ip));
}
void disp()
{ cout<<endl<<rp<<" "<<ip;
}
};
int main()
{complex s1(10,25),s2(20,35),s3;
int a=10;
s3.add(a,s1);
s3.disp();
s3.add(s1,s2);
s3.disp();
}
int main()
{int i,n;
employee e[10];
cout<<"enter number of employee ";
cin>>n;
for(i=0;i<n;i++)
e[i].getData();
cout<<setiosflags(ios::left);
cout<<setw(10)<<"Employye no"<<setw(10)<<"Employee name\n";
cout<<setw(10)<<"___________"<<setw(10)<<"_________ \n";
for(i=0;i<n;i++)
e[i].putData();
}
for(i=0;i<3;i++)
{ totalfeet+=d[i].feet;
totalinches+=d[i].inches;
}
totalfeet+=(totalinches/12);
totalinches=totalinches%12;
avgdistancefeet=(float)totalfeet/3;
avgdistanceinches=totalinches/3;
cout<<endl<<"The average distance is ..."<<avgdistancefeet<<" feet "<<avgdistanceinches<<"
inches"<<endl;
}
};
int main()
{distance d[3];
for(int i=0;i<3;i++)
d[i].getdata();
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
}
public:
tim() {hrs=0;min=0;sec=0;}
tim(long int i)
{
min=i/60;
min=min%60;
hrs=i/3600;
sec=i%60;
}
disp()
{cout<<hrs<<":"<<min<<":"<<sec;
}
};
int main()
{long int l;
tim m(l);
m.disp();
return 0;
}
_________________________________________________________
POINTERS
47. GENERIC POINTER
#include <iostream>
using namespace std;
int main()
{ int *a,a1=5;
float* b,b1=10;
void *c;
b=&b1;
a=&a1;
c=&a1;
cout<<"The value of the generic pointer can be ( pointing to an integer ):
"<<*static_cast<int*>(c);
c=&b1;
cout<<" and (pointing to a float ): "<<*static_cast<float*>(c);
a=reinterpret_cast<int*>(b);
cout<<"\nThe value of a float pointer pointing to an integer is: "<<*b;
}
H
Helloo
H
l
___________________________________________________________________________
int main()
{ int x=5;
cout<<"\nFIRST TYPE: ";
cout<<"\nBEFORE: "<<x;
change(x);
cout<<"\nAFTER: "<<x;
return 0;
}
FIRST TYPE:
BEFORE: 5
AFTER: 6
SECOND TYPE:
BEFORE: 6
AFTER: 7
cout<<*(++ptr)<<" ";
12
21
HELLO ALL.
H
52.THIS POINTER
#include<iostream.h>
using namespace std;
class max
{ int a;
public:
void getdata()
{ cout<<"Enter the Value :";
cin>>a;
}
max &greater(max &x)
{ if(x.a>=a)
return x;
elsereturn *this;
}
void display()
{ cout<<"Maximum No. is : "<<a<<endl;
}
};
main()
{ max one,two,three;
one.getdata();
two.getdata();
three=one.greater(two);
three.display();
}
53. Pointers for stack operations
#include<iostream.h>
#include<conio.h>
class stack
void stack::pop()
{if(!isempty())
{ top--;
--size;
cout<<"stack top is deleted !!";
}
else
cout<<"stack is empty, u cant delete \n";
}
void main()
{ stack a;
stack *ptr=&a;
int p,i,o;
char opt='y';
clrscr();
cout<<"enter the choice ";
while(opt=='y')
{ cout<<"\n1.insrt\n2.delete\n3.search\n4.display\n";
cin>>p;
switch(p)
{case 1:
ptr->push();
break;
case 2:
ptr->pop();
break;
case 3:
ptr->search();
break;
case 4:
ptr->display();
break;
}
cout<<"do u want to continue? ";
cin>>opt;
}
}
class String {
char *s;
int len;
public:
String() {
s=new char[81];
len=80;
}
String(int n) {
s=new char[n + 1];
len=n;
}
String(char *p) {
len=strlen(p);
s=new char[len + 1];
strcpy(s, p);
}
String(String &str);
String::String(String &str) {
len=str.len;
s=new char[len + 1];
strcpy(s, str.s);
}
main()
{
clrscr();
char *str="Hello everybody. ";
String a(str), b, c("Have a nice day. "), both, quote=c;
b.assign("How are you? ");
both.concat(a, b);
quote.print();
a.print();
both.print();
return 0;
}
____________________________________________________________________________________
INHERITANCE
55.Simple Inheritance
//public access specifier
#include<iostream>
using namespace std;
class B
{ protected:
int a,b;
public:
void getdata()
{
cout<<"enter the two numbers"<<endl;
cin>>a>>b;
}
57.Multilevel Inheritance
#include<iostream>
using namespace std;
class student // base class
{
private:
int rl;
char nm[20];
public:
void read`()
{ cout<<"enter Roll no and Name ";
cin>>rl>>nm;
}
void display()
{ cout <<"Roll NO:"<<rl<<endl;
cout<<"name : "<<nm<<endl;
}
};
class marks : public student
{protected:
int s1;
int s2;
59.Multiple inheritance
#include<iostream.h>
#include<conio.h>
class emp
{
protected:
char name[10],gen;
int no,sal,age;
public:
int g;
void getdata()
{
void display()
{
cout<<"empno:"<<no<<"\nname:"<<name<<"\ngen:"<<gen<<"\nage:"<<age<<"\nsal:"<<sal;
}
};
class student
{
protected:
char name[10],dept[10];
int regno,yr;
float cgpa;
public:
int g;
void getdata()
{
cout<<"enter name , no,dept,yr,cgpa and research interest:";
cin>>name>>regno>>dept>>yr>>cgpa>>g;
}
void display()
{
cout<<"name:"<<name<<"\nregno:"<<regno<<"\ndept"<<dept<<"\nyr:"<<yr<<"\ncgpa"<<cgpa
;
}
};
void display1()
{
student::display();
}
};
void main()
{ research q[25];
int i,j,u,v;
cout<<"no of employees";
cin>>v;
for(i=0;i<v;i++)
{ q[i].emp::getdata();
}
cout<<"enter no of students";
cin>>u;
65.over riding 2
#include<iostream>
using namespace std;
class m
{ public:
void display()
{
cout<<"m"<<endl;
}
};
class n
{ public:
void display()
{
cout<<"n"<<endl;
}
};
class p: public m,public n
{ public:
void display()
{ cout<<"p"<<endl;
m::display();
n::display();d
}
};
int main()
{ p p1;
p1.display();
}
Output:
Output:
a
b
c
a
b(int)
67. ORDER OF CONSTRUCTOR CALLING WHEN A CLASS IS INHERITED VIRTUALLY
class Base1 { public:
Base1() { cout<<Base 1; }
};
class Base2 { public:
Base2(){ cout<<Base2; }
};
OUTPUT:
Base2
Base1
Derived Class
_______________________________________________________________
#include<iostream>
using namespace std;
class abc
69.#include<iostream>
using namespace std;
class abc
{int a,b;
public:
void setdata(int c,int d)
{a=c;
b=d;
}
void getdata()
{cin>>a>>b;
}
void display()
{cout<<"\n"<<a<<"\n"<<b;
} };
int main()
{abc x, v;
v.setdata(4,8);
cout<<"enter 2 values";
x.getdata();
v.display();
x.display();
}
70.#include<iostream>
using namespace std;
class abc
{int a,b;
public:
void setdata(int c,int d)
{a=c;
b=d;
display(); //nesting of member functions
}
71.#include<iostream>
using namespace std;
class abc
{private:
int a,b;
void display() //member functions can also be private
{ cout<<a<<b;
}
public:
void setdata(int c,int d)
{a=c;
b=d;
display(); //calling private member functions
}
void getdata()
{cin>>a>>b;
display();
}
};
int main()
{abc x, v;
v.setdata(4,8);
v.display(); //error :display() is private can't be accessed from main()
x.getdata();
return 0; }
72. #include<iostream>
using namespace std;
class counter
{int a;
static int count;
public:
void getdata()
{cout<<enter a no;
cin>>a;
count++;
}
void display()
73.array of objects
#include<iostream>
using namespace std;
class abc
{int a,b;
public:
void getdata()
{cin>>a;
}
void display()
{cout<<"\n"<<a;
} };
int main()
{abc x[5]; //creating array of objects
cout<<"enter 5 no's";
for(int i=0;i<5;i++)
{ x[i].getdata();
}
for(int i=0;i<5;i++)
{ x[i].display();
}
return 0;
}
int main() {
Example Object;
// Constructor invoked.
prime(int x)
80.area of rectangle
//constructors
#include <iostream>
using namespace std;
class Area
{ private:
int length;
int breadth;
public:
// Constructor
Area(): length(5), breadth(2){ }
void GetLength()
{ cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
int main()
{ Area A1, A2(2, 1);
int temp;
cout << "Default Area when no argument is passed." << endl;
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << "Area when (2,1) is passed as argument." << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
return 0;
}
class Complex{
float real; // real part of the complex number
float imag; // imaginary part of the complex number
public:
Complex(float = 0.0, float = 0.0);
void print() {
cout << real << "+" << imag << "i" << endl; }
int
main(void)
{
Complex c1(1.1,2.2), c2(3.3, 4.4);
Complex c3 = add(c1, c2);
c3.print();
return(0);
}
class Vector{
int vec[3];
public:
void Init();
friend Vector multiply(Matrix &, Vector &);
void display();
};
class Matrix{
int mat[3][3];
public:
Matrix();
friend Vector multiply(Matrix &, Vector &);
};
Matrix::Matrix()
{
cout<<"\nEnter the data for the matrix:";
for(int r=0; r<3;r++)
for(int c=0; c<3;c++)
cin>>mat[r][c];
}
void Vector::display()
{
for(int i=0; i<3;i++)
cout<<vec[i]<<" ";
}
int
main(void)
{
clrscr();
Vector V1,V2;
Matrix M1;
V1.Init();
V2=multiply(M1,V1);
V2.display();
return(0);
}
public:
Coords(int xVal = 0, int yVal = 0) {
x = xVal;
y = yVal;
}
int retX() const {
return x;
}
int retY() const {
return y;
}
friend Coords operator+ (const Coords&, const Coords&);
friend Coords operator- (Coords&);
};
Coords operator+ (const Coords& left, const Coords& right) {
Coords result;
result.x = left.x + right.x;
result.y = left.y + right.y;
88.friend function
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class ad1;
class ad
{int a,b;
public:
void get()
{cin>>a>>b;
}
friend void put(ad,ad1);
};
class ad1
{int c,d;
public:
void get()
{cin>>c>>d;
}
friend void put(ad,ad1);
};
void put(ad ob,ad1 ob1)
{cout<<ob.a<<ob.b<<ob1.c<<ob1.d;
}
void main()
{ad ob;
ad1 ob1;
ob.get(); ob1.get();
put(ob,ob1);
}
89.Virtual Functions
class A
{ int a;
Class B: public A
{ int b;
public:
B()
{ b = 2;
}
virtual void show()
{ cout <<b;
}
};
int main()
{ A *pA;
B oB;
pA = &oB;
pAshow();
return 0;
}
90. virtual
#include<iostream>
using namespace std;
class shape
{ protected:
double base,height;
public:
void getdata(int i,int j)
{ base=i;
height=j;
}
virtual void area()
{ cout<<"\n\nTriangle Area "<<(0.5*base*height);
cout<<"\n\nRectangle Area "<<(base*height);
}
};
int main()
{ shape *s;
triangle t;
rectangle r;
int k,l,ch;
cout<<"\n\nEnter base ";
cin>>k;
cout<<"\n\nEnter height ";
cin>>l;
cout<<"\n\n1. Triangle 2.Rectangle ";
cin>>ch;
if(ch==1)
{ s=&t;
s->getdata(k,l);
s->area();
}
if(ch==2)
{ s=&r;
s->getdata(k,l);
s->area();
}
return 0;
}
91.
#include <iostream.h>
class Bclass
{
public:
void disp()
{ cout << " BASE BASE\n" ; }
virtual void sh()
{ cout << "base base\n"; }
};
class Dclass : public Bclass
{
public:
void disp()
{ cout << "DERIVED DERIVED\n"; }
void sh()
{ cout << "derived derived\n"; }
};
int main()
{
Bclass B;
Dclass D;
Bclass *ptr;
cout << "ptr points to base class\n" ;
ptr = &B;
ptr->disp();
ptr->sh();
cout << "ptr points derived class\n";
ptr = &D;
Result
ptr points to base class
BASE BASE
base base
ptr points derived class
BASE BASE
derived derived
case 2:
ptr=&r;
ptr->getdata();
ptr->display();
break;
case 3:
ptr=&c;
ptr->getdata();
ptr->display();
break;
}
}
}
93.class template
#include <iostream.h>
#include<conio.h>
template <class T>
T
max(T a, T b){
if (a > b)
return a;
else
return b;
}
int main(void)
{
clrscr();
cout << max(1,2)<<endl;
OTP:
Enter string: fate is not an eagle
elgae na ton si etaf
ComputerScience
102. Write a program to compare two strings they are exact equal or not?
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];
cout<<"Enter first string: ";
gets(str1);
cout<<"Enter second string: ";
gets(str2);
int i;
for (i = 0; str1[i] == str2[i] && str1[i]!= '\0' && str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal";
else
cout << "Strings are not equal";
return 0;
}
Otp:
Enter first string: Mango
Enter second string: Man
Strings are not equal
if(i == l/2)
cout << "Palindrome";
else
cout << "Not a palindrome";
}
Otp:
Enter string: radar
Palindrom
Otp:
Enter first string: Mango
Enter second string: go
Substring found at position 4
Otp:
Enter string: spoon
Reverse string: noops
Otp:
Enter string: Hello World
Lowercase string: hello world
Otp:
Enter a string: People always fear change.
108. Write the output of the following program. Assume that all necessary header files are included?
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}
int main()
{ char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;
}
Otp:
@a@E@E#rTH
109. Write the output of the following program?
int main()
{ char name[] = "CoMPutER";
for (int x = 0; x < strlen(name); x++)
Otp:
cOmmUTee
110.Write a program to write a program that reverses the given string using string class using a
re() function and displays the result.
Source code:
#include<iostream>
#include<string.h>
using namespace std;
void rev(string);
int main()
{ string s;
cout<<"Enter the string to be reversed:";
getline(cin,s);
rev(s);
}
void rev(string s)
{ char c;
for(int i=0,j=s.size();i<s.size()/2-1;i++,j--)
{ c=s[i];
s[i]=s[j];
s[j]=c;
}
cout<<"\nReversed string ="<<s;
}
Output:
111.Write a c++ program using string class which gets a string and a substring,The program
should replace the substring with new user given string.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{ string s;
char c[20],h[20];
int b,flag=0;
cout<<"Enter the string:";
getline(cin,s);
cout<<"\nEnter the sub string:";
cin.getline(c,20);
cout<<"\nEnter the replacement string:";
cin.getline(h,10);
113.Write a program to concatenate two strings read from without using any pre defined
function.use string clss concept.
Source code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{string str1,str2;
string str3;
int len ;
cout<<"Enter the string1:";
getline(cin,str1);
cout<<"Enter the string2:";
getline(cin,str2);
str3 = str1 + str2;
114.Write a program that counts Number of Vowels in a given sentence using string class
functions.
#include <iostream>
#include <string>
using namespace std;
int main ()
{string s,s1("aeiouAEIOU");
int f,i,count=0,len;
cout<<"Enter the string:";
getline(cin,s);
len = s.size();
f=s.find_first_of(s1,0);
if(f>=0)
count++;
i=f+1;
for(;i<len;)
{ f=s.find_first_of(s1,i);
count++;
i=f+1;
}
cout << "Number of vowels = " << count << endl;
return 0;
}
while(i<len)
{
if(s.at(i)==' ')
count++;
i++;
}
void putdata()
{cout<<"string is: "<<str;}
};
int main (void)
{ st s;
char temp;
s.getdata();
s.stln();
s.srt();
s.putdata();
return 0;
}
Files
Opening a file
ifstreamifile;
ifile.open(data2.txt);
File mode parameter Meaning
ios::app Append to end of file
ios::ate go to end of file on opening
fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
file.close();//Closing file
file.get(ch);
file.put(ch);
OUTPUT:
Time is a great teacher
OUTPUT:
Number of words in file is 5
}while(y==1);
return 0;
}
OUTPUT:
Enter The admission no. 1
Admission no. : 1
Student Name : a
Admission no. : 2
Student Name : b
Admission no. : 2
Student Name : b
Admission no. : 1
Student Name : a
All i/o streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the
next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next
element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from
both istream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream can be
manipulated using the following member functions:
seekg(offset, refposition );
seekp(offset, refposition );
The parameter offset represents the number of bytes the file pointer is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined
in the ios class.
example:
file.seekg(-10, ios::cur);
The seekp() and tellp() member functions are identical to the above two functions, but they are identified
with the put pointer. The seekg() and tellg() member functions are defined in the istream class. The
seekp() and tellp() member functions are defined in the ostream class.
Program tofinds out the number of records in the file billfile.dat by using the seekg() and tellg()
functions.
128.
Prepared by I Btech- I section 2016-17 Batch, Consolidated by Divya.V I Section
& updated by G.R.Brindha, SoC, SASTRA University
#include<fstream.h>
#include<iostream.h>
class bill
{
private:
intiBill_no;
floatfBill_amt;
public:
void getdata()
{
cout<<Enter Bill number;
cin>>iBill_no;
cout<<Enter Bill amount;
cin>>fBill_amt;
}
void showdata()
{
cout<<Bill number <<iBill_no<<endl;
cout<<Bill amount <<fBill_amt<<endl;
}
};
void main()
{
fstream Fi1(billfile.dat,ios::in);
Fi1.seekg(0,ios::end);
ini iEnd;
iEnd=Fi1.tellg();
cout<<The size of the file is <<iEnd<<endl;
cout<<Size of one record is <<sizeof(bill)<<endl
ini iNorec=iEnd/sizeof(bill);
cout<<There are <<iNored<<records in the file<<endl;
}