C++ Practicle File
C++ Practicle File
C++ Practicle File
/2021
Program 1:- Write a program to print my name using cin and cout.
#include<iostream>
using namespace std;
int main()
{
char a[5];
cout<<"enter your name"<<"\n";
cin>>a;
cout<<"the name is"<<a;
return 0;
}
Output:-
KHUSHI BIRLA 1
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
#include<iostream>
using namespace std;
int main()
{int i;
cout<<"Number's are: ";
for(i=0;i<=10;i++)
cout<<"\n"<<i;
return 0;
}
Output :-
KHUSHI BIRLA 2
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Program 3:- Write a program to swap two variables without using third
variable
#include<iostream>
using namespace std;
int main()
{int a=10,b=20;
cout<<"the value of 1st variable is: "<<a<<"\n";
cout<<"the value of 2nd variable is: "<<b<<"\n";
a=a*b;
b=a/b;
a=a/b;
cout<<"the values after swaping are: "<<a<<" & "<<b<<"\n";
return 0;
}
Output:-
KHUSHI BIRLA 3
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
#include<iostream>
using namespace std;
class A
{public:
int l=2,b=2;
void show()
{cout<<"The area is: "<<l*b<<"\n";
cout<<"The perimeter is: "<<2*l+2*b<<"\n";
}
};
int main()
{A ob;
ob.show();
return 0;
}
Output:-
#include<iostream>
using namespace std;
int main()
{int n,d,div;
cout<<"enter the value of denominator and numerator: "<<"\n";
cin>>n>>d;
try{
if(d==0)
{throw d;
}
div=n/d;
}
catch(int ex)
{cout<<"exception has occured"<<ex;
}
cout<<"the result is: "<<div;
return 0;
}
Output :-
KHUSHI BIRLA 5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output:-
KHUSHI BIRLA 6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output:-
KHUSHI BIRLA 7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
#include<iostream>
using namespace std;
class A
{public:
int a,b;
A(int x,int y)
{b=y;
a=x;
}
void print()
{cout<<"the value of a is"<<a<<"\n";
cout<<"the value of b is"<<b;
}
};
int main()
{A ob(2,3);
ob.print();
return 0;
}
Output:-
KHUSHI BIRLA 8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
void display()
{cout<<”the number a is”<<a;
cout<<”the number b is”<<b;}
};
class B:public A
{public:
void print()
{
Cout<<”the sum is”<<a+b;
}
};
int main()
{B ob1;
KHUSHI BIRLA 9
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
ob1.print();
return 0;
}
Output
{public:
void print()
{
cout<<"drink\n";}
};
class C:public B
{public:
void lot()
{cout<<"sleep\n";
}
};
int main()
{C ob1;
ob1.display();
ob1.print();
ob1.lot();
return 0;
}
Output
1
KHUSHI BIRLA
1
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
Output
Output
1
KHUSHI BIRLA
4
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
1
KHUSHI BIRLA
5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
1
KHUSHI BIRLA
6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
result[k] = arr1[i];
++k;
++i;
}
while (j < n2) {
result[k] = arr2[j];
++k;
++j;
}
}
void dispalyArray(int *arr, int n){
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main(){
int arr1[] = {10, 15, 17, 20};
int arr2[] = {5, 9, 7, 13, 19};
int result[SIZE(arr1) + SIZE(arr2)];
cout << "First sorted array:" << endl;
dispalyArray(arr1, SIZE(arr1));
1
KHUSHI BIRLA
7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
1
KHUSHI BIRLA
8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
1
KHUSHI BIRLA
9
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
2
KHUSHI BIRLA
0
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
return 0;
}
Output
2
KHUSHI BIRLA
1
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
class C:public A
{public:
void print()
{cout<<"go"<<"\n";
}
};
int main()
{C ob;
ob.print();
ob.show();
return 0;
}
Output
{public:
void show()
{cout<<"get"<<"\n";
}
};
class B
{public:
void display()
{cout<<"set"<<"\n";
}
};
class C:public A,public B
{public:
void print()
{cout<<"go";
}
};
int main()
{C ob;
ob.display();
ob.print();
2
KHUSHI BIRLA
3
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
ob.show();
return 0;
}
Output
2
KHUSHI BIRLA
4
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
output
Output
2
KHUSHI BIRLA
5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
output
2
KHUSHI BIRLA
6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
count1.display();
return 0;
}
Output
Output
2
KHUSHI BIRLA
9
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
};
int main() {
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
Output
3
KHUSHI BIRLA
1
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
private:
int meter;
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
int addFive(Distance d) {
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
3
KHUSHI BIRLA
2
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
class Address {
public:
string addressLine, city, state;
Address(string addressLine, string city, string state)
{
this->addressLine = addressLine;
this->city = city;
this->state = state;
}
};
class Employee
{
private:
Address* address;
public:
int id;
string name;
Employee(int id, string name, Address* address)
{
this->id = id;
this->name = name;
this->address = address;
}
void display()
3
KHUSHI BIRLA
3
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
{
cout<<id <<" "<<name<< " "<<
address->addressLine<< " "<< address->city<< " "<<address-
>state<<endl;
} }; int main(void) {
Address a1= Address("C-146, Sec-15","Noida","UP");
Employee e1 = Employee(101,"Nakul",&a1);
e1.display();
return 0;
}
Output
throw x;
else
throw 'x';
} catch (int x) {
cout << "Catch a integer and that integer is:" << x;
} catch (char x) {
cout << "Catch a character and that character is:" << x;
}
}
int main() {
cout << "Testing multiple catches\n:";
test(10);
test(0);
return 0;
}
Output
3
KHUSHI BIRLA
5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
void display()
{cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}};
int main()
{implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Output
3
KHUSHI BIRLA
7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
3
KHUSHI BIRLA
8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021
Output
3
KHUSHI BIRLA
9