You are on page 1of 93

PRACTICAL :- 1

Write program to generate following pattern:


a) * b) 1 c) ABCDEFG d)
1
** 12 ABC EFG 121
** * 123 AB FG 1331
1234 A G 14
641

Created by PK Monika (B.C.A-IInd year).

Coding :-
#include<conio.h>
#include<iostream> using
namespace std; class
pattern
{ int
r,c,p;
char ch;
public:
void p1();
void p2(); void
p3(); void
p4();
};
void pattern::p1()
{ for(r=1;r<4;r+
+)
{
for(c=1;c<6;c++)
{
if(c>=4-r && c<=2+r)

{ cout<<”
*”;
}
else

{ cout<<”
”;
}
}
cout<<”\n”;
} }; void
pattern::p2()
{ for(r=1;r<5;r+
+)
{
for(c=1;c<=r;c++)

{ cout<
<c;
}
cout<<”\n”;
} }; void
pattern::p3() {
int i,j,k,l; int a=0;
int m; l=68;
for(i=68;i>=65;i--)
{
for(j=65;j<=i;j++)
cout<<char(j);
for(k=1;k<a;k++)
cout<<" "; if(i==68||
i==67) l=69;
for(m=l;m<72;m++)
cout<<char(m);
a=a+2; l=l+1;
cout<<endl;
} };
void pattern::p4()
{ int
i,k,a;
a=1;
for(k=1;k<=4;k++)

{ if(a==1
1) a=121;
cout<<a;
a=a*11;
cout<<endl;
}
}; main() { pattern p;
cout<<”First Pattern: \n”;
p.p1();
cout<<”Second Pattern: \n”;
p.p2(); cout<<”Third
Pattern: \n”; p.p3();
cout<<”Fourth Pattern: \n”;
p.p4();
}

Output :-
PRACTICAL :- 2
Write program to display number 1 to 10 in octal, decimal
and hexadecimal system.

Created by PK Monika (B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; class
convert
{
public:
long d[20],o,h, i , n;
void insert();
void hexadecimal();
void decimal();
void octal();
}conv;

void convert :: insert()


{
int opt;
cout<<"Enter your option to display the numbers 1 to 10 in - ";
cout<<"\n"<<"\n"<<"1 - IN DECIMAL ";
cout<<"\n"<<"2 - IN OCTAL "; cout<<"\n"<<"3 -
IN HEXADECIMAL ";
cout<<"\n"<<"Enter your option - ";
cin>>opt;
if (opt ==1)
{
conv.decimal();
}
else if (opt ==2)
{
conv.octal();
}
else if ( opt ==3 )
{
conv.hexadecimal();
}
cout<<"\n"<<"\n"<<"\n";
}

void convert :: decimal()


{
for(i=1;i<=10;i++)
{
cout<<"\n"<<"The number "<<i<<" in decimal - "<<i;
}
}

void convert :: hexadecimal()


{
int z=0;
z=n;
int c , j ;
cout<<"\n"<<"\n"<<"NOTE : TAKE 10=A , 11=B , 12=C , 13=D , 14=E ,
15=F ";
for(i=1;i<=10;i++)
{
n=i; j=0;
for( ; n >= 1;n=(n/16))
{
c=(n%16);
d[j] = c;
j++; }
cout<<"\n"<<"The number "<<i<<" in hexadecimal - ";
if(i<10)
{
for(int k=j-1;k>=0;k--)
{
cout<<d[k];
}
}
else
cout<<'A';
}
}

void convert :: octal()


{ int c , j ;
for(i=1;i<=10;i++)
{
n=i;
j=0;
for( ; n >= 1;n=(n/8))
{
c=(n%8); d[j]
= c; j++;
}

cout<<"\n"<<"The number "<<i<<" in octal - ";


for(int k=j-1;k>=0;k--)
{
cout<<d[k];
}
}
}

int main() {
clrscr();
convert con;
con.insert();
getch(); return
0;
}

Output :-

1 – IN DECIMAL
2 – IN OCTAL
3 – IN HEXADECIMAL
PRACTICAL :- 3
Write a program using function to add, subtract and
multiply two matrices of order 3x3. You have to create one
function for addition, which accepts three array arguments.
First two array arguments are
matrices to add and third matrix is destination where the
resultant of addition of first two matrices is stored in similar
way create functions for matrix subtraction and
multiplication.
Created by PK Monika (B.C.A-IInd year).

Coding:-
#include<conio.h>
#include<iostream> using
namespace std;
void add(int a[][3],int b[][3],int c[][3]);
void sub(int a[][3],int b[][3],int c[][3]);
void mul(int a[][3],int b[][3],int c[][3]); int
i,j,k;
void main()
{
int i,j,a[3][3],b[3][3],c[3][3],d[3][3];
clrscr();
cout<<"Enter elements of first matrix of 3*3 \n"; for(i=0;i<3;i+
+)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
} cout<<"Enter elements of second matrix of 3*3 \
n"; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
} }
add(a,b,c);
cout<<"\nAddition of matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
sub(a,b,c);
cout<<"\nSubtraction of matrix\n"; for(i=0;i<3;i+
+)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
mul(a,b,c);
cout<<"\nMultiplication of matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
getch();
}

