Nothing Special   »   [go: up one dir, main page]

C++ Practicle File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./….

/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

Program 2:- Write a program to display number from 1 to 10 using loop.

#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

Program 4:-Write a program to calculate the area and perimeter of


rectangle.

#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:-

Program 5:- Write a program to show exception handling.


KHUSHI BIRLA 4
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

#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

Program 6:- Write a program to implement function overloading


#include<iostream>
using namespace std;
class first
{
public:
int add(int x , int y)
{
return x+y;
}
int add(int x, int y, int z)
{
return x+y+z;
}
};
int main()
{
first f1;
cout<<"1st answer is: "<<f1.add(10, 20)<<endl;
cout<<"2nd answer is: "<<f1.add(10, 20,30)<<endl;
return 0;
}

Output:-

KHUSHI BIRLA 6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program 7:- Write a program to implement function overriding.


#include<iostream>
using namespace std;
class A
{public:
void show()
{cout<<"eat"<<"\n";
}
};
class B:public A
{public:
void show()
{cout<<"sleep"<<"\n";
}
};
int main()
{B ob1;
ob1.show();
A ob2;
ob2.show();
return 0;
}

Output:-

KHUSHI BIRLA 7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program 8:- Write a program to implement parameterized constructor

#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

Program to show single inheritance


#include<iostream>
using namespace std;
class A
{int a=4;
int b=4;
public:

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

Program to show multilevel inheritance


#include<iostream>
using namespace std;
class A
{public:
void display()
{cout<<"eat"<<"\n";
}
};
class B:public A
1
KHUSHI BIRLA
0
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

{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

Program to create class and object


#include<iostream>
using namespace std;
class A
{public:
int a=9;
void show()
{cout<<"the square is"<<a*a;
}
};
int main()
{A ob;
ob.show();
return 0;
1
KHUSHI BIRLA
2
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Output

Program to implement encapsulation

Output

Program to show abstract class and pure


virtual function
#include<iostream>
1
KHUSHI BIRLA
3
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

using namespace std;


class A
{public:
virtual void print()=0;
};
class B:public A
{public:
void print()
{cout<<"drink\n"<<"\n";
}
void display()
{cout<<"sleep\n"<<"\n";
}};
int main()
{B ob;
ob.display();
ob.print();
return 0;
}

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

Program to merge two arrays


#include <iostream>
#define SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
using namespace std;
void mergeSortedArrays(int *arr1, int n1, int *arr2, int n2, int
*result){
int i, j, k;
i = 0;
j = 0;
k = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
result[k] = arr1[i];
++k;
++i;
} else {
result[k] = arr2[j];
++k;
++j;
}
}
while (i < n1) {

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

cout << "Second sorted array:" << endl;


dispalyArray(arr2, SIZE(arr2));
mergeSortedArrays(arr1, SIZE(arr1), arr2, SIZE(arr2),
result);
cout << "Final sorted array:" << endl;
dispalyArray(result, SIZE(result));
return 0;
}

Output

1
KHUSHI BIRLA
8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program to add two square matrix


#include<iostream>
using namespace std;
int main()
{int i, j, rows, columns, sum;
cout << "\nPlease Enter the Matrix rows and Columns = ";
cin >> i >> j;
int sumRCArray[i][j];
cout << "\nPlease Enter the Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> sumRCArray[rows][columns];
}
}
for(rows = 0; rows < i; rows++)
{sum = 0;
for(columns = 0; columns < j; columns++)
{sum = sum + sumRCArray[rows][columns];}
cout << "\nThe Sum of Items in " << rows + 1<< " Row of
a Matrix = " << sum ;
}
for(rows = 0; rows < i; rows++)
{sum = 0;
for(columns = 0; columns < j; columns++)

1
KHUSHI BIRLA
9
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

{sum = sum + sumRCArray[columns][rows];}


cout << "\nThe Sum of Items in Column of a Matrix = "
<< sum ;}
return 0;
}

Output

Program to show global and local


