Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

PILATHARA CO-OPERATIVE

ARTS & SCIENCE COLLEGE

PILATHARA KANNUR- 670504


(AFFILIATED TO KANNUR UNIVERSITY)

PRACTICAL RECORD

PROGRAMMING IN
C++
LANGUAGE
NAME : ..........................................
REG.NO : ..........................................
SEMESTER : ..........................................
SUBJECT : ..........................................
PILATHARA CO-OPERATIVE ARTS &
SCIENCE COLLEGE

PILATHARA KANNUR- 670504


(AFFILIATED TO KANNUR UNIVERSITY)
PRACTICAL RECORD
CERTIFICATE
CERTIFIED THAT THIS IS A BONAFIDE RECORD OF THE ORIGINAL WORK
DONE
BY...................................................................................................................
REG.NO...................................OF FIRST BCA IN THE C++ PROGRAMMING
DURING THE YEAR 2022-2025

EXAMINERS: LECTURER IN CHARGE:

1.

2.
INDEX
SL NAME OF THE PROGRAM PAGE
NO: NO:
1 ADD ONE DAY TO GIVEN DATE 4
2 TRACE AND TRANSPOSE 8

3 CLASS TIME 12

4 INLINE FUNCTION 14

5 FUNCTION OVERLOADING 17

6 ADD THE ARRAY ELEMENTS 19

7 OPERATOR OVERLOADING (UNARY) 22

8 OPERATOR OVERLOADING (BINARY) 24

9 ADD/SUBTRACT/MULTIPLICAION OF COMPLEX NUMBER 26

10 INHERITANCE 28

11 ARRAY OF POINTERS 32

12 FRIEND FUNCTION 35

13 OBJECT POINTERS 38

14 BANKING 41

15 VIRTUAL FUNCTION 45

3
PROGRAM-1
Aim:
Program to add one date to a given date.
Program:
#include<iostream>
using namespace std;
class date
{
int d,m,y;
public:
void read_day();
void next_day();
};
void date::read_day()
{
cout<<"enter date";
cin>>d>>m>>y;
}
void date::next_day()
{
if(d>0&&d<28)
{
d=d+1;
}
if(d==28)
{
if(m==2)
{
if((y%400==0)||(y%100==0)||(y%4==0))
{
d=29;
}

4
else
{
d=1;
m=3;
}
}
else
{
d=d+1;
}
}
else if(d==29)
{
if(m==2)
{
d=1;
m=3;
}
else
{
d=29;
};
}
if(d==30)
{
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
d=d+1;
}
else
{
d=1;
m=m+1;
}

5
}
if(d==31)
{
d=1;
if(m==12)
{
y=y+1;
m=1;
}
else
{
m=m+1;
}
}
cout<<"tomorrow's date:\n";
if(d<10)
{
cout<<"0"<<d<<" ";
}
else
{
cout<<d<<" ";
}
if(m<10)
{
cout<<"0"<<m<<" ";
}
else
{
cout<<m<<" ";
}
cout<<y;
}

6
int main()
{
date s;
s.read_day();
s.next_day();
return 0;
}

Output:
enter date31
08
2023
tomorrow's date:
01 09 2023

7
PROGRAM-2
Aim:
Program to find trace and transpose of a matrix
Program:
#include<iostream>
using namespace std;
class matrix
{
int r,c,i,j,a[5][5],s,t;
public:
void read()
{
cout<<"Enter the row and column of matrix";
cin>>r>>c;
cout<<"Enter the array elements";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
void write();
void trans();

8
void trace();
};
void matrix::write()
{
cout<<"The entered matrix is\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void matrix::trans()
{
cout<<"Transpose of matrix is\n";
for(j=0;j<c;j++)
{
for(i=0;i<r;i++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
9
void matrix::trace()
{
s=0;
cout<<"Trace value of a matrix\t";
if(r==c)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i]==a[j])
{
s+=a[i][j];
}
}
}
cout<<s;
}
}
int main()
{
matrix x;
x.read();
x.write();
x.trans();
x.trace();
10
return 0;
}