void add(int a[][3],int b[][3], int d[][3])


{ for(i=0;i<3;i+
+)
{ for(j=0;j<3;j+
+)
{
d[i][j]=a[i][j]+b[i][j];
}
}
}

void sub(int a[][3],int b[][3], int d[][3])


{ for(i=0;i<3;i+
+)
{ for(j=0;j<3;j+
+)
{
d[i][j]=a[i][j]-b[i][j];
}
}
}
void mul(int a[][3],int b[][3], int d[][3])
{
int i,j,k;
for(i=0;i<3;i++)
{ for(j=0;j<3;j+
+)
{ d[i]
[j]=0;
}
} for(i=0;i<3;i+
+)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
d[i][j]=d[i][j]+a[i][k]*b[k][j];
}
}
}
}

Output :-
PRACTICAL :- 4
Create a single program to perform following tasks without using library
functions:
a) To reverse the string accepted as argument.
b)To count the number of characters in string passed as argument in form of
character array.
c) To copy the one string to other string; passed as arguments in form of
source character array and destination character array without
using library function.
d)To count no. of vowels, consonants in each word of a sentence passed as
argument in form of character array.
Created by PK Monika(B.C.A-IInd year)

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
const int l=20; class
str

{ private
:
char st[l];
int i; public:
void reverse(char s[l])
{
int a=0,j=0;
cout<<"\n\t The reverse string is "; for(i=0;s[i]!='\
0';i++)
{
a=a+1;
}
for(i=a;i>=0;i--)
{
st[j]=s[i];
cout<<st[j];
j++;
}
cout<<"\n\t The no. of characters are "<<a;
}
void copy(char s[l])
{ int j;
cout<<"\n\n\n\t The copied string to destination array ";
for(i=0, j=0;s[i]!='\0';i++,j++)
{
st[j]=s[i];
cout<<st[i];
}

}
void count(char s[l])
{
int v=0,c=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
v=v+1;
else
if(s[i]==' ')
continue;
else
c=c+1;
}
cout<<"\n\t The vowels are "<<v;
cout<<"\n\t The consonants are "<<c;
}
};
void main()
{ str s;
char a[30];
clrscr();
cout<<"Enter a string: ";
cin.getline(a,30); s.reverse(a);
s.copy(a);
s.count(a);
getch();
}
Output :-

PRACTICAL :- 5

Create a class Student having data members to store roll