variable
#include<iostream>
using namespace std;
int a=2;//global variable
int main()
{int b=2;//local variable
cout<<"the sum is"<<a+b<<"\n";

2
KHUSHI BIRLA
0
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

return 0;
}

Output

Program to show multiple inheritance


#include<iostream>
using namespace std;
class A
{public:
void show()
{cout<<"get"<<"\n";
}
};
class B:public A
{public:
void display()
{cout<<"set"<<"\n";
}};

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

Program to show hierarchical


#include<iostream>
using namespace std;
class A
2
KHUSHI BIRLA
2
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

{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

Program to show constructor


overloading
#include <iostream>
using namespace std;
class construct
{public:
float area;
construct()
{area = 0;}
construct(int a, int b)
{area = a * b;}
void disp()

2
KHUSHI BIRLA
4
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

{cout<< area<< endl;}


};
int main()
{construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;}

output

Program to implement call by reference

Output

2
KHUSHI BIRLA
5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program to implement call by value


#include <iostream>
using namespace std;
void change(int data);
int main()
{ int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5; }

output

2
KHUSHI BIRLA
6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program to implement operator


overloading
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5) {}
void operator ++ () {
++value;
}
void display()
{cout << "Count: " << value << endl;
}
};
int main()
{ Count count1;
++count1;
2
KHUSHI BIRLA
7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

count1.display();
return 0;
}

Output

Program for dynamic memory


allocation using new and delete
#include <iostream>
using namespace std;
int main ()
{int* p = NULL;
p = new(nothrow) int;
if (!p)
cout << "allocation of memory failed\n";
else
{*p = 29;
cout << "Value of p: " << *p << endl;
}
2
KHUSHI BIRLA
8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

float *r = new float(75.25);


cout << "Value of r: " << *r << endl;
int n = 5;
int *q = new(nothrow) int[n];
if (!q)
cout << "allocation of memory failed\n";
else
{for (int i = 0; i < n; i++)
q[i] = i+1;
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << q[i] << " ";}
delete p;
delete r;
delete[] q;
return 0;
}

Output

2
KHUSHI BIRLA
9
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

Program to show pure virtual function


and abstract class
#include <iostream>
using namespace std;
class Shape {
protected:
float dimension
public:
void getDimension() {
cin >> dimension;
}
virtual float calculateArea() = 0;
};
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
3
KHUSHI BIRLA
0
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

Program for friend function


#include <iostream>
using namespace std;
class Distance {

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

Program to implement aggregation


#include <iostream>
using namespace std;

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

Program to show exception handling


using multiple catch block
#include<iostream>
using namespace std;
#include<conio.h>
void test(int x) {
try {
if (x > 0)
3
KHUSHI BIRLA
4
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

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

Program to implement scope resolution


operator
#include<iostream>

3
KHUSHI BIRLA
5
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

using namespace std;


int x;
int main()
{int x = 10;
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}

Output

Program to implement abstraction


#include <iostream>
using namespace std;
class implementAbstraction
{private:
int a, b;
public:
void set(int x, int y)
{a = x;
b = y;
3
KHUSHI BIRLA
6
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

void display()
{cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}};
int main()
{implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}

Output

Program to implement polymorphism


#include <iostream>
using namespace std;
void test(int i) {

3
KHUSHI BIRLA
7
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

cout << " The int is " << i << endl;


}
void test(double f) {
cout << " The float is " << f << endl;
}
void test(char const *ch) {
cout << " The char* is " << ch << endl;
}
int main() {
test(5);
test(5.5);
test("five");
return 0;
}

Output

Program to call member function using


object of the class
#include<iostream>

3
KHUSHI BIRLA
8
Object Oriented Programming Using C &C++ Paper code: BCA301 Date: …./…./2021

using namespace std;


class A
{public:
void show()
{cout<<"show"<<"\n";
}
};
int main()
{A ob;
ob.show();
return 0;
}

Output

3
KHUSHI BIRLA
9

You might also like