You are on page 1of 57

PRACTICAL FILE

OF

PROGRAMMING
IN
C++

SUBMITTED TO:

SUBMITTED BY:

MR. SANDEEP CHOPRA

JAHANVI ALAGH

ASSISTANT PROFESSOR

B.Sc. IT IIND SEM

INDEX
Program for demonstration of function
Program to short the element of array
Program for swapping integer by passing values
Program for swapping integer by passing address
Demonstration of inline functions
Function with default argument
Demonstration of pointer to function
Program for function Overloading
Program for swapping integer using function Overloading
Accessing member function with in a class
Demonstration of data hiding
Demonstration of Friend function
Program for defining member function inside a class
Program for defining member function outside of class
Program for defining member function outside as inline35
Demonstration of static member function of a class
Program to implement a Constructor
Program to demonstration of Parameterized constructor
Demonstration Constructor together with Destructor
Program for overloading a Constructor
Demonstration of Constructor with default argument
Program to implement copy constructor
Program for Operator Overloading

Program for overloading comparison operator


Program for overloading new and delete operator

Program for demonstration of function

#include<iostream.h>
#include<conio.h>
int max(int x, int y); //prototype
void main()
{
int a,b,c;
clrscr();
cout<<"Enter two integer<a,b>";
cin>>a>>b;
c=max(a,b);
cout<<"Max(a,b): "<<c<<endl;
getch();
}
int max(int x, int y) //Function defination
{
if(x>y)
return x;
else
return y;
}

OUTPUT:Enter two integer<a,b>4


5

Max(a,b):5
12

Program to short the element of array


#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
}
void bubblesort( int *a, int size)
{
char swapped = 'T';
for(int i=0; (i<size-1)&&(swapped=='T');i++)
{
swapped='F';
for(int j=0;j<(size-1)-i;j++)
{
if(a[j]>a[j+1])
{
swapped='T';
swap(a[j],a[j+1]);
}
}
}
}

void main(void)
{
13
int a[25];
int i,size;
clrscr();
cout<<"Program to sort elements ..."<<endl;
cout<<"Enter the the size of integer vector <max-25>:";
cin>>size;
cout<<"Enter the elements of the integer vector...."<<endl;
for(i=0;i<size;i++)
{
cin>>a[i];
}
bubblesort(a,size);
cout<<"Sorted vector:"<<endl;
for(i=0;i<size;i++)
cout<<a[i]<<"";
getch();
}

OUTPUT:Program to short elements . . .


Enter the size of integer vector <max-25>:
Enter the element of integer vector . . . .

15967
Sorted vector:
15679

Program for swaping integer by passing values


#include<iostream.h>
#include<conio.h>
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
cout<<"Now Value of a and b are"<<""<<x<<""<<y<<endl;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter two integer<a,b>";
cin>>a>>b;
swap(a,b);
getch();
}

OUTPUT:Enter two integer<a,b> 5


6
Now value of a and b are 6 5

Program for swaping integer by passing address


#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
cout<<"Now Value of a and b are"<<""<<*x<<""<<*y<<endl;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter two integer<a,b>";
cin>>a>>b;
swap(&a,&b);
getch();
}

OUTPUT:Enter two integer <a, b>8


9
Now the values of a and b are 8 9

Demonstration of inline functions


#include <iostream.h>
inline int Double(int);
int main()
{
int target;
cout <<"Enter a number to work with: ";
cin >> target;
cout <<"\n";
target = Double(target);
cout <<"Target: "<< target << endl;
target = Double(target);
cout <<"Target: "<< target << endl;
target = Double(target);
cout <<"Target: "<< target << endl;
return 0;
}
int Double(int target)
{
return 2*target;
}
OUTPUT:Enter a number to work with:-5
Target: 10
Target: 20
Target: 40

Function with default argument