number, name of student, name of three subjects, max marks,
min marks, obtained marks. Declare an object of class
student. Provide facilities to input data in data members and
display result of student.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
int k; class student
{ privat
e:
long roll_no;
int m_max , m_min , m_obt[3] , total,i,j;
char s_name[20] , sub[3][10] ; public:
void insert();
void display();
};
void student :: insert()
{
cout<<"-: INPUT SECTION :-"<<"\n"<<"\n";
cout<<"Enter the NAME OF THE STUDENT whose marksheet you want to
display - "<<"\n"<<"\n";
cin>>s_name; cout<<"\
n";
cout<<"Enter the ROLL NO of the student - ";
cin>>roll_no; cout<<"\n"<<"\n";
for(i=0;i<3;i++)
{
cout<<"Enter the NAME of subject no "<<i+1<<" - ";
cin>>sub[i];
}
cout<<"\n"<<"Enter the MAXIMUM MARKS in all the subjects - ";
cin>>m_max;
cout<<"\n"<<"Enter the MINIMUM MARKS in all the subjects - ";
cin>>m_min;
for(j=0;j<3;j++)
{
cout<<"Enter the MARKS OBTAINED in subject no "<<j+1<<" - ";
cin>>m_obt[j];
}}
void student :: display()
{ double
p; int f=0
; total =
0;
cout<<" MARKSHEET";
cout<<"\n"<<"\n"<<"\n"<<"\n"; cout<<"NAME
OF STUDENT - "<<s_name; cout<<"\n"<<"\
n"<<"ROLL NO - "<<roll_no;
cout<<"\n"<<"\n"<<"\n"<<"S.NO "<<" SUBJECT "<<" MAX.MARKS
"<<" MIN. MARKS "<<"MARKS OBTAINED"; for(i=0;i<3;i+
+)
{
cout<<"\n"<<" "<<i+1<<" "<<sub[i]<<" "<<m_max<<"
"<<m_min<<" "<<m_obt[i];
total = total + m_obt[i];
if(m_obt[i] < m_min)

{ f+
+;
}
}
p = (total/3);

cout<<"\n"<<"\n";
if(total >= (m_min*3) && f==0)
{
cout<<"\n"<<"TOTAL MARKS - "<<total; cout<<"\n"<<"\
n"<<"RESULT - PASSED ";
cout<<"\n"<<"\n"<<"PERCENTAGE - "<<p<<"%";
}
else if ( (total > (m_min*3)) && (f!=0) )
{
cout<<"TOTAL MARKS - "<<total;
cout<<"\n"<<"RESULT - FAILED ";
}
else if (total < (m_min*3) )
{
cout<<"TOTAL MARKS - "<<total;
cout<<"\n"<<"RESULT - FAILED ";
}
}

int main()
{ student
s;
s.insert();
s.display();
getch(); return
0;
}

Output :-
PRACTICAL :- 6

Create a class Student having data members to store roll


number, name of student, name of three subjects, max
marks, min marks, obtained marks. Declare array of object
to hold data of 3 students. Provide facilities to display result
of all students. Provide also facility to display result of
specific student whose roll number is given.
Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>

#include<iostream>

using namespace std; class student

int i;
int min[3],max[3],obt[3],tot[3];

char sname[20],subname[3][20];

public: int rl;

void read(); void

display_line(); void

display_result(int);

}; void

student::read()

cout<<"Enter Roll no of student : ";

cin>>rl;

cout<<"Enter name of student : ";

cin>>sname;

cout<<"Enter record of 3 Subjects \n"; for(i=0;i<3;i+

+)

{
cout<<"Enter name of subject "<<i+1<<" : ";

cin>>subname[i];

cout<<"\tMax Marks for "<<subname[i]<<" : ";

cin>>max[i];

cout<<"\tMin Marks for "<<subname[i]<<" : ";

cin>>min[i];

cout<<"\tObtain Marks for "<<subname[i]<<" : ";

cin>>obt[i];

}}

void student::display_result(int rn)


{ if(rn==rl

cout<<"\n\t>>>>>>>>>>>>>>> RESULT OF STUDENT <<<<<<<<<<<<<<<\n\n";

display_line();

cout<<"Roll No : "<<rl<<"\t\t\tName : "<<sname<<"\n";

display_line();
cout<<"Subject Name\tMax Marks\tMin Marks\tObtain marks\n";

display_line(); for(i=0;i<3;i++)

cout<<subname[i]<<"\t\t"<<max[i]<<"\t\t"<<min[i]<<"\t\t"<<obt[ i]<<"\t\t\n";

display_line();

}}

