0% found this document useful (0 votes)
7 views60 pages

C++ Programming Practical Exercises

The document is a compilation of programming exercises primarily focused on C++ concepts such as function overloading, data hiding, constructors, inheritance, operator overloading, templates, and exception handling. Each exercise includes a brief description followed by a code implementation and expected output. The exercises are designed to help learners practice and understand fundamental programming principles in C++.

Uploaded by

mukul1224107
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views60 pages

C++ Programming Practical Exercises

The document is a compilation of programming exercises primarily focused on C++ concepts such as function overloading, data hiding, constructors, inheritance, operator overloading, templates, and exception handling. Each exercise includes a brief description followed by a code implementation and expected output. The exercises are designed to help learners practice and understand fundamental programming principles in C++.

Uploaded by

mukul1224107
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Sr. Experiment Page Date Signature


No. Title No.
1 Write a program to compute the area of a triangle and a circle
by overloading the area () function.
2 Write a program to implement data hiding in C++.
3 Write a program to implement an inline function.

4 (a) Write a program to implement static data member.

(b) Write a program to implement static member function.

5 (a) Write a program to implement friend function.

(b) Write a program to implement friend class.

6 WAP for object pass as an argument and return an object.

7 Write a program to implement dynamic memory management


operators.
8 (a) Write a program to implement default
constructor.
(b) Write a program to implement parameterized
constructor.
(c) Write a program to implement copy
constructor.
(d) Write a program to implement destructor.

9 (a) Write a program to implement single level inheritance.

(b) Write a program to implement multilevel inheritance.

(c) Write a program to implement multiple inheritance.

(d) Write a program to implement hybrid inheritance.


10 (a) Write a program to implement unary operator overloading.

(b) Write a program to implement binary operator overloading.

11 Write a program to implement virtual function.

12 Write a program to swap data using function template.

13 Write a program to implement class template.

14 Write a program to implement exception handling.


PRACTICAL-1
Write a program to compute the area of a triangle and a
circle by overloading the area () function.

#include <iostream>
using namespace std;
class Shape {
public:
float area(float r) {
return 3.14 * r * r;
}
float area(float b, float h) {
return 0.5 * b * h;
}
};
int main() {
Shape s;
float r, b, h;
cout << "Enter radius of circle: ";
cin >> r;
cout << "Enter base of triangle: ";
cin >> b;

cout << "Enter height of triangle: ";


cin >> h;
cout << "\nArea of Circle = " << [Link](r);
cout << "\nArea of Triangle = " << [Link](b, h);
}
OUTPUT:
PRACTICAL-2
Write a program to implement data hiding in C++.
#include <iostream>
using namespace std;
class Student {
private:
int age;
public:
void setAge(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Student s;
int x;
cout << "Enter age: ";
cin >> x;
[Link](x);
cout << "Age is: " << [Link]();
return 0;
}
OUTPUT:
PRACTICAL-3
Write a program to implement an inline function
#include <iostream>
using namespace std;

inline int square(int x) {


return x * x;
}

intmain() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Square = " << square(n);
return 0;
}
OUTPUT:
PRACTICAL-4
a)Write a program to implement static data member.
#include <iostream>
using namespace std;

class Demo {
public:
static int count;
Demo() {
count++;
}
void show() {
cout << "Object number: " << count << endl;
}
};
int Demo::count = 0;
int main() {
Demo a, b, c;
[Link]();
[Link]();
[Link]();
}
OUTPUT:
b) Write a program to implement static member function.

