C++ Programs
C++ Programs
eg:
class stud
{
protected :
int regno,m1,m2; // data members
public :
void get(); // member function
}
class total : protected stud
{
protected:
int total;
}
class avg : protected total
{
}
void main()
{
stud s,t;
s.get();
t.get();
}
header file
-------------
#include<iostream.h>
datatypes
-----------
int (integer)
float
char
double
input syntax
--------------
cin>>var1>>var2>>.........>>varn;
eg :
int n;
char a;
cin>>n>>a; or cin>>n; cin>>a;
output syntax
-----------------
cout<<var1<<var2<<........<<varn;
eg:
int n=10;
char a='x';
cout<<" Value of n = "<<n;
cout<<"\n Value of a = "<<a;
output:
Value of n = 10
Value of a = x
operators
----------
1.Arithmetic operators +,-,*,/ , % (mod)
2.Relational operators > , < , <= , >=
3.Logical operators (or) || , (and) && , (not) !
4.Equality operators (equal to) == , (not equal to) !=
5.Assignment operators =, +=, -=, *=, /=, %=
6.Bitwise operators (or) | , (and) &
7.Conditional operator (condition)? thenpart : elsepart;
8.Increment Decrement ++ , --
comment - brief explanation of the program (comment lines are not executed)
1. Single line comment - // comment
2. Multi Line Comment - /* comments ................
........*/
Prog -1
Prog -2
//Constant
#include<iostream.h>
#include<conio.h>
void main()
{
int n=100;
const float pie=3.14;
clrscr();
n=200;
pie=7.44;
cout<<"\n The value of n is :"<<n; //modify
cout<<"\n The value of pie is :"<<pie; //can not modify
getch();
}
Prog -3
//constant pointers
#include<iostream.h>
#include<conio.h>
void xstrcpy(char *,const char *);
void main()
{
clrscr();
char str1[]="nagpur"; //n=2
char str2[10];
xstrcpy(str2,str1);
cout<<endl<<str2;
getch();
}
void xstrcpy(char *t,const char *s)
{
// *s="good"; //cannot modify
while(*s!='\0')
{
*t=*s;
cout<<endl<<*t;
t++;
s++;
}
*t='\0';
}
Prog -4
//Constant References
#include<iostream.h>
#include<conio.h>
void change(const int &);
void main()
{ //int a=10,*p
int i=32; //p=&a;
clrscr();
change(i);
cout<<endl<<i;
getch();
}
void change(const &j) //&j=i;
{
// j=30; //can not modify a constant object
cout<<j;
}
Prog -5
Prog -6
//Reference variable. A reference is like a pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int i=10;
int &j=i; //& - Reference datatype
clrscr();
cout<<endl<<"i="<<i<<"\t j="<<j;
j=20;
cout<<endl<<"i="<<i<<"\t j="<<j;
i=30;
cout<<endl<<"i="<<i<<"\t j="<<j;
i++;
cout<<endl<<"i="<<i<<"\t j="<<j;
j++;
cout<<endl<<"i="<<i<<"\t j="<<j;
cout<<endl<<"The address of i="<<&i;
cout<<endl<<"The address of j="<<&j;
getch();
}
Prog -7
i=j;
j=t;
cout<<"Inside the function :"<<endl;
cout<<i<<"\t"<<j;
}
void swapp(int *i,int *j)
{
int t;
t=*i;
*i=*j;
*j=t;
}
void swapr(int &i,int &j) //&i=a;&j=b;
{
int t;
t=i;
i=j;
j=t;
}
Prog -8
Prog -9
Prog -10
Prog -11
//String function
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[]="welcome";
int a[]={1,2,3},l;
clrscr();
strupr(str);
cout<<"In Upper case :"<<str<<endl;
strlwr(str);
cout<<"In Lower case :"<<str<<endl;
strrev(str);
cout<<"In reverse :"<<str<<endl;
l=strlen(str);
cout<<"Length of the string :"<<l<<endl;
cout<<"Integer array :"<<a[2];
getch();
}
Prog -12
#include<iostream.h>
#include<conio.h>
inline float square (float y)
{
return y*y;
}
void main()
{
clrscr();
float b=3.0,d;
d=square(++b);
cout<<endl<<"Using Inline function :"<<d;
getch();
}
Prog -13
//Inline Function-2
#include<iostream.h>
#include<conio.h>
#include<math.h>
inline double power(double m,int n)
{
return (pow(m,n));
}
void main()
{
double m,p;
int n;
clrscr();
cout<<"Enter the Base value :";
cin>>m; //2
cout<<"Enter the power value :";
cin>>n; //3
p=power(m,n);//2,3
cout<<"The Exponent value is :"<<p;
getch();
}
Prog -14
//Inline Function-3
#include<iostream.h>
#include<conio.h>
inline int large(int x,int y,int z)
{
if(x>y && x>z) //5>7 5>4
return x;
else if(y>z) //7>4
return y;
else
return z;
}
void main()
{
int a,b,c,d;
clrscr();
cout<<"\n Enter Three no's :"<<endl;
cin>>a>>b>>c;
d=large(a,b,c); //5 7 4
cout<<"The largest among the given no's is :"<<d;
getch();
}
Prog -15
/*
Class Syntax
------------
class <classname>
{
mode :
<datatype1> var11,var12,....,var1n;
<datatype2> var21,var22,....,var2n;
.
.
.
<datatypen> varm1,varm2,.....,varmn;
mode :
<returndatatype> functionname(<dt1> par1,<dt2> par2,....,<dtn>
parn)
{
local declarations;
set of statements;
}
.
.
.
};
three modes
-------------
1.public - access to all
2.private - accessed by the class itself
3.protected - accessed by the class and its immediate successor
*/
Prog -16
Prog -17
#include<iostream.h>
#include<conio.h>
class samp
{
private :
int n;
public:
void disp()
{
n=10;
cout<<"\n n = "<<n;
}
};
class sample
{
private :
int n1;
public :
void disp1()
{
n1=20;
cout<<"\n n1 = "<<n1;
}
};
void main()
{
clrscr();
samp s;
s.disp();
sample ss;
ss.disp1();
getch();
}
Prog -18
#include<iostream.h>
#include<conio.h>
class student
{
private :
int m1,m2,m3,m4,m5,sum;
float avg;
char name[20];
public:
void get()
{
cout<<"Enter the name :";
cin>>name;
cout<<"Enter the marks(m1,m2,m3,m4,m5):";
cin>>m1>>m2>>m3>>m4>>m5;
}
void put()
{
sum=m1+m2+m3+m4+m5;
avg=sum/5;
cout<<"Name :"<<name<<endl;
cout<<"Sum :"<<sum<<endl;
cout<<"Average :"<<avg<<endl;
}
};
void main()
{
student s; //object
clrscr();
s.get();
s.put();
getch();
}
Prog -19
Prog -20
//pointer object.
#include<iostream.h>
#include<conio.h>
class abc
{
int x;
public:
void set(int a)
{
x=a;
}
void disp()
{
cout<<"\n"<<"value of x:"<< x<<"\n";
cout<<"\n"<<"Address of x :"<<&x;
}
};
void main()
{
clrscr();
abc *ptr,obj;
ptr=&obj;
ptr->set(10);
ptr->disp();
ptr->set(100);
ptr->disp();
obj.set(90);
obj.disp();
getch();
}
Prog -21
//Constructor:
A function having same name as class and has no return type is called
as constructor and it is called automatically when we create an object for that
class.
1. Default constructor
2. Parameterized constructor
3. Multiple constructor
4. Copy constructor
5. Dynamic constructor
6. Constructor with default argument
//Default Constructor
/* Definition:
A Constructor that accepts no parameters is called the
default constructor.*/
Prog -22
Prog -23
//Parameterized Constructor
/* Definition:
A Constructor that can take parameters (or) arguments is called
parameterized constructor.*/
//Example program for parameterized constructor
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
sample(int n,int m)
{
a=n;
b=m;
}
void display()
{
cout<<endl<<"The value of a is "<<a;
cout<<endl<<"The value of b is "<<b;
}
};
void main()
{
clrscr();
sample i1(5,10);
i1.display();
getch();
}
Prog -24
//Copy Constructor
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code()
{}
code(int a)
{
id=a;
}
code(code &x) //copy constructor
{
id=x.id;
}
void display()
{
cout<<endl<<id;
}
};
void main()
{
clrscr();
code a(100); //Object a is created & Initialized
code b(a); //copy constructor called
code c=a; // " " " again
code d; //d is created,not initialized
d=a; //copy constructor not called
a.display();
b.display();
c.display();
d.display();
getch();
}
Prog -26
/* DYANAMIC CONSTRUCTOR:
A Dynamic constructor is one,which the allocate the exactly require
memory for the data member and initialize it with the received parameter.
The memory allocation is done with the help of the new operator.
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class name
{
char * starname;
int rank,length;
public :
name()
{
rank=0;
length=0;
starname =new char [length+1];
}
name(char *na,int rn)
{
length=strlen(na);
starname=new char [length+1];
strcpy(starname,na);
rank=rn;
}
void display()
{
cout<<"\n Starname is "<<starname<<" "<<"rank is "<<rank;
}
};
void main()
{
clrscr();
char *star1,*star2;
int rank1,rank2;
cout<<"\n Enter star name1 and rank : ";
cin>>star1>>rank1;
cout<<"\n Enter star name2 and rank : ";
cin>>star2>>rank2;
name n1(star1,1);
name n2(star2,2);
n1.display();
n2.display();
getch();
}
Prog -27
Prog -28
/*Multiple constructor
more than constructor can be used in a program is called as multiple
constructor*/
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
sample()
{
cout<<"\n Constructor1 is invoked";
}
sample(int a)
{
cout<<"\n Value of a is "<<a;
}
sample(int a,int b)
{
cout<<"\n Value of a & b is "<<a<<"\t"<<b;
}
};
void main()
{
clrscr();
sample s,s1(6),s2(10,20);
/* sample s1(6);
sample s2(6,10);*/
getch();
}
Prog -29
Prog -30
/* Destructor :
We declare destructor in a class to releases memory space for
future use.
1. Destructor is used to destroy the object that has been created by
the constructor.
2. Destructor member function name is same as the class name.
It is proceeded by Dilte "~" symbol.
3. Destructor never takes argument and nor return values.
4. It is invoke implicitly by the compiler upon exit of the program.
/* Destructor
- is a function which is having the same class name with
tilted (~) symbol and it is called whenever an object is
deleted.
- it doesn't return any values
- purpose is to close the files and deallocate the memory
*/
Prog -31
/* Types of Inheritance:
1.Single Inheritance
2.Multiple "
3.Multi level "
4.Hierarchical "
5.Hybrid "
6.Multi-path "
Single Inheritance:
Derivation of a class from only one base class is called single
inheritance.
*/
#include<iostream.h>
#include<conio.h>
class sample1
{
private :
int n;
public:
void display1()
{
n = 10;
cout<<"\n n = "<<n;
}
};
class sample2 : public sample1
{
private :
int m;
public :
void display2()
{
sample1::display1();
m = 20;
cout<<"\n m = "<<m;
}
};
void main()
{
sample1 ss1; //base class object
sample2 ss; //derived " "
clrscr();
ss.display2();
ss1.display1();
getch();
}
Prog -32
Prog -33
/* Multiple Inheritance:
Derivation of class from several (more than one) base classes is
called Multiple Inheritance.
*/
{
n=1;
cout<<"\n base1 class ";
cout<<"\n n = "<<n;
}
};
class base2
{
private :
int m;
public :
void disp1()
{
m=2;
cout<<"\n\n base2 class";
cout<<"\n m = "<<m;
}
};
class derv : public base1, public base2
{
private :
int p;
public :
void disp2()
{
base1::disp();
base2::disp1();
p=3;
cout<<"\n\n dervied class";
cout<<"\n p = "<<p;
}
};
void main()
{
clrscr();
derv d;
d.disp2();
getch();
}
Prog -34
/* Hierarchical inheritance:
One base class and more than one derived class is called as
Hierarchical Inheritance.*/
cout<<"\n p = "<<p;
}
};
class d : public a
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
a::disp();
q=4;
cout<<"\n q = "<<q;
}
};
class e : public c
{
private :
int r;
public :
void disp4()
{
cout<<"\n\n E class";
c::disp2();
r=5;
cout<<"\n r = "<<r;
}
};
class f : public c
{
private :
int s;
public :
void disp5()
{
cout<<"\n\n F class";
c::disp2();
s=6;
cout<<"\n s = "<<s;
}
};
class g : public c
{
private :
int x;
public :
void disp6()
{
cout<<"\n\n G class";
c::disp2();
x=6;
cout<<"\n x = "<<x;
}
};
void main()
{
clrscr();
e ee;
f ff;
g gg;
b bb;
d dd;
ee.disp4();
ff.disp5();
gg.disp6();
bb.disp1();
dd.disp3();
getch();
}
Prog -35
/* Hybrid Inheritance :
Combination of more than one type of inheritance is called as
Hybrid inheritance.*/
cout<<"\n p = "<<p;
}
};
class d : public b
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
b::disp1();
q=4;
cout<<"\n q = "<<q;
}
};
class e : public b
{
private :
int r;
public :
void disp4()
{
cout<<"\n\n E class";
b::disp1();
r=5;
cout<<"\n r = "<<r;
}
};
class f : public c
{
private :
int s;
public :
void disp5()
{
cout<<"\n\n F class";
c::disp2();
s=6;
cout<<"\n s = "<<s;
}
} ;
void main()
{
clrscr();
d dd;
e ee;
f ff;
dd.disp3();
ee.disp4();
ff.disp5();
getch();
}
Prog -36
#include<iostream.h>
#include<conio.h>
class a
{
private:
int n;
public:
void disp()
{
n=1;
cout<<"\n A class ";
cout<<"\n n = "<<n;
}
};
class b : public a
{
private :
int m;
public :
void disp1()
{
cout<<"\n B class";
a::disp();
m=2;
cout<<"\n m = "<<m;
}
};
class c:public a
{
private :
int p;
public :
void disp2()
{
cout<<"\n C class";
a::disp();
p=3;
cout<<"\n p = "<<p;
}
};
class d : public b,public c
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
b::disp1();
c::disp2();
q=4;
cout<<"\n q = "<<q;
}
};
void main()
{
clrscr();
d dd;
dd.disp3();
getch();
}
Prog -37
//Function Overloading
#include<iostream.h>
#include<conio.h>
class emp
{
int eno;
char ename[10];
float hra,ta,basic_sal,tot;
public:
void get()
{
cout<<"Enter the Employee number :";
cin>>eno;
cout<<"Enter the Employee Name :";
cin>>ename;
}
void get(float h,float t,float b)
{
hra=h;
ta=t;
basic_sal=b;
}
void process()
{
tot=hra+ta+basic_sal;
}
void put()
{
cout<<"Employee Number is :"<<eno<<endl;
cout<<"Employee name is :"<<ename<<endl;
}
void put1()
{
cout<<"Employee Net Salary is :"<<tot<<endl;
}
};
void main()
{
emp e1,e2;
clrscr();
e1.get();
e1.put();
e2.get(200,40,2100);
e2.process();
e2.put1();
getch();
}
Prog -38
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m,n,o;
float d,e;
cout<<"\n Enter three integer values :";
cin>>m>>n>>o;
cout<<"\n Enter two floating values :";
cin>>d>>e;
add(m,n);
add(m,n,o);
add(d,m);
add(d,e);
getch();
}
Prog -39
//Function Overloading-3
#include<iostream.h>
#include<conio.h>
double power(double,int);
int power(int,int);
void main()
{
double m,p;
int n,x;
clrscr();
cout<<"\n Enter the Base value :";
cin>>m;
cout<<"\n Enter the power value:";
cin>>n;
p=power(m,n);
cout<<endl<<"The I exponent value is :"<<p<<endl;
x=power(n,m);
cout<<endl<<"The II exponent value is :"<<x<<endl;
getch();
}
double power(double i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}
int power(int i,int j)
{
int k;
int t=j,x;
for(k=1;k<i;k++)
{
x=t;
j=j*x;
}
return(j);
}
Prog -40
/* Friend function
The main concept of the object oriented programming are data hiding
and data encapsulation.whenever data objects are declared in a private and
protected category of class,these members are restricted from accessing by
non member function.
To solve this problem,a friend function can be declared to have an
access to these members.
Definition :
Prog -35
//friend classes
#include<iostream.h>
#include<conio.h>
class two;
class one
{
private :
int i;
public :
one()
{
i=10;
}
friend two;
};
class two //friend class
{
public :
void func1(one o)
{
cout<<endl<<o.i;
}
void func2(one o)
{
cout<<endl<<o.i+10;
}
};
void main()
{
clrscr();
one o;
two b;
b.func1(o);
b.func2(o);
getch();
}
Prog -41
//To find minimum of two no's between two class objects using Friend function
#include<iostream.h>
#include<conio.h>
class sum1; //dummy class declaration
class sum
{
private :
int n;
public :
void get()
{
cout<<"\n Enter the value of n :";
cin>>n;
}
friend void minimum(sum obj,sum1 obj1);
};
class sum1
{
private :
int m;
public :
void get1()
{
cout<<"\n Enter the value of m :";
cin>>m;
}
friend void minimum(sum obj,sum1 obj1);
};
Prog -42
//Friend function
#include<iostream.h>
#include<conio.h>
class sum
{
private :
int n;
public :
sum()
{
n=10;
}
friend void display(sum obj);
};
Prog -43
//To accesss the values from two different class using Friend function
#include<iostream.h>
#include<conio.h>
class sum1; //dummy class declaration
class sum
{
private :
int n;
public :
sum()
{
n=10;
}
friend void display(sum obj,sum1 obj1);
};
class sum1
{
private :
int m;
public :
sum1()
{
m=20;
}
friend void display(sum obj,sum1 obj1);
};
Prog -44
//Function Overloading
#include<iostream.h>
#include<conio.h>
class emp
{
int eno;
char ename[10];
float hra,ta,basic_sal,tot;
public:
void get()
{
cout<<"Enter the Employee number :";
cin>>eno;
cout<<"Enter the Employee Name :";
cin>>ename;
}
void get(float h,float t,float b)
{
hra=h;
ta=t;
basic_sal=b;
}
void process()
{
tot=hra+ta+basic_sal;
}
void put()
{
cout<<"Employee Number is :"<<eno<<endl;
cout<<"Employee name is :"<<ename<<endl;
}
void put1()
{
cout<<"Employee Net Salary is :"<<tot<<endl;
}
};
void main()
{
emp e1,e2;
clrscr();
e1.get();
e1.put();
e2.get(200,40,2100);
e2.process();
e2.put1();
getch();
}
Prog -45
#include<iostream.h>
#include<conio.h>
void add(int a,int b)
{
cout<<endl<<(a+b)*2;
}
void add(float x,float y)
{
cout<<endl<<(x+y)*2;
}
void main()
{
clrscr();
int m,n,o;
float d,e;
cout<<"\n Enter three integer values :";
cin>>m>>n>>o;
cout<<"\n Enter two floating values :";
cin>>d>>e;
add(m,n);
add(m,n,o);
add(d,m);
add(d,e);
getch();
}
Prog -46
//Function Overloading-3
#include<iostream.h>
#include<conio.h>
double power(double,int);
int power(int,int);
void main()
{
double m,p;
int n,x;
clrscr();
cout<<"\n Enter the Base value :";
cin>>m;
cout<<"\n Enter the power value:";
cin>>n;
p=power(m,n);
cout<<endl<<"The I exponent value is :"<<p<<endl;
x=power(n,m);
cout<<endl<<"The II exponent value is :"<<x<<endl;
getch();
}
double power(double i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}
int power(int i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}
Prog -47
/* OPERATOR OVERLOADING
Syntax:
returntype operator symbol(arg)
{
};
*/
//Example for unary operator overloading
#include<iostream.h>
#include<conio.h>
class over
{
int count;
public:
void operator ++();
void operator --();
over()
{
count=0;
}
void disp()
{
cout<<"\n count ="<<count;
}
};
void over :: operator ++()
{
count++;
}
void over :: operator --()
{
count--;
}
void main()
{
clrscr();
over obj1,obj2;
obj1.disp();
obj2.disp();
obj1++;
++obj1;
obj2++;
++obj2;
cout<<"\n ";
obj1.disp();
obj2.disp();
obj1--;
--obj2;
cout<<"\n ";
obj1.disp();
obj2.disp();
getch();
}
Prog -48
Prog -50
Prog -51
Prog -52
Syntax:
class_name::function_name;
*/
Prog -53
#include<iostream.h>
#include<conio.h>
class base
{
protected :
int x;
public :
void set1(int a)
{
x=a;
}
};
class derv1 : virtual public base
{
protected :
int y;
public :
void set2(int b)
{
y=b;
}
void show()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Y ="<<y;
}
};
class derv2 : virtual public base
{
protected :
int z;
public :
void set3(int c)
{
z=c;
}
void view()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Z ="<<z;
}
};
class virderv : public derv1,public derv2
{
protected :
int m;
public :
void set4(int n)
{
m=n;
}
void disp()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Y ="<<y;
cout<<"\n Value of Z ="<<z;
cout<<"\n Value of M ="<<m;
show();
view();
}
};
void main()
{
clrscr();
virderv obj1;
obj1.set1(35);
obj1.set2(45);
obj1.set3(55);
obj1.set4(85);
obj1.disp();
getch();
}
Prog -54
*/
/* virtual function :
If a function with same name with same datatype is used in different
classes then it will access by using virtual function concept.*/
Prog -55
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived : public base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n Show derived";
}
};
void main()
{
clrscr();
derived d;
base *bptr;
cout<<"\n bptr points to base";
bptr->display();
bptr->show();
cout<<"\n bptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
/*
d.display();
d.show();*/
getch();
}
Prog -56
#include<iostream.h>
#include<conio.h>
class number
{
private:
int p;
public:
void assign(int q)
{
this->p=q;
}
void display()
{
cout<<endl<<this->p<<"\n";
}
number()
{
cout<<"\n Object created";
}
~number()
{
cout<<"\n Object destroyed";
}
};
void main()
{
clrscr();
number n,m;
n.assign(100);
m.assign(200);
n.display();
m.display();
getch();
}
Prog -57
obj2.address();
obj1.disp();
obj2.disp();
getch();
}
Prog -58
/* Templates :
If we want to access any datatype with same function or class, we can
use template concept.
Types of template:
1. Function template
2. Class template
syntax:
template<class type>
type functionname(type parameter1,type parameter2,...,type parametern)
{
Local declaration;
set of statements;
}
*/
#include<iostream.h>
#include<conio.h>
template<class t1,class t2>
void display(t1 x,t2 y)
{
cout<<"\n x = "<<x<<" "<<"\t y = "<<y;
}
void main()
{
clrscr();
display(1048,"abi");
display(12.25,'A');
display(10,20);
getch();
}
Prog -59
# include<iostream.h>
# include<conio.h>
template <class tmpdt>
tmpdt add(tmpdt a ,tmpdt b)
{
tmpdt temp;
temp = a+b;
cout<<"\n a = "<<a;
cout<<"\n b = "<<b;
return(temp);
}
void main()
{
clrscr();
int i=10,j=20,r1;
float x=10.1, y=30.5,r2;
char a='t', b='w',r3;
cout<<" The original value of i and j is "<<i<<" "<<j<<endl;
cout<<" The original value of x and y is "<<x<<" "<<y<<endl;
cout<<" The original value of a and b is "<<a<<" "<<b<<endl;
r1 = add(i,j);
r2 = add(x,y);
r3 = add(a,b);
cout<<"\n Integer Addition Result = "<<r1<<endl;
cout<<" Float Addition Result = "<<r2<<endl;
cout<<" Character Addition Result = "<<r3<<endl;
getch();
}
Prog -60
Prog -61
/* FILE
-------
header file - #include<fstream.h>
stream - flow of data
------
1. ifstream - for reading text from the file
2. ofstream - for writing text into the file
3. fstream - for both reading and writing from/ into the file
file operations
_______________
1. file open
2. file process
3. file close
Prog -62
Prog -63
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ofstream out("c:\\tc\\test",ios::in||ios::binary);
clrscr();
out<<"Shirts"<<" "<<625<<endl;
out<<"Jeans"<<" "<<760<<endl;
out<<"Trousers"<<" "<<600<<endl;
out.close();
ifstream in("c:\\tc\\test",ios::binary);
char item[20];
float cost;
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";
in.close();
getch();
}
Prog -64
// C++ program to write records into and read records from the file
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char nm[50];
int age;
int idnum;
clrscr();
ofstream out("samp1.txt");
out<<"AAA"<<" "<<33<<" "<<101<<endl;
out<<"BBB"<<" "<<55<<" "<<102<<endl;
out<<"CCC"<<" "<<44<<" "<<103<<endl;
out.close();
cout<<"\n Given content is written into the file successfully";
ifstream in("samp1.txt");
cout<<"\n\n File Consists \n";
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in.close();
getch();
}
Prog -65
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define size 6
void main()
{
char num[size+1];
clrscr();
fstream inout("samp",ios::in|ios::out|ios::binary);
for (int i=0;i<10;i++)
inout<<i;
inout.seekp(2);
char var[20];
cout<<"enter a string:";
cin>>var;
inout>>var;
inout.seekg (4);
inout.read (num,size);
num[size]=0;
cout<<num<<endl;
getch();
}
Prog -66
#include<iostream.h>
#include<conio.h>
class stack
{
int top;
int item[50];
public:
stack()
{
top=0;
}
void push(int data);
void view();
void pop();
};
void main()
{
stack s;
int choice,data;
clrscr();
do
{
cout<<"\n 1. Push ";
cout<<"\n 2. Pop ";
cout<<"\n 3. View";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter the data : ";
cin>>data;
s.push(data);
break;
case 2:
s.pop();
break;
case 3:
s.view();
break;
}
}
while(choice !=4);
}
void stack :: push(int data)
{
if(top == 5)
{
cout<<"\n Stack OverFlow";
}
else
{
item[top] = data;
top = top+1; // top++;
}
}
void stack :: pop()
{
if(top == 0)
{
cout<<"\n Stack is Empty";
}
else
{
top = top -1; // ps->top --;
cout<<"\n pop the data : \n "<<item[top];
}
}
Prog -67
#include<iostream.h>
#include<conio.h>
class queue
{
int front,rear;
int item[50];
public:
queue()
{
front=0;
rear=0;
}
void push(int data);
void view();
void pop();
};
void main()
{
queue q;
int choice,data;
clrscr();
do
{
cout<<"\n 1. Push ";
cout<<"\n 2. Pop ";
cout<<"\n 3. View";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter the data : ";
cin>>data;
q.push(data);
break;
case 2:
q.pop();
break;
case 3:
q.view();
break;
}
}
while(choice !=4);
}
void queue :: push(int data)
{
if(rear-front== 5)
{
cout<<"\n Queue OverFlow";
}
else
{
item[rear] = data;
rear++;
}
}
void queue :: pop()
{
if(front==rear)
{
cout<<"\n Queue is Empty";
front=rear=0;
}
else
{
cout<<"\n pop the data : \n "<<item[front];
front=front+1;
}
}
Prog -68
Prog -69
#include<iostream.h>
#include<conio.h>
class tree;
class tnode
{
friend class tree;
private :
int data;
tnode *lchild;
tnode *rchild;
public :
tnode()
{
data=0;
lchild=0;
rchild=0;
}
tnode(int d)
{
data = d;
lchild=0;
rchild=0;
}
};
class tree
{
private :
tnode *root;
public :
tree()
{
root=0;
}
void insert(int temp);
void display();
void inorder(tnode *p);
void preorder(tnode *p);
void postorder(tnode *p);
};
void tree::insert(int temp)
{
tnode *p = root;
tnode *q = 0;
while(p)
{
q = p;
if (temp == p->data)
return;
else if (temp < p->data)
p = p->lchild;
else
p = p->rchild;
}
tnode *t = new tnode;
t->lchild = 0;
t->rchild=0;
t->data = temp;
if (! root)
root = t;
else if (t->data < q->data)
q->lchild = t;
else
q->rchild = t;
return;
}
void tree::display()
{
cout<<"\n Inorder\n";
inorder(root);
cout<<"\n Preorder\n";
preorder(root);
cout<<"\n Postorder\n";
postorder(root);
}
void tree::inorder(tnode *p)
{
if (p)
{
inorder(p->lchild);
cout<<p->data<<" ";
inorder(p->rchild);
}
}
void tree::preorder(tnode *p)
{
if (p)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
void tree::postorder(tnode *p)
{
if (p)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->data<<" ";
}
}
void main()
{
tree t;
int ch;
clrscr();
do
{
cout<<"\n 1 - Insert";
cout<<"\n 2 - Display";
cout<<"\n 3 - Exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
int n,p;
cout<<"\n Enter the Number of elements : ";
cin>>n;cout<<"\n Enter the Elements : ";
for(int i=0;i<n;i++)
{
cin>>p;
t.insert(p);
}
break;
case 2:
t.display();
break;
case 3:
cout<<"\n Process Over";
break;
default:
cout<<"\n Invalid Choice";
break;
}
getch();
clrscr();
}
while(ch != 3);
getch();
}