void student::display_line()

cout<<"---------------------------------------------------------\n";

} main()

student s[3];

int i,ch,rl;

cout<<"\t\t\

tEnter Records
of 3 Students :";

for(i=0;i<3;i++)

cout<<"\nEnter Records of Student "<<i+1<<" : \n\n";

s[i].read();

cout<<"\nIf you want to Print Result of all Sudents Press 1\n";

cout<<"and if you want to Print Result of Specific student Press 2\n";

cin>>ch; if(ch==1)

{ for(i=0;i<3;i++)

s[i].display_result(s[i].rl);

} else

if(ch==2)

cout<<"Enter Roll number of student : ";

cin>>rl; for(i=0;i<3;i++)

s[i].display_result(rl);
}

getch();

Output :-
PRACTICAL :- 7
Create a class Sarray having an array of integers having 5 elements
as data member provide following facilities:
a)Constructor to get number in array elements.
b)Sort the elements.
c) Find largest element.
b) Search for presence of particular value in array element.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
class sarray
{ public
:
int
a[5],i,j,temp,num;
public: sarray()
{
cout<<"Enter 5 elements of array : \
n"; for(i=0;i<5;i++) cin>>a[i]; }
void sort();
void search();
void display(); };
void
sarray::sort()
{ for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
} } void
sarray::search()
{ cout<<"Enter item to be searched :
"; cin>>num;
for(i=0;i<5;i++)

{ if(num==a[i]
)
{
cout<<"Item found at Location "<<i+1;
break;
}
}
if(i==5)
cout<<"Entered Item is not present in array...";
} void
sarray::display()
{
cout<<"\nSorted elements are : \n";
for(i=0;i<5;i++) cout<<a[i]<<"\t";
} main()
{ sarray
s;
int ch;
cout<<"\nEnter you choice : \n";
cout<<"1. Sort the Element\n";
cout<<"2. Find the Largest\n";
cout<<"3. Search for presence of particular value in array elements...\n";
cin>>ch;
if(ch==1)
{
s.sort();

s.display(); }
else if(ch==2)
{
s.sort();
cout<<"Largest element in the array is : "<<s.a[4];
} else
if(ch==3)
s.search();

else
cout<<"You Select a Wrong Choice...!!!";
}
Output :-
PRACTICAL :- 8
Write program to create class complex having data members to store real
and imaginary part. Provide following facilities:
a)Add two complex no. using objects.
b)Subtract two complexes no. using objects.
c) Multiply two complexes no. using objects.
d)Divide two complex no. using objects.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<stdio.h>
#include<iostream> using
namespace std; class
complex
{ int real,
image; public:
void get()
{
cout<<"\nEnter Real no.: ";
cin>>real;
cout<<"\nEnter Imaginary no.: ";
cin>>image;
}

complex add(complex c2)


{
complex c3;
c3.real=real+c2.real;
c3.image=image+c2.image;
return c3;
}

complex subtract(complex c2)


{
complex c3; c3.real=real-
c2.real;
c3.image=image-c2.image;
return c3;
}

complex multiply(complex c2)


{
complex c3;
c3.real=real*c2.real;
c3.image=image*c2.image;
return c3;
}

complex division(complex c2)


{
complex c3;
c3.real=real/c2.real;
c3.image=image/c2.image;
return c3;
}
void show3()
{
cout<<"\nMultiplication is: "<<real<<" * "<<image<<"i\n";
}
void show4()
{
cout<<"\nDivision is: "<<real<<" / "<<image<<"i\n";
}
void show1()
{
cout<<"\nAddition is: "<<real<<" + "<<image<<"i\n";
}
void show2()
{
cout<<"\nSubtraction is: "<<real<<" - "<<image<<"i\n";
}
}; main()
{ complex
c1,c2,c3;
cout<<"\nEnter First Complex Number: \n";
c1.get();
cout<<"\nEnter Second Complex Number: \n";
c2.get(); c3=c1.add(c2); c3.show1();
c3=c1.subtract(c2); c3.show2();
c3=c1.multiply(c2); c3.show3();
c3=c1.division(c2); c3.show4(); getch();
}

