AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NH5,BYPASS ROAD,GUDUR.
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
OF
C++ LAB
18CS305
AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
AUDISANKARA
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
C++ LAB
LAB MANUAL
Prepared by: Verified by:
C++ LAB (18CS305)
INDEX
S.No Name of the Experiments
1 Programs on Tokens
Programs on control statements ( if-else, Nested if-else, jump,
2 goto, break,continue, while, do-while, for, switch-case).
3 Programs on Memory management operators.
4 Programs to implement on parameter passing techniques.
5 Programs using inline functions.
6 Programs using function overloading.
7 Programs to implement Access specifiers.
8 Programs on Friend functions.
9 Programs on Templates
10 Programs on Copy Constructor.
11 Programs on Constructors with default arguments.
12 Programs on types of inheritance.
Exp:No:1 Programs on Tokens
Aim: To write C++ programs on tokens.
(1) a) To write a C++ program on variables.
Program
#include<iostream.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
cout<<"enter values of a,b:";
cin >>a >> b;
c=a+b;
cout<<"sum of a&b is:"<<c;
getch();
INPUT
Enter values of a & b : 4 5
OUTPUT
Sum of a &b is 9
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(1) b) To write a C++ program on keywords.
Program
#include<iostream.h>
#include<conio.h>
void main()
int a=5;
float b=2.5;
clrscr();
cout<<"value of a is:"<<a;
cout<<"value of b is:"<<b;
getch();
OUTPUT
Value of a is : 5
Value of b is:2.5
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(1) c) To write a c++ program on constants.
Program
#include<iostream.h>
#include<conio.h>
void main()
const sunday=0;
const monday=1;
int c;
clrscr();
cout<<"enter a day(0 or 1):";
cin>>c;
if(c==sunday&&c!=monday)
cout<<"holiday";
else
cout<<"working day";
getch();
INPUT
Enter a day ( 0 or 1): 0
OUTPUT
Holiday
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(1) d) To write a C++ program on special characters.
Program
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
char ch;
clrscr();
cout<<"enter any character:";
ch=getchar();
if(isalpha(ch))
cout<<"alphabet";
else
if(isdigit(ch))
cout<<"digit";
else
cout<<"special character:";
getch();
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
INPUT
Enter any character: “
OUTPUT
Special character
(1) e) To write a C++ program arithmetic operators
Program
#include<iostream.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
cout<<"enter a,b values:";
cin>>a>>b;
c=a+b;
cout<<"value of c is:"<<c<<endl;
c=a-b;
cout<<"value of c is:"<<c<<endl;
c=a*b;
cout<<"value of c is:"<<c<<endl;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
c=a/b;
cout<<"value of c is:"<<c<<endl;
c=a%b;
cout<<"value of c is:"<<c;
getch();
INPUT
Enter a,b values: 5 3
OUTPUT
Value of c is: 7
Value of c is : 2
Value of c is 15
Value of c is 2.5
Value of c is 2
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:2 Programs on Dynamic Initialization of variables.
Aim: To write C++ program on dynamic initialization of variable.
Program
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
cout<<"enter radius";
int r;
cin>>r;
float area=3.14*r*r;
cout<<"area="<<area;
getch();
INPUT
Enter radius: 2
OUTPUT
Area=12.56
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:3 Programs on control statements
Aim: To write C++ programs on control structures.
(3) a) To write a C++ program on if-else
Program
#include<iostream.h>
#include<conio.h>
void main()
int n;
clrscr();
cout<<"enter n value";
cin>>n;
if(n%2==0)
cout<<n<<"is even";
else
cout<<n<<"is odd";
getch();
INPUT
Enter n value:5
OUTPUT
5 is odd
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(3) b) To write a C++ program on nested if –else
Program
#include<iostream.h>
#include<conio.h>
void main()
int num;
clrscr();
cout<<"enter num";
cin>>num;
if(num>0) {
cout<<"you entered positive number";
else if(num<0)
cout<<"you entered negative number";
else
cout<<"you entered zero";
getch();
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
INPUT
Enter a number -5
OUTPUT
You enter a negative number
(3) c) To write a C++ program on switch case
Program
#include<iostream.h>
#include<conio.h>
void main()
int marks;
clrscr();
cout<<"enter your marks between 0 to 100";
cin>>marks;
switch(marks/10)
case 10:
case 9:
cout<<"your grade:A";
break;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
case 8:
case 7:
cout<<"your grade:B";
break;
case 6:
cout<<"your grade:C";
break;
case 5:
case 4:
cout<<"your gradeP:D";
break;
default:
cout<<"you failed";
getch();
INPUT
Enter your marks b/w 0 to 100: 98
OUTPUT
Your grade A
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(3) d) To write a C++ program on for loop
Program
#include<iostream.h>
#include<conio.h>
void main()
int i,n,fact=1;
cout<<"enter n value";
cin>>n;
for(i=1;i<=n;i++)
fact=fact*i;
cout<<"factorial of"<<n<<"="<<fact;
getch();
INPUT
Enter n value : 5
OUTPUT
Factorial of 5 is 120
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(3) e) To write a C++ program on while loop
Program
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
int val,num,sum=0;
clrscr();
cout<<"enter a value";
cin>>val;
num=val;
while(num!=0)
sum=sum+num%10;
num=num/10;
cout<<"the sum of digits of"<<sum;
getch();
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
INPUT
Enter a number:123
OUTPUT
The sum of digits is: 6
(3) f) To write a C++ program on do-while loop
Program
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
int x=1,y;
clrscr();
cout<<"number and their cubes";
do
y=pow(x,3);
cout<<"\t"<<x<<"\t"<<y<<"\n";
x++;
}while(x<=3);
getch();
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
OUTPUT
Numbers and their cubes
1 1
2 8
3 9
(3) g) To Write a C++ program on break & continue
Program
#include<iostream.h>
#include<conio.h>
void main()
int i;
clrscr();
cout<<"the loop with\'break'\produced the output as:";
for(i=1;i<=10;i++)
if(i%3==0)
break;
else
cout<<i;
cout<<"the loop with\'continue'\produes the output as:";
for(i=1;i<=10;i++)
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
{
if(i%3==0)
continue;
else
cout<<i;
getch();
OUTPUT
The loop with break produce the output as
The loop with continue produce the output as
10
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(3) h) To write a C++ program on goto
Program
#include<iostream.h>
#include<conio.h>
void main()
int x=10;
clrscr();
loop:cout<<x;
x--;
if(x>0)
goto loop;
getch();
}
OUTPUT
10
9
8
7
6
5
4
3
2
1
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:4 To write a C++ program on memory management operators
(4) a) To write a C++ program to allocate memory using new operator and
deallocate by using delete operator .
Program :
#include<iostream.h>
#include<conio.h>
void main( )
clrscr( );
int *p=new int[3],k;
for(k=0; k<3; k++)
cout<<”Enter a number:”;
cin>> *p;
p++;
p - =3;
cout<<” Entered numbers with their address are:”;
for(k=0;k<3;k++)
cout<<”\t”<<*p<<”\t”<<p;
p++;
P - =3;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
delete p;
INPUT :
Enter a number: 7
Enter a number: 8
Enter a number: 9
OUTPUT
Entered numbers with their address are:
7 3658
8 3660
9 3662
(4) b) To write a C++ program to display number of bytes
occupied by char data type.
Program:
#include<iostream.h>
#include<conio.h>
void main( )
clrscr( );
cout<< sizeof(‘a’);
OUTPUT: 1
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(4) c) To write a C++ program to declare an integer, initialize it and display
it.Terminate statement using comma operator.
Program:
#include<iostream.h>
#include<conio.h>
void main( )
int x;
clrscr( ),
x=10,
cout<<”x=”<<x;
cout<<endl;
OUTPUT:
x=10
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(4) d) To write a C++ program to use comma operator in if – else structure as
scope indicator.
Program:
#include<iostream.h>
#include<conio.h>
void main( )
int x;
clrscr( ),
x=10;
if(x==10)
cout<< x,
cout<< x+1,
cout<< x+2,
cout<< “End of if block”;
else
cout<<”false”,
cout<<”end”;
OUTPUT:
10
11
12
End of if block
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:5 To write C++ programs to implement on parameter passing
techniques.
(5) b) To write a C++ program to demonstrate pass by value.
Program:
#include<iostream.h>
#include<constream.h>
void main( )
clrscr( );
int x,y;
void change(int,int);
cout<<” Enter values of x&y:”;
cin>>x>>y;
change(x,y);
cout<< “In function main( )”;
cout<< “ values x=”<<x<<” and y=”<<y;
cout<<” Address x=”<< &x<<” and y=”<<&y;
void change(int a , int b)
int k; k=a; a=b; b=k;
cout<<” In function change( )”;
cout<<” values x=”<<a<<” and y=”<<b;
cout<< “ address x=”<<&a<<”and y=”<<&b;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
}
INPUT:
Enter values of x & y : 5 4
OUTPUT
In function change( )
Values of x=4 and y=5
Address x=4090 and y=4092
In function main( )
Values x=5 and y=4
Address x=4096 and y=4094
(5) b) To write a C++ Program to demonstrate pass by address.
Program:
#include<iostream.h>
#include<constream.h>
void main( )
clrscr( );
int x,y;
void change(int *,int *);
cout<<”Enter values of x & y:”;
cin>>x>>y;
change(&x,&y);
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
cout<<” In main( )”;
cout<<” values x=”<<x<<”and y=”<<y;
cout<< “addresses x=”<<&x<<” and y=”<<&y;
void change( int *a,int *b)
int *k;
*k=*a;
*a=*b;
*b=*k;
cout<<” In change( )”;
cout<<” values x=”<< a <<” and y=”<<b;
INPUT:
Enter values of x & y : 5 4
OUTPUT
In change ( )
Values x=4 and y=5
Addresses x=4096 and y=4094
In main( )
Values x=4 and y=5
Addresses x=4096 and y=4094
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(5) c) To write a C++ program to pass the value of variable by value,
reference ,and display the Result.
Program:
#include<iostream.h>
#include<conio.h>
#include<process.h>
Void main( )
clrscr( );
void funA(int s)
void funB(int &);
void funC(int *);
int s=4;
funA(s);
cout<< “value of s=”<<s<<” address of s:”<<unsigned(&s);
funB(s);
cout<< “value of s=”<<s<<” address of s:”<<unsigned(&s);
funC(s);
cout<< “value of s=”<<s<<” address of s:”<<unsigned(&s);
Void funA( int i)
i++;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Void funB( int &k)
K++;
Void funC( int *j)
++*j;
OUTPUT:
Value of s=4 Address of s: 4096
Value of s=5 Address of s:4096
Value of s=6 Address of s:4096
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:6 To write C++ programs using inline functions.
(6) a) To write a C++ program to calculate square using inline function and macro.
Program:
#include<iostream.h>
#include<constream.h>
#define SQUARE(v) v * v
inline float square( float j)
return (j*j);
void main( )
clrscr( );
int p=3,q=3,r,s;
r= SQUARE(++p);
s= SQUARE(++q);
cout<<” r=” <<r <<”\n”<<”s=”<<s;
OUTPUT:
r=25
s=16
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
(6) b) To write a C++ program to define function cube( ) as inline for
calculating cube.
Program:
#include<iostream.h>
#include<constream.h>
void main( )
clrscr( );
int cube(int);
int j,k,v=5;
j=cube(3);
k=cube(v);
cout<<” cube of j=”<<j;
cout<<” cube of k=”<<k;
inline int cube( int h)
return(h*h*h);
OUTPUT
Cube of j=27
Cube of k=125
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:7 To write C++ programs using function overloading.
Aim: To write C++ program to define overloaded function add( ) for integer and
float and perform the addition.
Program:
#include<iostream.h>
#include<conio.h>
int add ( int , int , int);
float add ( float, float, float);
int main( )
Clrscr( );
float fa,fb,fc,fd;
int ia,ib,ic,id;
cout<< “ enter integer values for ia,ib,ic:”;
cin>> ia>>ib>>ic;
cout<< “ enter float values for fa,fb,fc:”;
cin>> fa>>fb>>fc;
id=add(ia,ib,ic);
cout<<” addition”;<<id;
fd=add(fa,fb,fc);
cout<<” addition”;<<fd;
return 0;
Add( int j, int k, int l)
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
{
Return (j+k+l);
Float add( float a, float b,float c)
Return (a+b+c);
INPUT:
Enter integer values for ia,ib,ic: 1 2 4
Enter float values for fa,fb,fc: 2.2 3.1 4.5
OUTPUT
Addition : 7
Addition : 9.8
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:8 To write C++ programs to implement access specifiers.
Aim: To write C++ programs to implement access specifiers.
Program:
#include<iostream.h>
#include<conio.h>
Struct item
Private
int codeno;
float price;
int qty;
public:
void show ( )
codeno=125;
price=195;
qty=200;
cout<<” code no=”<< codeno;
cout<<”price=”<< price;
cout<<” quantity=”<< qty;
};
int main( )
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Clrscr();
Item one;
One.show();
Return 0;
OUTPUT
Code no= 125
Price=195
Quantity=200
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:9 To write C++ programs to implement access specifiers.
Aim : To write C++ programs on Friend functions.
Program:
#include< iostream.h>
#include<conio.h>
Class ac
private:
char name[15];
int acno;
float bal;
public:
void read( )
Cout<< “ name :”;
Cin>>name;
Cout<<”a/c no:”;
Cin>>acno;
Cout<<”balance”;
Cin>>bal;
Friend void showbal(ac);
};
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Void showbal( ac a)
Cout<<” balance of a/c no”<<a.acno<<”is rs “<< a.bal;
int main( )
ac k;
k.read( );
showbal(k);
return 0;
INPUT:
Name : sai
A/c No : 474
Balance : 40000
OUTPUT
Balance of A/c no .474 is rs.40000
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:10 To write a C++ program on copy constructor.
Aim: To write a C++ program on copy constructor.
Program
#include<iostream.h>
#include<conio.h>
Class num
int n;
public:
num ( )
num (int k)
n=k;
num ( num &j)
n=j.n;
Void show(void)
Cout<<n;
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
};
main( )
Clrscr();
Num J(50);
Num K(J);
Num L=J;
Num M;
M=J;
Cout<<” object J value of n:”;
J.show();
Cout<<” object Kvalue of n:”;
K.show();
Cout<<” object L value of n:”;
L.show();
Cout<<” object M value of n:”;
M.show();
return 0;
OUTPUT:
object J value of n:50
object K value of n:50
object L value of n:50
object M value of n:50
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:11 To write a C++ program on constructor with default arguments.
Program
#include<iostream.h>
#include<conio.h>
#include<math.h>
Class power
Private:
int num;
int power;
in ans;
public:
power(int n=9,int p=3);
void show( )
Cout<<num<<”raise to”<<power <<”is””<<ans;
};
power::power( int n, int p)
num=n;
power=p;
ans=pow(n,p);
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
main( )
Clrscr();
class power p1,p2(5);
p1.show();
p2.show();
return 0;
OUTPUT
9 raise to 3 is 729
5 raise to 3 is 125
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
Exp:No:12 To write a C++ program on inheritance.
Program
#include<iostream>
#include<conio.h>
class staff
private:
char name[50];
int code;
public:
void getdata();
void display();
};
class typist: public staff
private:
int speed;
public:
void getdata();
void display();
};
void staff::getdata()
cout<<"Name:";
gets(name);
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
cout<<"Code:";
cin>>code;
void staff::display()
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
void typist::getdata()
cout<<"Speed:";
cin>>speed;
void typist::display()
cout<<"Speed:"<<speed<<endl;
int main()
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL
getch();
return 0;
}
OUTPUT
Enter data
Name:Roger Taylor
Code:13
Speed:46
Display data
Name:Roger Taylor
Code:13
Speed:46
Audisankara College of Engg & Technology(Autonomous). C++ LAB MANUAL