Output:
Enter the row and column of matrix
2
2
Enter the array elements
11
12
13
14
The entered matrix is
11 12
13 14
Transpose of matrix is
11 13
12 14
Trace value of a matrix 25

11
PROGRAM-3
Aim:
Create a class time comprises hr, min, and sec.as meber data and add() and
display() as member functions. Use constructors to initialise the object.
Write a main function to add two objects, store it in another time object
and diplay the resulant time(constructors)
program:
#include<iostream>
using namespace std;
class Time
{
int hr,min,sec;
public:
Time()
{
}
Time(int h,int m,int s)
{
hr=h;
min=m;
sec=s;
}
Time add(Time x1)
{
Time t;
t.hr=hr+x1.hr;
t.min=min+x1.min;
t.sec=sec+x1.sec;
if(t.sec>60)

12
{
t.min++;
t.sec=t.sec-60;
}
if(t.min>60)
{
t.hr++;
t.min=t.min-60;
}
return(t);
}
void display()
{
cout<<hr<<":"<<min<<":"<<sec<<"\n";
}
};
int main()
{
Time t1(3,20,45),t2(3,40,35),t3;
t3=t1.add(t2);
t1.display();
t2.display();
cout<<"sum=";
t3.display();
}

Output:
3:20:45
3:40:35
sum=7:1:20
13
PROGRAM-4
Aim:
Program to find Biggest, Smallest, Sum and Difference
of two numbers using inline function.
Program:
#include<iostream>
using namespace std;
class value
{
public:
inline int biggest(int x,int y)
{
if(x>y)
{
return(x);
}
else
{
return(y);
}
}
inline int smallest(int x,int y)
{
if(x<y)
{
return(x);
}
else
{

14
return(y);
}
}
inline int sum(int x,int y)
{
return(x+y);
}
inline int diff(int x,int y)
{
return(x-y);
}
};
int main()
{
int x,y;
value v;
cout<<"enter two numbers"<<endl;
cin>>x>>y;
cout<<"biggest number="<<v.biggest(x,y)<<endl;
cout<<"smallest number="<<v.smallest(x,y)<<endl;
cout<<"sum="<<v.sum(x,y)<<endl;
cout<<"diff="<<v.diff(x,y)<<endl;
return(0);
}

Output:
enter two numbers
10
20
biggest number=20
smallest number=10
15
sum=30
diff=-10

16
PROGRAM-5
Aim:
Programm to find the area and volume of respective figures using
function over loading.
program:
#include<iostream>
using namespace std;
class shape
{
public:
void area(int s)
{
int p;
p=s*s;
cout<<"area of square="<<p<<"\n";
}
void volume(int s)
{
int v;
v=s*s*s;
cout<<"volume of square='<<v<<"\n";
}
void area(int l, int b, int h)
{
int p;
p=l*b;
cout<<"area of rectangle="<<p<<"\n";
}
void volume(int l, int b, int h)
{
int v;
v=l*b*h;
cout<<"volume of a rectangle="<<v<<"\n";
}
};
int main()
{

17
int a,b,c,d;
shape s;
cout<<"side of a square\n";
cin>>a;
s.area(a);
s.volume(a);
cout<<"enter the length, bredth and hight of a rectangle\n";
cin>>b>>c>>d;
s.area(b,c);
s.volume(b,c,d);
return 0;
}

Output:
side of a square=2
2
area of square=4
volume of square=8
enter the length, breadth and height of a rectangle
4
5
7
area of a rectangle=20
volume of a rectangle=140