Output:-

PRACTICAL :- 9

Create class Polar having data members radius and angle. It contains
member functions for taking input in data members and member
function for displaying value of data members. Class Polar contains
declaration of friend function add which accepts two objects of class
Polar and returns object of class Polar after addition. Test the class using
main function and objects of class Polar.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; class
polar
{ public
:
int radius,angle; void
read(); friend polar add(polar
polar); void display(polar);
}; void
polar::read()
{ cout<<"Enter
Radius : ";
cin>>radius;
cout<<"Enter
Angle : ";
cin>>angle;
} polar add(polar p1, polar p2)
{ polar p3;
p3.radius=p1.radius+p2.radius;
p3.angle=p1.angle+p2.angle;
return p3; } void
polar::display(polar p)
{ cout<<"Sum of Radius is : "<<p.radius<<" & \nSum of Angle is:
"<<p.angle;
} main() { polar
p[3]; p[0].read();
p[1].read();
p[3]=add(p[0],p[1]);
p[0].display(p[3]);
getch();
}
Output :-

PRACTICAL :- 10
Write a program to create class Mother having data member to
store salary of Mother, create another class Father having data
member to store salary of Father. Write a friend function, which
accepts objects of class Mother, and Father and prints Sum of Salary
of Mother and Father objects.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
class father; class
mother
{ long int
msal; public:
mother()
{
cout<<"Enter Mother's Salary:";
cin>>msal;
} friend void add(mother,
father);
}; class
father
{ long int
fsal; public:
father()
{ cout<<"\nEnter Father's
Salary:"; cin>>fsal;
} friend void add(mother,
father);
}; void add(mother m,
father f)
{ cout<<"\nSum Of Salaries is:
"<<m.msal+f.fsal;
}
main()
{ mother
m; father
f;
add(m,f);
getch();
}

Output :-
PRACTICAL :- 11
Create a class Counter having a static data member, which keeps
track of no. of objects created of type Counter. One static member
function must be created to increase value of static data member as
the object is created. One static member function must be created to
decrease value of static data member as the object is destroyed. One
static member function must be created to display the current value
of static data member. Use main function to test the class Counter.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
static int count; class
counter
{ public
:
void increase();
void decrease();
void display();
};
void counter::increase()
{
count++;
cout<<"after increasing value of count="<<count<<endl;
}
void counter::decrease()
{
count--;
cout<<"after decreasing value of count="<<count<<endl;
}
void counter::display()
{
cout<<"current value of count="<<count;
} main()
{ counter c1;
c1.increase();
c1.decrease();
c1.display();
getch();
}

Output :-

PRACTICAL :- 12
Define structure student. Structure student has data members for
storing name, rollno., name of three subjects and marks. Write
member function to store and print data.
Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; struct
student
{ private
:
char name[40],sub1[20],sub2[20],sub3[20];
int rollno,sub1mark,sub2mark,sub3mark;
public:
void get()
{
cout<<"Enter Name: ";
cin.getline(name,40); cout<<"\
nEnter Rollno: "; cin>>rollno;
cout<<"\nEnter Name of Subject 1: ";
cin>>sub1; cout<<"\nEnter Name of
Subject 2: "; cin>>sub2; cout<<"\
nEnter Name of Subject 3: ";
cin>>sub3; cout<<"\nEnter Marks of
Subject 1: "; cin>>sub1mark;
cout<<"\nEnter Marks of Subject 2: ";
cin>>sub2mark; cout<<"\nEnter
Marks of Subject 3: ";
cin>>sub3mark;
}
void show()
{
cout<<"\n\nName is: "<<name<<endl; cout<<"Rollno.
is: "<<rollno<<endl; cout<<"First Subject is:
"<<sub1<<endl; cout<<"Second Subject is:
"<<sub2<<endl; cout<<"Third Subject is: "<<sub3<<endl;
cout<<"Marks Of First Subject is: "<<sub1mark<<endl;
cout<<"Marks Of Second Subject is: "<<sub2mark<<endl;
cout<<"Mark Of Third Subject is: "<<sub3mark<<endl;
}
};
main()
{ struct
student s;
s.get();
s.show();
getch();
}