#include<iostream.h>
#include<conio.h>
void printline(char='-',int=70);
void main()
{
clrscr();
printline(); //uses both default arguments
printline('!'); //assume 2nd argument as default
printline('*',40); //ignores default arguments
printline('U',55); //ignores default arguments
getch();
}
void printline(char ch, int count)
{
int i;
cout<<endl;
for(i=0;i<count;i++)
cout<<ch;
}

Demonstration of pointer to function


#include <iostream.h>
void Square (int&,int&);
void Cube (int&, int&);
void Swap (int&, int &);
void GetVals(int&, int&);
void PrintVals(int, int);
enum BOOL { FALSE, TRUE };
int main()
{
void (* pFunc) (int &, int &);
BOOL fQuit = FALSE;
int valOne=1, valTwo=2;
int choice;
while (fQuit == FALSE)
{
cout <<"(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: ";
cin >> choice;
switch (choice)
{
case 1: pFunc = GetVals; break;
case 2: pFunc = Square; break;
case 3: pFunc = Cube; break;
case 4: pFunc = Swap; break;
default : fQuit = TRUE; break;
}

if (fQuit)
break;
PrintVals(valOne, valTwo);
pFunc(valOne, valTwo);
PrintVals(valOne, valTwo);
}
return 0;
}
void PrintVals(int x, int y)
{
cout <<"x: "<< x <<" y: "<< y << endl;
}
void Square (int & rX, int & rY)
{
rX *= rX;
rY *= rY;
}
void Cube (int & rX, int & rY)
{
int tmp;
tmp = rX;
rX *= rX;
rX = rX * tmp;
tmp = rY;
20
rY *= rY;

rY = rY * tmp;
}
void Swap(int & rX, int & rY)
{
int temp;
temp = rX;
rX = rY;
rY = temp;
}
void GetVals (int & rValOne, int & rValTwo)
{
cout <<"New value for ValOne: ";
cin >> rValOne;
cout <<"New value for ValTwo: ";
cin >> rValTwo;
}

OUTPUT:(0) Quit (1) Change value (2) Square (3) cube (4) swap :1
x: 1 y: 2
New value for valone: 4
New value for valtwo:5
x:4 y:5
(1) Quit (1) Change value (2) Square (3) cube (4) swap :2
x: 4 y:5
x: 16 y: 25

Program for function Overloading


#include <iostream.h>
int Double(int);
long Double(long);
float Double(float);
double Double(double);
int main()
{
int myInt = 6500;
long myLong = 65000;
float myFloat = 6.5F;
double myDouble = 6.5e20;
int doubledInt;
long doubledLong;
float doubledFloat;
double doubledDouble;
cout <<"myInt: "<< myInt <<"\n";
cout <<"myLong: "<< myLong <<"\n";
cout <<"myFloat: "<< myFloat <<"\n";
cout <<"myDouble: "<< myDouble <<"\n";
doubledInt = Double(myInt);
doubledLong = Double(myLong);
doubledFloat = Double(myFloat);
doubledDouble = Double(myDouble);
cout <<"doubledInt: "<< doubledInt <<"\n";
cout <<"doubledLong: "<< doubledLong <<"\n";

cout <<"doubledFloat: "<< doubledFloat <<"\n";


cout <<"doubledDouble: "<< doubledDouble <<"\n";
return 0;
}
int Double(int original)
{
cout <<"In Double(int)\n";
return 2 * original;
}
long Double(long original)
{
cout <<"In Double(long)\n";
return 2 * original;
}
float Double(float original)
{
cout <<"In Double(float)\n";
return 2 * original;
}
double Double(double original)
{
cout <<"In Double(double)\n";
return 2 * original;
}

OUTPUT:myInt: 6500
myLong: 65000
myFloat: 6.5
myDouble: 6.5e+20
inDouble(int)
inDouble(long)
inDouble(float)
inDouble(double)
DoubleInt: 13000
Doublelong: 130000
Doublefloat: 13
Doubledouble: 1.3e+21