#include <iostream>
using namespace std;
class Test {
public:
static int num;
static void show() {
cout << "Value of num = " << num;
}
};
intTest::num = 10;
intmain() {
Test::show();
}
OUTPUT:
PRACTICAL-5
a)Write a program to implement friend function.
#include <iostream>
using namespace std;
class Number {
private:
int x;
public:
void input() {
cout<<”enter value of x : ”
cin >> x;
}
friend void show(Number n);
};
void show(Number n) {
cout << "Value is: " << n.x;
}
intmain() {
Number n;
[Link]();
show(n);
}
OUTPUT:
b) Write a program to implement friend class
#include <iostream>
using namespace std;
class A {
private:
int x;
public:
void input() {
cout << "Enter value of x: ";
cin >> x;
}
friend class B;
};
class B {
public:
void show(A obj) {
cout << "Value of x = " << obj.x;
}
};
int main() {
A a;
[Link]();
B b;
[Link](a);
return 0;
}
OUTPUT:
PRACTICAL:6
WAP for object pass as an argument and return an object.
#include <iostream>
using namespace std;
class Number {
public:
intvalue;
void getData() {
cout << "Enter a number: ";
cin >> value;
}
Number add(Number n2) {
Number temp;
[Link] = value + [Link];
return temp;
}
};
intmain() {
Number n1, n2, result;
cout << "--- Input for first object ---\n";
[Link]();
cout << "--- Input for second object ---\n";
[Link]();
result = [Link](n2);
cout << "Result after adding objects = " << [Link];
return 0;
}
OUTPUT:
PRACTICAL- 7
Write a program to implement dynamic memory management
operators.
#include <iostream>
using namespace std;

intmain() {
intn;
cout << "Enter size of array: ";
cin>> n;
int*arr = new int[n];
cout << "Enter " << n << " numbers:\n";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "You entered: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
delete[] arr;
return 0;
}
OUTPUT:
PRACTICAL-8
a)Write a program to implement default constructor.
#include <iostream>
using namespace std;
class Student {
int age;
public:
Student() {
age = 0;
}
void input() {
cout << "Enter age: ";
cin >> age;
}
void display() {
cout << "Age = " << age;
}
};
int main() {
Student s;
[Link]();
[Link](); }
OUTPUT:
b) Writeaprogram to implement parameterized constructor.
#include<iostream>
usingnamespacestd;
class Student {
intage;
public:
Student(int a) {
age = a;
}
void show() {
cout<<"Age= " << age;
}
};
int main() {
int x;
cout<<"Enterage: ";
cin >> x;

Student s(x);
[Link]();
return 0;
}
OUTPUT:
c) Write a program to implement copy constructor.
#include <iostream>
using namespace std;
class Number {
int x;
public:
Number(int a) {
x = a;
}
Number(Number &n) {
x = n.x;
}
void show() {
cout << "Value = " << x << endl;
}
};
int main() {
int a;
cout << "Enter a number: ";
cin >> a;
Number n1(a);
Number n2(n1);
cout << "Original Object: ";
[Link]();
cout << "Copied Object: ";
[Link]();
}
OUTPUT:
d) Write a program to implement destructor.
#include <iostream>
using namespace std;
class Demo {
int value;
public:
Demo(int v) {
value = v;
cout << "Object Created with value = " << value << endl;
}
~Demo() {
cout << "Object Destroyed with value = " << value << endl;
}
};
intmain() {
intx;
cout << "Enter a value: ";
cin>> x;
Demo d(x);
}
OUTPUT:
PRACTICAL:9
a)Write a program to implement single level inheritance.
#include <iostream>
using namespace std;
class Person {
public:
string name;
void getData() {
cout << "Enter name: ";
cin >> name;
}
};
class Student : public Person {
public:
int roll;
void getRoll() {
cout << "Enter roll number: ";
cin >> roll;
}
void display() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << roll;
}

};
intmain() {
Student s;
[Link]();
[Link]();
[Link]();
}
OUTPUT:
b) Write a program to implement multilevel inheritance.
#include <iostream>
using namespace std;
class A {
public:
int x;
void getX() {
cout << "Enter value of x: ";
cin >> x;
}
};
class B : public A {
public:
int y;
void getY() {
cout << "Enter value of y: ";
cin >> y;
}
};
class C : public B {
public:
void display() {
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "Sum = " << x + y;
}
};
intmain() {
Cobj;
[Link]();
[Link]();
[Link]();
}
OUTPUT:
c) Write a program to implement multiple inheritance.
#include <iostream>
using namespace std;
class A {
public:
intx;
void getX() {
cout << "Enter value of x: ";
cin >> x;
}
};