Output :-
PRACTICAL :- 13

Write a class having name. Calculate that uses static


overloaded function to calculate area of circle, area of
rectangle and area of triangle.

Created by PK Monika(B.C.A-IInd year).


Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
class calculate
{ public
:
static area(int r)
{ cout<<"\nArea Of Circle is:
"<<3.14*r*r;
} static area(int l,int
b)
{
cout<<"\nArea Of Rectangle is: "<<l*b;
}
static area(float b,float a)
{
cout<<"\nArea Of Triangle is: "<<0.5*a*b;
}
}; main() { static int radius,
length,breadth; static float
base, amp; cout<<"Enter
Radius:"; cin>>radius;
cout<<"\nEnter Base:";
cin>>base; cout<<"\nEnter
Amplitude:"; cin>>amp;
cout<<"\nEnter Length:";
cin>>length; cout<<"\nEnter
Breadth:"; cin>>breadth;
calculate::area(radius);
calculate::area(length,breadth);
calculate::area(base,amp);
getch();
}
Output :-

PRACTICAL :- 14
Write a class ArraySort that uses static overloaded function to
sort an array of floats, an array of integers.
Created by PK Monika(B.C.A-IInd year).

Coding:-
#include<conio.h>
#include<iostream> using
namespace std; class
arraysort
{ public: int
n,i,j; float
a[5],temp; void
accept()
{
cout<<"\nEnter how many elements: ";
cin>>n; for(i=0;i<n;i++)
{
cout<<"\nEnter elements: "; cin>>a[i]; }
cout<<"\n----------------Array----------------\n\n";
for(i=0;i<n;i++)
{ cout<<a[i]<<"\
t";
} for(i=0;i<n;i+
+)
{
for(j=0;j<n-i-1;j++)

{ if(a[j]>a[j+1]
)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
} } cout<<"\n------------Sorting
Array------------\n\n"; for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
}; main() {
arraysort r;
r.accept();
getch();

Output :-
PRACTICAL :- 15
Write a program using class, which uses static overloaded
function to swap two integers, two floats methods use reference
variable.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; class
sample
{ public
:
static void swap(int &x,int &y)
{ int
t=0;
t=x;
x=y;
y=t;
}
static void swap(float &x,float &y)
{ float
t=0; t=x;
x=y; y=t;
} };
main()
{ static int a,b; static float
f1,f2; cout<<"Enter two
Integer:\n"; cin>>a>>b;
sample::swap(a,b);
cout<<"\nSwapped integer values are: "<<a<<" and "<<b<<"
"<<endl; cout<<"\nEnter two Float:\n"; cin>>f1>>f2;
sample::swap(f1,f2);
cout<<"\nSwapped Float values are: "<<f1<<" and "<<f2;
getch();
}
Output :-

PRACTICAL :- 16
Create a class Polar having radius and angle as data members. Provide
following facilities: a) Overloaded insertion and extraction operators for
data input and display.
b)Overloaded constructor for initialization of data members.
c) Overloaded operator + to add two polar co-ordinates using objects of class
Polar.

Created by PK Monika(B.C.A-IInd year).

Coding:-
#include<conio.h>
#include<iostream> using
namespace std; class
polar
{
int radius, angle;
public: polar()
{ radius=10;
angle=20;
} polar(int
a,int b)
{
radius=a;
angle=b;
} polar operator +
(polar p)

{ polar
t;
t.radius=radius+p.radius;
t.angle=angle+p.angle;
return t;
}
friend void operator >>(istream &,polar &);
friend void operator <<(ostream &,polar &);
}; void operator >>(istream &in, polar
&p)
{ cout<<"\nEnter Radius: \n";
in>>p.radius; cout<<"\nEnter Angle: \
n"; in>>p.angle; } void operator
<<(ostream &out,polar &p)
{ out<<"\nRadius= "<<p.radius<<"\
n"; out<<"\nAngle= "<<p.angle<<"\
n";
} main()
{ polar
p1,p2,p
3;
cout<<"
\nEnter
Value
for First
Object:
";
cin>>p1
;
cout<<"
\nEnter
Value
for
Second
Object:
";
cin>>p2
;
p3=p1+
p2;
cout<<"
\nResult
After
Additio
n is: \n";
cout<<p
3;
getch();
}