Program for swapping integer using function Overloading


#include<iostream.h>
#include<conio.h>
void swap( char &x,char &y)
{
char t;
t=x;
x=y;
y=t;
}
void swap( int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
}
void swap( float &x,float &y)
{
float t;
t=x;
x=y;
y=t;
}
void main()
{

clrscr();
char ch1,ch2;
cout<<"Enter two chatacter <ch1,ch2>:";
cin>>ch1>>ch2;
swap(ch1, ch2); //Copiler calls swap(char &x, char &y)
cout<<"After swapping<ch1,ch2>:"<<""<<ch1<<""<<ch2<<endl;
int x,y;
cout<<"Enter two integer <x,y>:";
cin>>x>>y;
swap(x,y); //Copiler calls swap(int &x, int &y)
cout<<"After swapping<x,y>:"<<""<<x<<""<<y<<endl;
float c,d;
cout<<"Enter two Floats <c,d>:";
cin>>c>>d;
swap(c, d); //Copiler calls swap(float &x, float &y)
cout<<"After swapping<c,d>:"<<""<<c<<""<<d<<endl;
getch();
}

OUTPUT:Enter two character <ch1, ch2>: g


H
After swapping <ch1, ch2>: h g
Enter two integer <x, y>: 7
3

After swapping <x, y>: 3 7


Enter two float <c, d>: 4.5
6.9
After swapping <c, d>: 6.9 4.5

Acessing member function with in a class


#include<iostream.h>
#include<conio.h>
class numberpairs
{
int num1,num2; //Private by default
public:
void read()
{
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
}
int max() //member function
{
if(num1>num2)
{return num1;}
else
{ return num2;}
}
//Nesting of member function
void showmax()
{
cout<<"maximum="<<max();
}

};
void main()
{
clrscr();
numberpairs n1;
n1.read();
n1.showmax();
getch();
}

OUTPUT:Enter first number: 8


Enter second number: 9
maximum: 9

Demonstration of data hiding


#include<iostream.h>
#include<conio.h>
class part
{
private: //private members
int modelnum;
int partnum;
float cost;
public:
void setpart(int mn,int pn, float c)
{
modelnum=mn;
partnum=pn;
cost=c;
}
void showpart()
{
cout<<"Model:"<<modelnum<<endl;
cout<<"Number:"<<partnum<<endl;
cout<<"Cost:"<<cost<<endl;
}
};
void main()
{
part p1,p2;

clrscr();
//values passed to object
p1.setpart(1996,23,1250.55);
p2.setpart(2000,243,2354.75);
//each object display their value
cout<<"First part details....."<<endl;
p1.showpart();
cout<<"Second part details...."<<endl;
p2.showpart();
getch();
}

OUTPUT:First part details.....


Model: 1996
Number: 23
Cost: 1250.550049
Second part details....
Model: 2000
Number: 243
Cost: 2354.75

Demonstration of Friend function


#include<iostream.h>
#include<conio.h>
class two; //Advance declaration like function prototype
class one
{
private:
int data1;
public:
void setdata(int init)
{
data1=init;
}
friend int add_both(one a, two b); //Friend function
};
class two
{
private:
int data2;
public:
void setdata( int init)
{
data2=init;
}
friend int add_both(one a, two b); //Friend function
};

// Friend function of class one and two


int add_both( one a ,two b)
{
return a.data1+b.data2;
}
void main()
{
one a;
two b;
clrscr();
a.setdata(5);
b.setdata(10);
cout<<"sum of one and two:"<<add_both(a,b);
getch();
}

OUTPUT:sum of one and two: 15

Program for defining member function inside a class