classB {
public:
int y;
void getY() {
cout << "Enter value of y: ";
cin >> y;
}
};
class C : public A, public B {
public:
void show() {
cout << "Sum = " << x + y;
}
};
intmain() {
Cobj;
[Link]();
[Link]();
[Link]();
}
OUTPUT:
d) Write a program to implement hybrid inheritance.
#include <iostream>
using namespace std;
class A {
public:
int x;
void getX() {
cout << "Enter value of x: ";
cin >> x;
}
};
class B : public A {
public:
inty;
void getY() {
cout << "Enter value of y: ";
cin >> y;
}
};

classC {
public:
int z;
void getZ() {
cout << "Enter value of z: ";
cin >> z;
}
};
class D : public B, public C {
public:
void display() {
cout << "Sum = " << x + y + z;
}
};
intmain() {
Dobj;
[Link]();
[Link]();
[Link]();
[Link]();
}
OUTPUT:
PRACTICAL-9
a)Write a program to implement unary operator overloading.
#include <iostream>
using namespace std;

class Number {
int x;
public:
void get() {
cout << "Enter value: ";
cin >> x;
}
void show() {
cout << x;
}
void operator -() {
x = -x;
}
};

intmain() {
Number n;
[Link]();
-n;
[Link]();
}
OUTPUT:
b) Write a program to implement binary operator overloading.
#include <iostream>
using namespace std;
class Number {
int value;
public:
void input() {
cout << "Enter value: ";
cin >> value;
}
Number operator+(Number obj) {
Number temp;
[Link] = value + [Link];
return temp;
}
void display() {
cout << "Result = " << value;
}
};
int main() {
Number a, b, c;
[Link]();
[Link]();
c = a + b;
[Link]();
}
OUTPUT:
PRACTICAL-11
Write a program to implement virtual function.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void area(float x, float y) {
cout << "No shape selected";
}
};
class Rectangle : public Shape {
public:
void area(float x, float y) {
cout << "Area of Rectangle = " << x * y;
}
};
int main() {
Shape* s;
Rectangle r;
float l, b;
cout << "Enter length: ";
cin >> l;
cout << "Enter breadth: ";
cin >> b;
s = &r;
s->area(l, b);
}
OUTPUT:
PRACTICAL:12
Write aprogramtoswap data using function template.
#include <iostream>
using namespace std;
voidswapData(T&a,T&b) {
T temp = a;
a=b;
b = temp;
}
int main() {
int x, y;
cout<<"Entervalueof x: ";
cin >> x;
cout<<"Entervalueof y: ";
cin >> y;
swapData(x, y);
cout<<"Afterswapping:\n";
cout<<"x="<<x<<"\n";
cout << "y = " << y;
return 0;
}
OUTPUT:
PRACTICAL:13
Write a program to implement class template.
#include <iostream>
using namespace std;
template <class T>
class Number {
T value;
public:
Number(T v) {
value = v;
}
T getValue() {
return value;
}
};
int main() {
int x;
cout << "Enter an integer: ";
cin >> x;
Number<int> n1(x);
cout << "You entered: " << [Link]() << endl;
float y;
cout << "Enter a float: ";
cin >> y;
Number<float> n2(y);
cout << "You entered: " << [Link]();
}
OUTPUT:
PRACTICAL-14
Write a program to implement exception handling.
#include<iostream>
usingnamespacestd;

int main() {
int a, b;
cout<<"Enternumerator: ";
cin >> a;
cout<<"Enterdenominator: ";
cin >> b;

try{
if(b==0)
throw b;
cout<<"Result: " << a / b;
}
catch (int x) {
cout<<"Error: Division by zero not allowed";
}
}
OUTPUT:

You might also like