Output :-
PRACTICAL :- 17
Create class DegreeCelsius having a single data member to hold value of
temperature in degree Celsius. Provide following facilities:
a) Overloaded operator ++ which will increase value of data member by 1
(consider postfix and prefix operator overloading).
b) Overloaded operator -- which will decrease value of data member by 1
(consider postfix and prefix operator overloading).
c) Overloaded insertion and extraction operators for input in data
member and display value of data member.
Created by PK Monika(B.C.A-IInd year).

Coding:-
#include<conio.h>
#include<iostream> using
namespace std; class
DegreeCelcius
{ int
cel;
public:
friend void operator >>(istream &, DegreeCelcius &);
friend void operator <<(ostream &,DegreeCelcius &);
void operator ++()
{
cel++;
}
void operator ++(int)
{ cel++; }
void operator --()
{ cel--; }
void operator --(int)
{ cel--
;
}
};
void operator >>(istream &in,DegreeCelcius &d)
{ cout<<"\nEnter Temperature in Celcius: ";
in>>d.cel; } void operator <<(ostream
&out,DegreeCelcius &d)
{ out<<d.c
el; } main()
{
DegreeCelcius d,u; cin>>d; u=d; d++; cout<<"\nValue
After Increment by Post-increment Operator: "; cout<<d; ++d;
cout<<"\nValue After Increment by Pre-increment Operator: ";
cout<<d;
u--;
cout<<"\nValue After Decrement by Post-decrement Operator: ";
cout<<u; cout<<"\nValue After Decrement by Pre-decrement
operator: "; cout<<u; getch();
}

Output :-
18

Create a program having pointer to void to store address of


integer variable then print value of integer variable using
pointer to void. Perform the same operation for float
variable.
Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace
std; main() { void
*p; int a; float f;
p=&a;
*(int *)p=54;
cout<<*(int *)p<<"\n";
p=&f; *(float *)p=45.67;
cout<<*(float *)p;
getch();
}

Output :-

19
Write program to find biggest number among three numbers
using pointer and function.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; void max(int *,int
*,int *); main() { int a,b,c;
cout<<"\nEnter First Value: ";
cin>>a; cout<<"\nEnter Second
Value: "; cin>>b; cout<<"\nEnter
Third Value: "; cin>>c;
max(&a,&b,&c);
getch();
}
void max(int *p,int *q,int *r)
{ int max; max=*p; if(*q>max) max=*q; if(*r>max) max=*r;
cout<<"\nThe biggest number is: "<<max;
}

Output :-

20
Write a program using inline function to calculate area of circle.

Created by PK Monika(B.C.A-IInd year).


Coding:-

#include<conio.h> #include<iostream>
using namespace std; class circle { float
cir; public:
void area(int r); void show()
{
cout<<"Area Of Circle is: "<<cir;
} }; inline void circle::area(int r)
{ cir=(3.14*r*r); } main() { circle c;
int p;
cout<<"Enter The Radius: "; cin>>p;
c.area(p);
c.show(); getch();
}

Output :-
21

Write a program using inline function to find minimum of


two functions. The inline function should take two arguments
and should return the minimum value.

Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream> using
namespace std; class small
{ int a;
public:
void min(int x,int y); void
show()
{
cout<<"\nSmallest no is: "<<a;
} }; inline void small::min(int x,
int y) { a=x<y?x:y; } main()
{ small s; int p,q; cout<<"\nEnter
Any Two Integer Nos.: \n";
cin>>p>>q; s.min(p,q);
s.show(); getch();
}

Output :-
22

Consider an example of declaring the examination result. Design