#include<iostream.h>
#include<conio.h>
class date
{
private:
int day;
int month;
int year;
public:
void set(int dayin, int monthin, int yearin)
{
day=dayin;
month=monthin;
year=yearin;
}
void show()
{
cout<<day<<"-"<<month<<"-"<<year<<endl;
}
};
void main()
{
date d1,d2,d3; //date object creation
clrscr();
// Set date of birth

d1.set(26,2,1990);
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}

OUTPUT:Birthdate of the first author: 26-2-1990


Birthdate of the second author: 14-4-1971
Birthdate of the third author: 1-9-1733
33

Program for defining member function outside of class


#include<iostream.h>
#include<conio.h>
class date
{
private:
int day;
int month;
int year;
public:
void set(int dayin, int monthin, int yearin); // Declaration
void show(); //Declaration
};
void date::set(int dayin, int monthin, int yearin)
{
day=dayin;
month=monthin;
year=yearin;
}
void date::show()
{
cout<<day<<"-"<<month<<"-"<<year<<endl;
}
void main()
{
date d1,d2,d3; //date object creation

clrscr();
// Set date of birth
d1.set(26,2,1990);
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}

OUTPUT:Birthdate of the first author: 26-2-1990


Birthdate of the second author: 14-4-1971
Birthdate of the third author: 1-9-1733

Program for defining member function outside as inline


#include<iostream.h>
#include<conio.h>
class date
{
private:
int day;
int month;
int year;
public:
void set(int dayin, int monthin, int yearin); // Declaration
void show(); //Declaration
};
inline void date::set(int dayin, int monthin, int yearin)
{
day=dayin;
month=monthin;
year=yearin;
}
inline void date::show()
{
cout<<day<<"-"<<month<<"-"<<year<<endl;
}
void main()
{
date d1,d2,d3; //date object creation

clrscr();
// Set date of birth
d1.set(26,2,1990);
36
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}

OUTPUT:Birthdate of the first author: 26-2-1990


Birthdate of the second author: 14-4-1971
Birthdate of the third author: 1-9-1733

Demonstration of static member function of a class


#include<iostream.h>
#include<conio.h>
#include<string.h>
class directory
{
public:
////The static string
static char path[]; //declaration
static void setpath(char const *newpath)
{
strcpy(path,newpath);
}
};
/* The static function
void directory::setpath(char const *newpath);
{
strcpy(path,newpath);
} */
// Defination of static variable
char directory::path[199]="/usr/udit";
void main()
{
clrscr();
//static data member acess, which is defined as public
cout<<"path"<<directory::path<<endl;

38
//Alternative calling setpath without an object of the class directory
directory::setpath("/usr");
cout<<"path:"<<directory::path<<endl;
//calling with object
directory dir;
dir.setpath("/etc" );
cout<<"path:"<<dir.path;
getch();
}

OUTPUT:path/usr/udit
path/usr
path/etc

Program to implement a Constructor


#include<iostream.h>
#include<conio.h>
const int MAX_ITEMS=25; // maximum number of items that a bag can hold
class bag
{
private:
int contents[MAX_ITEMS]; //bag memory area
int itemcount; // Number of item present in bag
public:
// set itemcount to empty
bag() // constructor
{
itemcount=0;
}
void put( int item)
{
contents[ itemcount++]=item;
}
void show();
};
// Display the contents of bag
void bag::show()
{
for(int i=0; i<itemcount;i++)
cout<<contents[i]<<"";

cout<<endl;
}
void main()
{
int item;
bag bag;
clrscr();
while(1)
{
cout<<"Enter item number<0-no item>:";
cin>>item;
if(item==0) //item ends,break
break;
bag.put(item);
cout<<"Item in bag:";
bag.show();
}
getch();
}

OUTPUT:Enter item number <0- no item>: 9


Item in bag: 9
Enter item number <0- no item>: 5
Item in bag: 9 5

Program to demonstration of Parameterized constructor