18
PROGRAM-6
Aim:
Program to find the elements of an array to the corresponding elements
of another array.
Program:
#include<iostream>
using namespace std;
class arr_ad
{
int a[10],b[10],c[10],i,n;
public:
void getdata()
{
cout<<"Enter limit"<<endl;
cin>>n;
cout<<"Enter array elements of A"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter array elements of B"<<endl;
for(i=0;i<n;i++)
{
cin>>b[i];
}
}
void sum()
{
for(i=0;i<n;i++)
{
19
c[i]=a[i]+b[i];
}
}
void display()
{
for(i=0;i<n;i++)
{
cout<<c[i]<<endl;
}
}
};
int main()
{
arr_ad a;
a.getdata();
a.sum();
cout<<"The added Array="<<endl;
a.display();
return(0);
}

Output:
Enter limit
2
Enter array elements of A
1
2
Enter array elements of B
3
4
20
The added Array=
4
6

21
PROGRAM-7
Aim:
Program to negate the elements of an array. Use operator overloading
function with the operator - .(operator overloading-unary)
Program:
#include<iostream>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display();
void operator -();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display()
{
cout<<x<<"";
cout<<y<<"";
cout<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{

22
space s;
s.getdata(15,20,25);
cout<<"Before negation:\n";
s.display();
-s;
cout<<"After negation:\n";
s.display();
return (0);
}

Output:
Before negation:
15
20
25
After negation:
-15
-20
-25

23
PROGRAM-8
Aim:
Program to compare two strings.Use operator overloading(==).
Do not use any built in functions. (operator overloading-binary)
Program:
#include<iostream>
using namespace std;
class str
{
int i,c;
char s[20];
public:
void getstring()
{
cout<<"Enter a string:";
cin>>s;
}
void operator==(str ob);
};
void str::operator==(str ob)
{
int i=0;c=0;
while(s[i]!='\0'||ob.s[i]!='\0')
{
if(s[i]!=ob.s[i])
{
c++;
}
i++;

24
}
if(c==0)
{
cout<<"\nEqual";
}
else
{
cout<<"\nNot equal";
}
}
int main()
{
str s1,s2;
s1.getstring();
s2.getstring();
s1==s2;
return 0;
}

Output:
Enter a string:APPLE
Enter a string:APPLE
Equal

Enter a string:ORANGE
Enter a string:APPLE
Not equal

25
PROGRAM-9
Aim:
Program for Addition / Subtraction / Multiplication of complex
numbers using classes. (operator overloading)
Program:
#include<iostream>
using namespace std;
class complex
{
int real,img;
public:
void read()
{
cout<<"Enter real and imaginary number";
cin>>real>>img;
}
void display()
{
cout<<"\n\treal="<<real<<"\n\timg="<<img<<"+i";
}
complex operator+(complex c)
{
complex x;
x.real=real+c.real;
x.img=img+c.img;
return(x);
}
};
int main()
{
26
complex c1,c2,c3;
cout<<"\n Enter the value for c1";
c1.read();
cout<<"\n Enter the value of c2";
c2.read();
c3=c1+c2;
cout<<"Sum of 2 complex is given below";
c3.display();
return 0;
}

Out put:
Enter the value for c1Enter real and imaginary number2
3
Enter the value of c2Enter real and imaginary number2
3
Sum of 2 complex is given below
real=4
img=6+i

27
PROGRAM-10
Aim:
Define a class student with name, regno, date of birth and name of college
as member data and functions to get and display these details. Design
another class Test with subjects of study and grade for each subjects
as member data and corresponding input and output functions. Derive
a class result from both Student and Test classes and print the result of
each student with relevant information.
Program:
#include<iostream>
using namespace std;
class test;
class student
{
int dob,regno;
char name[10];
char clgname[30];
public:
void read()
{
cout<<"Enter the name:";
cin>>name;
cout<<"\nEnter the regno:";
cin>>clgname;
cout<<"\nEnter year of birth:";
cin>>dob;
}
void putno()
{
28
cout<<"\nName="<<name;
cout<<"\nRegno="<<regno;
cout<<"\nCollegename="<<clgname;
cout<<"\nYOB="<<dob;
}
};
class test
{
public:
int maths,digital,cpp,english;
void getmark()
{
cout<<"\nEnter the marks of maths:";
cin>>maths;
cout<<"\nEnter the marks of digital:";
cin>>digital;
cout<<"\nEnter the marks of CPP:";
cin>>cpp;
cout<<"\nEnter the marks of english:";
cin>>english;
}
void putmark()
{
cout<<"\nMathematics="<<maths;
cout<<"\nDigital System="<<digital;
cout<<"\nCPP="<<cpp;
cout<<"\nEnglish="<<english;
}
};
29
class result:public student,public test
{
int total;
public:
void display()
{
total=maths+digital+cpp+english;
cout<<"\nTotal mark="<<total<<"\n";
}
};
int main()
{
result r;
r.read();
r.getmark();
r.putmark();
r.display();
return 0;
}

Output:
Enter the name:radha
Enter the regno:102
Enter year of birth:2002
Enter the marks of maths:40
Enter the marks of digital:36
Enter the marks of CPP:29
Enter the marks of english:35

Mathematics=40
30
Digital System=36
CPP=29
English=35
Total mark=140

31
PROGRAM-11
Aim:
Start with an array of pointer to string representing the days of week.
Provide functions to sort the strings into alphabetical order. Use pointers
(array of pointers)
Program:
#include<iostream>
#include<string.h>
using namespace std;
char *str[10]={"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
class sort_string
{
char *temp;
public:
void display()
{
for(int i=0;i<7;i++)
{
cout<<endl<<str[i];
}
}
void sort()
{
int k;
for(int i=0;i<7;i++)
{
for(int j=i+1;j<7;j++)
{

32
k=strcmp(str[i],str[j]);
if(k>0)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
}
};
int main()
{
sort_string s;
cout<<"ARRAY BEFORE SORTING\n";
s.display();
cout<<"\nARRAY AFTER SORTING IN ALPHABETIC ORDER\n";
s.sort();
s.display();
return(0);
}

Output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
33
ARRAY AFTER SORTING IN ALPHABETIC ORDER
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday

34
PROGRAM-12
Aim:
Design two classes A and B with member data n1 ana n2 respectively.
Set values for the data member. Write a program to increment the
interchange the values of both A and B. Use friend function.
Program:
#include<iostream>
using namespace std;
class B;
class A
{
int n1;
public:
void read()
{
cout<<"enter value";
cin>>n1;
}
void show()
{
cout<<"\nn1 is="<<n1;
}
friend void swap(A&,B&);
};
class B
{
int n2;
public:
void readx()
35
{
cout<<"enter value";
cin>>n2;
}
void showx()
{
cout<<"\nn2 is="<<n2;
}
friend void swap(A&,B&);
};

void swap(A&a,B&b)
{
int t;
t=a.n1;
a.n1=b.n2;
b.n2=t;
}
int main()
{
A s1;
B s2;
s1.read();
s2.readx();
cout<<"before swaping";
s1.show();
s2.showx();
cout<<"\n";
swap(s1,s2);
36
cout<<"after swaping";
s1.show();
s2.showx();
return 0;
}

Output:
Enter value23
Enter value24
Before swaping
n1 is=23
n2 is=14
after swaping
n1 is=14
n2 is=23

37
PROGRAM-13
Aim:
Design a class employee with relevant details. Read the details of n
Employees from the keyboard and write it into File named
'EmpDataFile'. Also read the deatils back from the same file and display.
Use separate functions to write and read into and out of the file.
Program:
#include<iostream>
#include<fstream>
using namespace std;
class employe
{
public:
char name[10];
int id;
float salary;
void getdata()
{
cout<<"Enter The Name,ID,Salary Of Employe"<<endl;
cin>>name>>id>>salary;
}
};
int main()
{
int n,i;
employe e1[5];
cout<<"Enter the number of employees";
cin>>n;
for(i=0;i<n;i++)
{
38
e1[i].getdata();
}
ofstream file1;
file1.open("emp_data.txt",ios::out);
for(i=0;i<n;i++)
{
file1.write((char*)&e1[i],sizeof(e1[i]));
}
file1.close();
ifstream file2;
file2.open("emp_data.txt",ios::in);
file2.seekg(0);
for(i=0;i<n;i++)
{
file2.read((char*)&e1[i],sizeof(e1[i]));
cout<<"Name="<<e1[i].name<<"ID="<<e1[i].id<<"Salary="<<
e1[i].salary<<endl;
}
file2.close();
return(0);
}

Output:
Enter the number of employees2
Enter The Name,ID,Salary Of Employe
devu
12
23000
Enter The Name,ID,Salary Of Employe
devan
13
39
22000
Name=devuID=12Salary=23000
Name=devanID=13Salary=22000

40
PROGRAM-14
Aim:
Define a class to represent a bank account.Include the following members:
 Data Members:
1.Name of the depositor
2.Account number
3.Type of account
4.Balance amount in the account
 Member Functions:
1.To assign initial values
2.To deposit an amount
3.To withdraw an amount after checking the balance
4.To display name and balance
Use appropriate main program.(Application level class program)
Program:
#include<iostream>
using namespace std;
class bank
{
char name[20],type[20];
int accno,balance,amount;
public:
void assign()
{
cout<<"\nEnter the account details\n";
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the account number:";
cin>>accno;
41
cout<<"Enter the account type:";
cin>>type;
balance=1000;
}
void deposit()
{
cout<<"\nEnter the amount to be deposited:";
cin>>amount;
balance=balance+amount;
}
void withdraw()
{
cout<<"\nEnter the amount to be withdraw:";
cin>>amount;
balance=balance-amount;
if(balance<=1000)
{
cout<<"Withdraw not possible..since balance less than 1000\n";
balance+=amount;
}
}
void display()
{
cout<<"Name of account holder:"<<name<<"\naccountno:"<<
accno<<"\nType of account:"<<type<<"\nbalance amount:" <<
balance;
}
};

42
int main()
{
bank b;
int ch;
b.assign();
cout<<"\nMENU\nAccount operations\n"<<"1.Deposit an
amount\n2.Withdraw an amount\n3.Exit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
b.deposit();
break;
case 2:
b.withdraw();
break;
case 3:
cout<<"Exiting....\n";
break;
default:
cout<<"Invalid choice\n";
break;
}
b.display();
return 0;
}

43
Output:
Account details

Enter the name: Anu


Enter the account number: 5443
Enter the account type: savings

MENU
Account operations
1.Deposit an amount
2.Withdraw an amount
3.Exit

Enter your choice:1


Enter the amount to be deposited:1000
Name of account holder: Anu
accountno:5443
Type of account: savings
balance amount:2000

MENU
Account operations
1.Deposit an amount
2.Withdraw an amount
3.Exit

Enter your choice:2


Enter the amount to be withdraw: 2000
Withdraw not possible..since balance less than 1000
Name of account holder: Anu
44
accountno:5443
Type of account: savings
Balance amount:2000

MENU
Account operations
1.Deposit an amount
2.Withdraw an amount
3.Exit

Enter your choice:3


Exiting...

45
PROGRAM-15
Aim:
Create a base class called shape. Use this class to store double type values
that could be used to compute the area of figures. Derive two specific
classes called TRIANGLE and RECTANGLE from the base SHAPE.
Add to the base class, a member function get_data() to initialize
base class data members and another member function display_area()
to compute and display the area of figures. Make display_area() as a
virtual function and redefine this function in the derived class to
suite the requirments(virtual functions)
Program:
#include<iostream>
using namespace std;
class shape
{
public:
double b,h;
void read()
{
cout<<"enter the height and breadth";
cin>>b>>h;
}
virtual void display_area()=0;
};
class triangle : public shape
{
public:
void display_area()
{

46
cout<<"area="<<0.5*b*h;
}
};
class rectangle : public shape
{
public:
void display_area()
{
cout<<"area="<<b*h;
}
};
int main()
{
triangle t;
t.read();
t.display_area();
rectangle r;
r.display_area();
return(0);
}

Output:
area=66
area=0

47

You might also like