three classes:- student, exam and result. The student class has data
members such as that representing roll number, name of student.
Create the class exam, which contains data members representing
name of subject, minimum marks, maximum marks, obtained
marks for three subjects. Derive class result from both student and
exam classes. Test the result class in main function.

Created by PK Monika(B.C.A-IInd year).

Coding:-
#include<conio.h>
#include<iostream> using
namespace std; class
student
{ protected:
char roll_number[10];
char name[30]; public:
void getData()
{
cout<<"\n ENTER ROLL NUMBER OF STUDENT : "; cin>>roll_number;
cout<<"\n ENTER NAME OF STUDENT : "; cin>>name;
}
void showData()
{
cout<<"\n ROLL NUMBER OF STUDENT IS :"<<roll_number; cout<<"\n NAME OF
STUDENT IS : "<<name;
} }; class exam { protected:
char student_name[3][20]; int max_marks[3]; int
min_marks[3]; int obtained_marks[3];
public:
void getData(); void showData();
}; void exam::getData()
{ for(int i=0;i<3;i++)
{
cout<<"\n ENTER THE NAME OF SUBJECT : "; cin>>student_name[i];
cout<<"\n ENTER THE MAXIMUM MARKS OF THE SUBJECT : ";
cin>>max_marks[i]; cout<<"\n ENTER THE MINIMUM MARKS OF THE
SUBJECT : "; cin>>min_marks[i]; cout<<"\n ENTER THE OBTAINED
MARKS FOR THE SUBJECT : "; cin>>obtained_marks[i];
} } void exam::showData()
{ for(int i=0;i<3;i++)
{
cout<<"\n THE NAME OF SUBJECT IS : " <<student_name[i]; cout<<"\n
MAXIMUM MARKS IN THE SUBJECT IS : "<<max_marks[i]; cout<<"\n
MINIMUM MARKS IN THE SUBJECT IS : "<<min_marks[i]; cout<<"\n
OBTAINED MARKS IN THE SUBJECT IS :"<<obtained_marks[i];
} } class result:public student,public exam
{ int max_total; int
obtained_total; public:
void getData()
{ student::getData();
exam::getData();
} void showData();
}; void result::showData()
{ obtained_total=0;
max_total=0;
student::showData();
exam::showData(); for(int
i=0;i<3;i++)
{
max_total+= max_marks[i]; obtained_total+=
obtained_marks[i];
}
cout<<"\n TOTAL OBTAINED "<<obtained_total<<" OUT OF "<<max_total; cout<<"\
n PERCENTAGE = "<<(float)obtained_total/max_total*100.0<<"%";
} main() { result
s;
s.getData();
s.showData();
getch();
}
Output :-
PRACTICAL :- 23

Create a base class shape having two data members with


two- member function get data (pure virtual function) and
print area (not pure virtual 1function). Derive classes
triangle and rectangle from class shape and redefine
member function print area in both classes triangle and
rectangle and test the functioning of classes using pointer
to base class objects and normal objects.
Created by PK Monika(B.C.A-IInd year).

Coding:-

#include<conio.h>
#include<iostream>
using namespace std;
class shape
{ protecte
d: float
b,h,l;
public:

virtual void getData()=0; virtual


void printarea()
{
} }; class triangle:public
shape
{ public:
void getData()
{
cout<<"\n ENTER BASE AND HEIGHT OF THE TRIANGLE : \n ";
cin>>b>>h;
} void printarea()
{
cout<<"\n AREA OF TRIANGLE IS : "<<1.0/2*b*h<<"\n";
} }; class rectangle:public
shape
{ public:
void getData()
{
cout<<"\n ENTER LENGTH AND BREADTH OF RECTANGLE :\n";
cin>>b>>l;
} void printarea()
{
cout<<"\n AREA OF RECTANGLE IS :"<<b*l;
}
}; main() { triangle *t=new triangle; rectangle *r=new
rectangle; shape *s; s=t; s->getData(); s->printarea();
s=r; s->getData(); s->printarea(); getch();
}
Output :-

You might also like