#include<iostream.h>
#include<conio.h>
const int MAX_ITEMS=25;
class bag
{
private:
int contents[MAX_ITEMS];
int itemcount;
public:
bag() // Constructor
{
itemcount=0;
}
bag(int item) // Parameterized constructor
{
contents[0]=item;
itemcount=1;
}
void put (int item)
{
contents[itemcount++]=item;
}
void show();
};
void bag::show()

{
if(itemcount)
for(int i=0; i<itemcount; i++)
cout<<contents[i]<<"";
else
cout<<"Nil";
cout<<endl;
}
void main()
{
int item;
bag bag1;
bag bag2=4;
clrscr();
cout<<"Gifted bag 1 initially has:";
bag1.show();
cout<<"Gifted bag2 initially has:";
bag2.show();
while(1)
{
cout<<"Enter Item number:";
cin>>item;
if(item==0)
break;
bag2.put(item);
cout<<"Item in bag2:";

bag2.show();
}
getch();
}

OUTPUT:Gifted bag 1 initially has: Nil


Gifted bag 2 initially has: 4
Enter item number: 9
Item in bag 2: 4 9

Demonstration Constructor together with Destructor


#include<iostream.h>
#include<conio.h>
int nobjects =0;
int nobj_alive=0;
class myclass
{
public:
myclass() // Constructor
{
++nobjects;
++nobj_alive;
}
~myclass() //Distructor
{
--nobj_alive;
}
void show()
{
cout<<" Total number of object created:"<<nobjects<<endl;
cout<<" Number of object currently alive:"<<nobj_alive<<endl;
}
};
void main()
{
myclass obj1;

clrscr();
obj1.show();
{ //new block
myclass obj1,obj2;
obj2.show();
getch();
}

OUTPUT:Total number of object created: 1


Number of object currently alive: 1
Total number of object created: 3
Number of object currently alive: 3
Total number of object created: 3
Number of object currently alive: 1
Total number of object created: 5
Number of object currently alive: 3

Program for overloading a Constructor


#include<iostream.h>
#include<conio.h>
class AccClass
{
private:
int accno;
float balance;
public:
AccClass() //Constructor no 1
{
cout<<"Enter the account number for acc1 object:";
cin>>accno;
cout<<"Enter the balance";
cin>>balance;
}
AccClass(int an) //Constructor no 2
{
accno=an;
balance=0.0;
}
AccClass(int acval, float bal) //Constructor no 3
{
accno=acval;
balance=bal;
}

void display()
{
cout<<"Account number is :"<<accno<<endl;
cout<<"Balance is:"<<balance<<endl;
}
void moneytransfer(AccClass &acc,float amount);
};
void AccClass::moneytransfer(AccClass &acc,float amount)
{
balance=balance-amount;
acc.balance=acc.balance+amount;
}
void main()
{
int trans_money;
clrscr();
AccClass acc1;
AccClass acc2(10);
AccClass acc3(20,750.5);
cout<<"Account Information......"<<endl;
acc1.display();
acc2.display();
acc3.display();
cout<<"How much money to be transferd from acc3 to acc1:";
cin>>trans_money;
acc3.moneytransfer(acc1,trans_money);

cout<<"Updated Information about account...."<<endl;


acc1.display();
acc2.display();
acc3.display();
getch();
}

OUTPUT:Enter the account number for acc1 object: 456


Enter the balance: 4509.5
Account Information......
Account number is : 456
Balance is: 4509.5
Account number is : 10
Balance is: 0
Account number is : 20
Balance is: 750.5
How much money to be transferd from acc3 to acc1:750
Updated Information about account....
Account number is : 456
Balance is: 5259.5
Account number is : 10
Balance is: 0
Account number is : 20
Balance is: 0.5
48

Demonstration of Constructor with default argument


#include<iostream.h>
#include<conio.h>
#include<math.h>
class complex
{
private:
float real;
float imag;
public:
complex() //constructor 1
{
real=imag=0.0;
}
complex(float real_in,float imag_in=0.0) //constructor with default argument
{
real=real_in;
imag=imag_in;
}
void show(char *msg)
{
cout<<msg<<real;
if(imag<0)
cout<<"-i\n";
else
cout<<"+i\n";

cout<<fabs(imag);endl;
}
complex add(complex c2);
};
complex complex::add(complex c2)
{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
void main()
{
clrscr();
complex c1(1.5 , 2.0);
complex c2(2.2);
complex c3;
c1.show("C1=");
c2.show("C2=");
c3=c1.add(c2); //add c1 and c2 assign to c3
c3.show("C3=c1.add(c2);");
getch();
}

OUTPUT:C1= 1.5 + i2

C2= 2.2 + i0
C3= c1.add(c2): 3.7 + i2

Program to implement copy constructor


#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code(){} //constructor
code(int a) // constructor
{
id=a;
}
code(code &x) // Copy constructor
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
void main()
{
clrscr();
code a(100); // Object a is created and initilized
code b(a); // Copy constructor called

code c=a;
51
code d;
d=a;
cout<<"\n id of a:";
a.display();
cout<<"\n id of b:";
b.display();
cout<<"\n id of c:";
c.display();
cout<<"\n id of d:";
d.display();
getch();
}

OUTPUT:id of a: 100
id of b: 100
id of c: 100
id of d: 100

Program for Operator Overloading


#include<iostream.h>
#include<conio.h>
class index
{
private:
int value;
public:
index()
{
value=0;
}
int getindex()
{
return value;
}
void operator++() // Prefix or postfix increment operator
{
value=value+1;
}
};
void main()
{
index idx1, idx2;
clrscr();
//Display index values

cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
// Advance index object with ++ operator
++idx1;
idx2++;
idx2++;
cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
getch();
}

OUTPUT:Index1=0
Index2=0
Index1=1
Index2=2

Program for overloading comparison operator


#include<iostream.h>
#include<conio.h>
enum boolean { FALSE, TRUE };
class index
{
private:
int value;
public:
index()
{
value=0;
}
index( int val)
{
value=val;
}
int getindex()
{
return value;
}
boolean operator < (index idx)
{
return( value < idx.value ? TRUE : FALSE );
}
};

void main()
{
index idx1 =5;
index idx2=10;
clrscr();
cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
if(idx1<idx2)
cout<<"\n Index1 is less than Index2";
else
cout<<"\nIndex1 is not less than Index2";
getch();
}

OUTPUT:Index1=5
Index2=10
Index1 is less then Index2

Program for overloading new and delete operator


#include<iostream.h>
#include<conio.h>
const int ARRAY_SIZE=10;
class vector
{
private:
int *array; // array is dynamically allocatable data member
public:
//overloading of new operator
void * operator new(size_t size)
{
vector * my_vector;
my_vector= ::new vector;
my_vector->array= new int[ARRAY_SIZE];
return my_vector;
}
//Overloading Delete operator
void operator delete( void* vec)
{
vector *my_vect;
my_vect = (vector *) vec;
delete (int *) my_vect->array;
::delete vec;
}
void read();

int sum();
};
void vector::read()
{
for( int i=0;i<ARRAY_SIZE; i++)
{
cout<<"Vector["<<i<<"]=?";
cin>> array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<ARRAY_SIZE;i++)
sum+=array[i];
return sum;
}
void main()
{
clrscr();
vector *my_vector= new vector;
cout<<"Enter Vector data..."<<endl;
my_vector->read();
cout<<"Sum of Vector="<<my_vector->sum();
delete my_vector;
getch();

OUTPUT:Enter Vector data....


Vector[0]=?1
Vector[1]=?2
Vector[2]=?3
Vector[3]=?9
58
Vector[4]=?8
Vector[5]=?5
Vector[6]=?6
Vector[7]=?7
Vector[8]=?9
Vector[9]=?2
Sum of Vector=52

You might also like