RAJIV SAHU C++ Practicle PDF
RAJIV SAHU C++ Practicle PDF
PRACTICAL
REPORT
ON
“PROGRAMMING IN C++”
SUBMITTED TO
HEMCHAND YADAV UNIVERSITY, DURG (C.G.)
FOR THE PARTIAL FULLFILLMENT OF THE DEGREE
OF
Bachelor Of Computer Application
Session 2023-2024
b) A B C D E F G C) *
ABC EFG
* *
AB F G
* * *
A G
b) 1
d) 1
12
121
123
1331
1234
14641
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
long fact(int);
int main()
{
int i,j,k,space=1, ctr=1;
char ch1='D', ch2='D',ch;
cout<<"\nPattern 1 :-\n\n";
for(i=1;i<=4; i++)
{
for (ch='A';ch<=ch1;ch++)
cout<<ch;
if (i==1)
cout<<"\b";
for(j=2;j<space;j++)
cout<<" ";
for (ch=ch2;ch<='G';ch++)
cout<<ch;
cout<<"\n";
ch1--;
ch2++;
space+=2;
}
cout<<"\nPress any key !";
getch();
cout<<"\n\nPattern 2 :-\n\n";
space=2;
for (i=1;i<=3;i++)
{
for(j=1;j<=space;j++)
cout<<" ";
for (k=1;k<=ctr;k++)
cout<<"* ";
cout<<"\n";
space--;
ctr++;
}
cout<<"\nPress any key !";
getch();
cout<<"\n\nPattern 3 :-\n\n";
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
cout<<j<<" ";
cout<<"\n";
}
cout<<"\nPress any key !";
getch();
cout<<"\n\nPattern 4 :-\n\n";
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
cout<<fact(i)/(fact(j)*fact(i-j))<<" ";
cout<<"\n";
} cout<<"\nPress any key to EXIT!";
getch();
}
long fact (int n){
int i;
long f=1;
for (i=1;i<=n;i++)
f*=i;
return f;
}
OUTPUT:-
Practical: 02
AIM: Write a program to display number 1 to 10 in octal, decimal and hexa-decimal
system.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"Decimal Octal Hexadecimal"<<endl;
CODING:-
#include<iostream>
int main()
{
double num1;
double num2;
double total;
cout << "Enter first number: "<<endl;
cin >> num1;
return 0;
OUTPUT:
Practical: 04
AIM: Write a c++ program to convert Celsius to Fahrenheit.
CODING:-
#include <iostream>
using namespace std;
int main(){
float c;
cout << "Enter Temperature in celsius\n";
cin >> c;
float f = (9 * c) / 5;
f += 32;
cout << "Temperature in fahrenheit: " << f;
return 0;
}
OUTPUT:
Practical: 05
AIM: 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 are matrices toad and third
matrix is destination where the resultant of addition of first two matric4s is stored .In similar way to
create functions for marks subtraction and multiplication.
CODING:-
include<iostream>
#include<conio.h>
using namespace std;
void Add(int[][3],int[][3],int[][3]);
void Sub(int[][3],int[][3],int[][3]);
void Mult(int[][3],int[][3],int[][3]);
int main() {
int a[3][3],b[3][3],c[3][3],i,j;;
cout<<"Enter matrix1:\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\n Enter matrix 2:\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
Add(a,b,c);
Sub(a,b,c);
Mult(a,b,c);
}
void Add(int a[][3],int b[][3],int c[][3])
{
cout<<"\n\nAddition of matrices : \n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
void Sub(int a[][3],int b[][3],int c[][3])
{
cout<<"\n\nSubstraction of matrices : \n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
cout<<c[i][j]<<" ";
} cout<<"\n";
}
}
void Mult(int a[][3],int b[][3],int c[][3])
{
int sum=0;
cout<<"\n\nMultiplcaition of matrices : \n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k,3;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
cout<<c[i][j]<<" ";
sum=0;}
cout<<"\n";
} }
OUTPUT:-
Practical: 06
AIM: Create a single program to perform following tasks without using library functions:
CODING:-
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
using namespace std;
void strRev(char[ ]);
void countChar(char[ ]);
void strCopy(char[ ]);
void countVCP(char[ ]);
int main()
{
char A[50];
cout<<"\nEnter a string: ";
gets(A);
strRev(A);
countChar(A);
strCopy(A);
countVCP(A);
getch( );
}
void strRev(char A[ ])
{
int i,j,temp,ctr=0;
for(i=0;A[i]!='\0';i++)
ctr++;
for(i=ctr-1,j=0;i>=ctr/2;i--,j++)
{
temp=A[j];
A[j]=A[i];
A[i]=temp;
}
cout<<"\n\na.Reserved String: "<<A;
}
void countChar(char A[ ])
{
int i,j,ctr=0;
for(i=0;A[i]!='\0';i++)
{
if(isalpha(A[i]))
ctr++;
}
cout<<"\n\nb.Total characters in string: "<<ctr;
}
void strCopy(char A1[ ])
{
char A2[50];
int i,j,temp,ctr=0;
for(i=0;A1[i]!='\0';i++)
ctr++;
for(i=ctr-1,j=0;i>ctr/2;i--,j++)
{
temp=A1[j];
A1[j]=A1[i];
A1[i]=temp;
}
for(i=0;A1[i]!='\0';i++)
A2[i]=A1[i];
A2[i]='\0';
cout<<"\n\nc.Copies String: "<<A2;
}void countVCP(char A[ ])
{
int vowel,conso,punct;
vowel=conso=punct=0;
for(int i=0;A[i]!='\0';i++)
{
if(ispunct(A[i]))
punct++;
else if(A[i]=='a'||A[i]=='e'||A[i]=='E'||A[i]=='A'||A[i]=='i'||A[i]=='I'||A[i]=='o'||
A[i]=='O'||A[i]=='u'||A[i]=='U')
vowel++;
else if(A[i]!=' ')
conso++;
}
cout<<"\n\nd.Total no. of punctuations: "<<punct;
cout<<"\n\nd.Total no. of vowels: "<<vowel;
cout<<"\n\nd.Total no. of consonants: "<<conso;
}
OUTPUT:-
Practical: 07
AIM: create a class student having data member to store roll number, name of student,
name of three subject, max marks, min marks, obtained marks. Declare an object of class
student. Provide facilities to input data in data member and display result of student.
CODING:-
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class student
{
int roll,max,min,obt[3];
char name[20], sub[3][10];
public:
void getResult();
void putResult();
};
void student :: getResult(){
cout<<" student result entry :- \n";
cout<<"\n\nSubjects obtained"<<endl;
for(int i=0; i<3; i++){
cout<<sub[i]<<obt[i]<<endl;
total += obt[i];
}
maxtotal =max * 3;
cout<<"\nPercentages scored : "<<total/maxtotal * 100<<"m";
}
int main()
{
student s;
s.getResult();
s.putResult();
return 0;
}
OUTPUT:-
Practical: 08
AIM:- Create a class salary having an array of integer 5 elements as data member provide
followings:
a) Constructor to get number in array elements.
b) Sort the element.
c) Find largest element.
d) Search for presence of particular value in array element.
CODING:-
#include<iostream>
using namespace std;
class salary
{
int arr[];
public:
salary()
{
for(int i=0;i<5;i++)
{
cout<<"Enter element"<<i+1 <<" :";
cin>>arr[i];
}
}
void sort();
void largest();
void search(int);
};
int main()
{
int num,ch;
salary ob;
do{
cout<<"****MENU*****"<<endl;
cout<<"\n1. Sort The Array";
cout<<"\n2. Find Largest Element";
cout<<"\n3. Search For An Element";
cout<<"\n4. Exit Program";
cout<<"\n\nEnter Your Choice :";
cin>>ch;
switch(ch)
{
case 1: ob.sort();
break;
case 2: ob.largest();
break;
case 3:cout<<"\nEnter The Element Want To Search : ";
cin>>num;
ob.search(num);
break;
case 4:exit(0);
default:cout<<"\nWrong Choise!!!"<<"\npress any key";
}
}while(1);
}
void salary::sort()
{
int i,j,temp;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
} }
}
cout<<"\nSorted Array(Asending order):\n";
for(int i=0;i<5;i++)
cout<<arr[i]<<endl;}
void salary :: largest()
{
int i,temp;
temp=arr[0];
for(i=0;i<5;i++)
{
if(temp<arr[i])
temp=arr[i];
}
cout<<"\nGreatest Element : "<<temp;
}
void salary :: search(int num)
{
int flag=0,pos,i;
for(i=0;i<5;i++)
{
if(num==arr[i])
{
flag=1;
pos=i+1;
}
}
if(flag==0)
cout<<"\nElent Not Found";
else
cout<<"\nElement Found At Position"<<pos;
}
OUTPUT:-
Practical: 09
AIM: create a class Simple with static member functions for following tasks:
a) To find factorial by recursive member function.
b) To check whether a no. is prime or not.
c) To generate Fibonacci series up to requested terms.
CODING:-
#include<iostream>
#include<stdlib.h>
using namespace std;
class Simple
{
public:
static long int fact(int);
static void Cprime();
static void fab();
};
int main()
{
int num,ch;
do{
cout<<"*MENU*"<<endl;
cout<<"\n1. Find factorial of a number";
cout<<"\n2. Check for a prime number";
cout<<"\n3. Print fibonacci series";
cout<<"\n4. Enter your choice :";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter a number :";
cin>>num;
cout<<"\nFactorial of "<<num<<" is :"<<Simple::fact(num);
return 0;
break;
case 2: Simple::Cprime();
return 0;
break;
case 3:Simple::fab();
return 0;
break;
case 4: exit(0);
void Simple::Cprime()
{
int num,flag=0;
cout<<"\n Enter a number:";
cin>>num;
if(flag==0)
cout<<"\n"<<num<<" is a Prime Number ";
else
cout<<"\n"<<num<<" Is Not A Prime Number";
}
void Simple::fab()
{
int n1=-1, n2=1, n3, num, i;
cout<<"\nEnter Number of Terms :";
cin>>num;
CODING:-
#include <iostream>
using namespace std;
class darray {
private:
int** arr;
public:
darray(int r, int c) {
rows = r;
cols = c;
void input() {
}
void output() {
return result;
} return result;
result.arr[i][j] = 0;
for (int k = 0; k < cols; k++) {
return result;
};
int main() {
array1.input();
array2.input();
array1.output();
array2.output();
cout<<"Addition Result:"<<endl;
sum.output();
cout<<"Subtraction Result:"<<endl;
diff.output();
product.output();
return 0;
}
OUTPUT:-
Practical: 11
AIM: Write program to create class complex having data members to store real and
imaginary part. Provide following facilities:
CODING:-
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class complex
{
int real, image;
public:
void get()
{
cout<<"Enter Real Part: ";
cin>>real;
cout<<"Enter Imaginary Part: ";
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 show1()
{
cout<<"\n\n1.Addition : "<<real<<"+"<<image<<"i\n";
}
void show2()
{
if(image>0)
cout<<"\n2. Subtraction :"<<real<<"- "<<image<<"i\n";
else
cout<<"\n2.Subtraction :"<<real<<"+"<<-1*image<<"i\n";
}
void show3()
{
cout<<"\n3. Multiplication :"<<real<<" * "<<image<<"i\n";
}
void show4()
{
cout<<"\n4. Division: "<<real<<" / " <<image<<"i\n";
}
};
int main()
{
complex c1,c2,c3;
c3=c1.add(c2);
c3.show1();
c3=c1.subtract(c2);
c3.show2();
c3=c1.multiply(c2);
c3.show3();
c3=c1.division(c2);
c3.show4();
return 0;
}
OUTPUT:-
Practical: 12
AIM: Write program to create class distance having data members feet and inch (A single
object will store distance in form such as 5 feet 3 inch). It contains member functions for
taking input in data members and member function for displaying value of data
members. Class Distance contains declaration of friend function add which accepts two
objects of class Distance and returns object of class Distance after addition. Class Distance
contains declaration of another friend function. Subtract accepts two objects of class
Distance and returns object of class Distance after subtraction. Text the class using main
function and objects of class Distance.
CODING:-
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
int inch;
public:
void inputDistance() {
cout << "Enter the distance in feet: ";
cin >> feet;
cout << "Enter the distance in inches: ";
cin >> inch;
}
void displayDistance() {
cout << "The distance is " << feet << " feet " << inch << " inches." << endl;
}
friend Distance add(Distance d1, Distance d2) {
Distance result;
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
if (result.inch >= 12) {
result.feet += result.inch / 12;
result.inch %= 12;
}
return result;
}
friend Distance subtract(Distance d1, Distance d2) {
Distance result;
int totalInchesD1 = (d1.feet * 12) + d1.inch;
int totalInchesD2 = (d2.feet * 12) + d2.inch;
OUTPUT:-
Practical: 13
AIM: 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.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
class father;
class mother
{
long int msal;
public:
mother()
{
cout<<"\nEnter 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 m, father f);
};
void add(mother m,father f)
{
cout<<"\n\nSum of salaries of Mother & Father = "<<m.msal+f.fsal;
}
int main()
{
mother m;
father f;
add(m,f);
return 0;
}
OUTPUT:-
Practical: 14
AIM: 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.
Declare class Father to be friend class of Mother. Write a member function in Father,
which accepts object of class Mother and prints Sum of Salary of Mother and Father
Objects. Create member function in each class to get input in data member and to display
the value of data member.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
class father;
class mother
{
long int msal;
public:
void get()
{
cout<<"\nEnter Mother's Salary : ";
cin>>msal;
}
friend class father;
};
class father
{
long int fsal;
public:
void get()
{
cout<<"\nEnter Father's Salary : ";
cin>>fsal;
}
void add(mother m)
{
cout<<"\n\nSum of salaries of Mother & Father = "<<m.msal+fsal;
}
};
int main()
{
mother m;
father f;
m.get();
f.get();
f.add(m);
return 0;
}
OUTPUT:-
Practical: 15
AIM: 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.
CODING:-
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
private:
char name[40],sub1[20],sub2[20],sub3[30];
int rollno,sub1mark,sub2mark,sub3mark;
public:
void get();
void show();
};
int main()
{
struct student s;
s.get();
s.show();
return 0;
}
void student::get()
{
cout<<"\nEnter Student Deatails :-\n\n";
cout<<"\nEnter Name of student : ";
gets(name);
Practical: 16
AIM: Write program to create a class Polar which has data member radius and angle,
define overloaded constructor to initialize object and copy constructor to initialize one
object by another existing object keep name of parameter of parameterized constructor
same as data members. Test function of the program in main function.
CODING:-
#include<iostream>
using namespace std;
class polarss
{
int radius, angle;
public:
polar()
{
radius=10;
angle=20;
}
polar(int radius, int angle)
{
this->radius=radius;
this->angle=angle;
}
polar(polar &p)
{
radius=p.radius;
angle=p.angle;
}
void show()
{
cout<<"Radius = "<<radius<<"\t"
<<"angle = "<<angle;
}
};
int main()
{
polar p1;
polar p2(20,30);
polar p3(p2);
Practical: 17
AIM: Write program to create a class Polar which has data member radius and angle,
define overloaded constructor to initialize object and copy constructor to initialize one
object by another existing object keep name of parameter of parameterized constructor
same as data members. Test function of the program in main function.
CODING:-
#include<iostream>
using namespace std;
class polar
{
int radius, angle;
public:
polar(int radius=40, int angle=50)
{
this->radius=radius;
this->angle=angle;
}
void show()
{
cout<<"Radius = "<<radius<<"\t"
<<"angle = "<<angle;
}
};
int main()
{
polar p1;
polar p2(50,40);
cout<<"\n\nDefault Values :-\n";
p1.show();
return 0;
}
OUTPUT:-
Practical: 18
AIM: write a program using class, which uses static overloaded function to swap two
integers; two floats methods use parameter passing by address.
CODING:-
#include<iostream>
using namespace std;
class sample
{
public:
static void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
static void swap(float *x,float *y)
{
float t;
t = *x;
*x = *y;
*y = t;
}
};
int main()
{
static int a,b;
static float f1,f2;
sample::swap(&a,&b);
cout<<"\n\nValues After Swapping :-\n ";
cout<<"a = "<<a
<<"\nb= "<<b ;
cout<<"\n----------------------------------------------";
sample::swap(&f1,&f2);
cout<<"\n\nValues After Swapping :- \n";
cout<<"f1 ="<<f1
<<"\nf2 = "<<f2 ;
return 0;
}
OUTPUT:-
Practical: 19
AIM: Create class String having pointer to character as data member and provide
following Facilities:
CODING:-
#include <iostream>
#include <cstring>
class String {
private:
char* str;
public:
String(const char* s = nullptr) {
if (s != nullptr) {
str = new char[strlen(s) + 1];
strcpy(str, s);
} else {
str = new char[1];
str[0] = '\0';
}
}
~String() {
delete[] str;
}
String operator+(const String& other) const {
String result;
result.str = new char[strlen(str) + strlen(other.str) + 1];
strcpy(result.str, str);
strcat(result.str, other.str);
return result;
}
String& operator=(const String& other) {
if (this != &other) {
delete[] str;
str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
}
return *this;
}
bool operator==(const String& other) const {
return (strcmp(str, other.str) == 0);
}
bool operator<(const String& other) const {
return (strcmp(str, other.str) < 0);
}
bool operator>(const String& other) const {
return (strcmp(str, other.str) > 0);
}
bool operator<=(const String& other) const {
return (strcmp(str, other.str) <= 0);
}
bool operator>=(const String& other) const {
return (strcmp(str, other.str) >= 0);
}
bool operator!=(const String& other) const {
return (strcmp(str, other.str) != 0);
}
friend std::istream& operator>>(std::istream& in, String& s) {
char buffer[1000];
in >> buffer;
s = buffer;
return in;
}
friend std::ostream& operator<<(std::ostream& out, const String& s) {
out << s.str;
return out;
}
};
int main() {
String s1("Hello ");
String s2("RAJIV");
String s3 = s1 + s2;
std::cout << "Concatenation: " << s3 << std::endl;
String s4;
std::cout << "Enter a string: ";
std::cin >> s4;
std::cout << "Comparison: " << (s1 == s2 ? "Equal" : "Not Equal") << std::endl;
std::cout << "Comparison: " << (s1 < s2 ? "Less Than" : "Not Less Than") << std::endl;
return 0;
}
OUTPUT:-
Practical: 20
AIM: Create a class Matrix having data member double dimension array of floats of size
3x3. provide following facilities:
a) Overloaded extraction operator for data input.
b) Overloaded insertion operator for data output.
c) Overloaded operator + for adding two using matrix objects.
d) Overloaded operator – for subtracting two using matrix objects.
e) Overloaded operator * for multiplying two using matrix objects.
CODING:-
#include <iostream>
using namespace std;
class Matrix {
private:
double data[3][3];
public:
// Overloaded extraction operator for data input
friend std::istream& operator>>(std::istream& in, Matrix& mat) {
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
in >> mat.data[i][j];
return in;
}
int main() {
Matrix mat1, mat2;
return 0;
}
OUTPUT:-
Practical: 21
AIM: 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) Over loaded constructor for initialization of data members.
c) Overloaded operator + to add two polar co-ordinates using objects of class Polar.
CODING:-
#include<iostream>
using namespace std;
class polar
{
int radius, angle;
public:
void get()
{
cout<<"Enter radius: ";
cin>>radius;
cout<<"enter Angle: ";
cin>>angle;
}
void show()
{
cout<<"radius = "<<radius<<"\n"
<<"angle = "<<angle;
}
friend polar add(polar, polar);
};
polar add(polar p1,polar p2)
{
polar p3;
p3.radius=p1.radius+p2.radius;
p3.angle=p1.angle+p2.angle;
return p3;
}
int main()
{
polar p1,p2,p3;
cout<<"\nEnter First polar :-\n";
p1.get();
cout<<"\nEnter second polar :-\n";
p2.get();
cout<<"\n value of first polarP: -\n";
p1.show();
cout<<"\n value of first polarP: -\n";
p2.show();
p3=add(p1,p2);
cout<<"\nAfter addtion value is:-\n";
p3.show();
return 0;
}
OUTPUT:
Practical: 22
AIM: Create a class polar that contains data member radius and angels. Create another
class Cartesian in the same program and provide following facilities:
a) It should be possible to assign object of polar class to object of cartesian class.
b) It should be possible to assign object of Cartesian class to object of polar class.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
class circle
{
public:
float radius;
circle()
{
cout<<"\n Enter Radius of Circle: ";
cin>>radius;
}
void Acircle()
{
cout<<"\n\nArea of Circle = "<< 3.14 * radius * radius;
}
};
class sphere: public circle
{
public:
void Svol()
{
cout<<"\n\nVolume of Sphere = "<< 1.33 * 3.14 * radius * radius * radius;
}
};
public:
cylinder()
{
cout<<"\nEnter Height of Cylinder:";
cin>>height;
}
void Cvol()
{
cout<<"\n\nVolume of Cylinder = "<< 3.14 * radius * radius * height;
}
};
int main()
{
cylinder c;
c.Acircle();
c.Svol();
c. Cvol();
return 0;
}
OUTPUT:-
Practical: 23
AIM: Write a program to find biggest number among three number using pointer and
function.
CODING:-
#include<iostream>
using namespace std;
void max(int *,int *,int *);
int main()
{
int a,b,c;
OUTPUT:-
Practical: 24
AIM: Write swapping program demonstrate call by value, call by address, call by
reference in a simgle program.
CODING:-
#include<iostream>
using namespace std;
void swapvalue(int,int);
void swapref(int &,int &);
void swappad(int *,int *);
int main()
{
int a,b;
cout<<"\nEnter value of a : ";
cin>>a;
cout<<"\nEnter value of b : ";
cin>>b;
swapvalue(a,b);
swapref(a,b);
cout<<"\n\nCALL BY REFERENCE :-";
cout<<"\n\nValue of a after swap "<<a;
cout<<"\nValue of b after swap "<<b;
swappad(&a,&b);
cout<<"\n\nCALL BY ADDRESS :-";
cout<<"\n\nValue of a after swap "<<a;
cout<<"\nValue of b after swap "<<b;
return 0;
}
void swapvalue(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<"\n\nCALL BY VALUE :-";
cout<<"\n\nValue of a after swap "<<a;
cout<<"\nValue of b after swap "<<b;
}
void swapref(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
void swappad(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
OUTPUT:-
Practical: 25
AIM: Write a program using inline function to calculate area of circle.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
class circle
{
float cir;
public:
void area(float r);
void show()
{
cout<<"\n\nArea of Circle is : "<<cir;
}
};
inline void circle :: area(float r)
{
cir=(3.14*r*r);
}
int main()
{
circle c;
float p;
c.area(p);
c.show();
return 0;
}
OUTPUT:-
Practical: 26
AIM: 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.
CODING:-
#include<iostream>
#include<conio.h>
using namespace std;
class small{
int a;
public:
void min(int x,int y);
void show()
{
cout<<"\n\nsmallest Number Is : "<<a;
}
};
inline void small::min(int x,int y)
{
a=x<y?x:y;
}
int main()
{
small s;
int p,q;
cout<<"\nEnter Integer 1 : ";
cin>>p;
#include<iostream>
#include<conio.h>
using namespace std;
class circle
{
public:
float radius;
circle()
{
cout<<"\n Enter Radius of Circle: ";
cin>>radius;
}
void Acircle()
{
cout<<"\n\nArea of Circle = "<< 3.14 * radius * radius;
}
};
class sphere: public circle
{
public:
void Svol()
{
cout<<"\n\nVolume of Sphere = "<< 1.33 * 3.14 * radius * radius * radius;
}
};
public:
cylinder()
{
cout<<"\nEnter Height of Cylinder:";
cin>>height;
}
void Cvol()
{
cout<<"\n\nVolume of Cylinder = "<< 3.14 * radius * radius * height;
}
};
int main()
{
cylinder c;
c.Acircle();
c.Svol();
c. Cvol();
return 0;
}
OUTPUT:-
Practical: 28
AIM: Create a base class shape having two data members with two-member function get
data (pure virtual function) and print area (not pure virtual function). 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.
CODING:-
#include <iostream>
using namespace std;
class Shape {
protected:
double base;
double height;
public:
virtual void getData() = 0;
void printArea() {
cout << "Area: " << calculateArea() << endl;
}
virtual double calculateArea() const {
return 0.0;
}
};
class Triangle : public Shape {
public:
int main()
{
Shape* shapePtr;
Triangle triangle;
Rectangle rectangle;
shapePtr = ▵
shapePtr->getData();
shapePtr->printArea();
shapePtr = &rectangle;
shapePtr->getData();
shapePtr->printArea();
Triangle anotherTriangle;
Rectangle anotherRectangle;
anotherTriangle.getData();
anotherTriangle.printArea();
anotherRectangle.getData();
anotherRectangle.printArea();
return 0;
}
OUTPUT:-
Practical: 29
AIM: Write a class having name Calculate that uses static overload function to calculate
area of circle, area of rectangle and area of triangle.
CODING:-
#include <iostream>
#include <cmath>
class ArraySort {
public:
int main() {
return 0;
OUTPUT:-
Practical: 30
AIM: Write a class Array Sort that uses static overload function to sort an array of floats,
an array of integers.
CODING:-
#include <iostream>
#include <algorithm>
class ArraySort {
public:
static void sortArray(float arr[], int size) {
std::sort(arr, arr + size);
}
static void sortArray(int arr[], int size) {
std::sort(arr, arr + size);
}
};
int main() {
const int sizeFloat = 5;
float floatArray[sizeFloat] = {4.5, 2.0, 8.1, 1.7, 5.3};
ArraySort::sortArray(floatArray, sizeFloat);
ArraySort::sortArray(intArray, sizeInt);
return 0;
}
OUTPUT:-