C++ Exam Pratical
C++ Exam Pratical
Program:
#include <iostream.h>
class CirArea {
private:
float area, r;
public:
void getInput() {
cin >> r;
void findArea() {
area = 3.14 * r * r;
void display() {
cout << "Area of the circle = " << area << endl;
};
int main() {
CirArea x;
1
x.getInput();
x.findArea();
x.display();
return 0;
Output:
Result:
Thus the demonstration of class and object program was created successfully.
2
Ex:No:2
Function Overloading
Aim :
To implement function overloading.
Program:
#include <iostream.h>
class FunOver {
public:
void area(int x) {
int a = x * x;
cout << "Area of square = " << a << endl;
}
void area(int x, int y) {
int a = x * y;
cout << "Area of rectangle = " << a << endl;
}
void area(float x, float y) {
float a = 0.5 * x * y;
cout << "Area of triangle = " << a << endl;
}
};
int main() {
FunOver x;
cout << "......Area of square.......\n";
cout << "Enter the value of side\n";
int side;
cin >> side;
x.area(side);
cout << "\n ......Area of rectangle......\n";
cout << "Enter the value of length and breadth\n";
int length, breadth;
3
cin >> length >> breadth;
x.area(length, breadth);
cout << "\n........ Area of triangle.....\n";
cout << "Give the values for base and height \n";
float base, height;
cin >> base >> height;
x.area(base, height);
return 0;
}
Output:
......Area of square.......
Enter the value of side
4
Area of square = 16
......Area of rectangle......
Enter the value of length and breadth
45
Area of rectangle = 20
........ Area of triangle.....
Give the values for base and height
3.5 4.2
Area of triangle = 7.35
Result:
Thus the function overloading program was executed successfully.
4
Ex:No:3
Passing object function
Aim :
To write a C++ program to demonstrate the concept of passing object to function.
Program:
#include <iostream.h>
class PassObj {
int x, y;
public:
void getData() {
cout << "\nEnter x: ";
cin >> x;
cout << "\nEnter y: ";
cin >> y;
}
PassObj swap(PassObj r) {
PassObj temp;
temp.x = r.y;
temp.y = r.x;
return temp;
}
void display() {
cout << "\nx = " << x << endl;
cout << "y = " << y << endl;
}
};
int main() {
PassObj p, q;
cout << "\nGetting values for object p";
p.getData();
q = p.swap(p);
5
cout << "\nValues of object p" << endl;
p.display();
cout << "\nValues of object q" << endl;
q.display();
return 0;
}
Output:
Getting values for object p
Enter x: 3
Enter y: 7
Values of object p
x=3
y=7
Values of object q
x=7
y=3
Result:
Thus the concept of passing object to function program was executed successfully.
6
Ex:No:4
Friend function
Aim:
To find minimum of two numbers using friend function which accepts two values
from two different classes objects.
Program:
#include <iostream.h>
class second;
class first {
int f;
public:
void getvalue() {
std::cout << "\nEnter the value of the first object: ";
std::cin >> f;
}
friend void mintwo(first, second);
};
class second {
int s;
public:
void getvalue() {
std::cout << "\nEnter the value for the second object: ";
std::cin >> s;
}
friend void mintwo(first, second);
};
void mintwo(first f1, second s1) {
if (s1.s < f1.f) {
std::cout << "Second object value is lesser than the first object\n";
} else {
std::cout << "First object value is lesser than the second object\n";
7
}
}
int main() {
first x;
second y;
x.getvalue();
y.getvalue();
mintwo(x, y);
return 0;
}
Output:
Enter the value of the first object: 20
Enter the value for the second object: 10
Second object value is lesser than the first object
Result:
Thus a friend function concept of the program was executed successfully.
8
Ex:No:5
Constructor and Destructor
Aim:
To write a C++ program to demonstrate the concept of constructor and destructor.
Program:
#include <iostream.h>
class sample {
int x, y;
public:
sample() {
cout << "Default Constructor Called" << endl;
x = 3;
y = 4;
}
sample(int a, int b) {
cout << "Constructor with Args Called" << endl;
x = a;
y = b;
}
~sample() {
cout << "Destructor Called" << endl;
}
void Display() {
cout << "\n x = " << x << endl;
cout << "\n y = " << y << endl;
}
};
int main() {
sample s1;
s1.Display();
{
9
sample s2(8, 9);
s2.Display();
}
return 0;
}
Output:
Default Constructor Called
x=3
y=4
Constructor with Args Called
x=8
y=9
Destructor Called
Destructor Called
Result:
Thus the concept of constructor and destructor program was executed successfully.
10
Ex:No:6
Negated value
Aim:
To find the negated value of given vector.
Program:
#include <iostream.h>
class changesign {
int x, y, z;
public:
void getdata() {
cout << "Enter the values of x, y, z: \n";
cin >> x >> y >> z;
}
void display() {
cout << "(" << x << ", " << y << ", " << z << ")" << "\n";
}
void operator -() {
x = -x;
y = -y;
z = -z;
}
};
int main() {
changesign s;
s.getdata();
cout << "s = ";
s.display();
-s;
cout << "\n-s = ";
s.display();
return 0;
11
}
Output:
Enter the values of x, y, z:
123
s = (1, 2, 3)
-s = (-1, -2, -3)
Result:
Thus the concept of an unary operator overloading program was verified successfully.
12
Ex:No:7
Binary operator
Aim:
To add the two complex numbers using binary operator overloading concept in C++.
Program:
#include <iostream.h>
class complex {
int real;
int img;
public:
void getdata() {
cin >> real >> img;
}
void display() {
cout << real << "+" << img << "i" << "\n";
}
complex operator +(complex x) {
complex t;
t.real = real + x.real;
t.img = img + x.img;
return t;
}
};
int main() {
complex c1, c2, c3;
cout << "Enter the real and imaginary parts for c1\n";
c1.getdata();
cout << "Enter the real and imaginary parts for c2\n";
c2.getdata();
c3 = c1 + c2;
cout << "\nc1 is: ";
13
c1.display();
cout << "c2 is: ";
c2.display();
cout << "\nThe added complex number c3 is: ";
c3.display();
return 0;
}
Output:
Enter the real and imaginary parts for c1
23
Enter the real and imaginary parts for c2
45
c1 is: 2+3i
c2 is: 4+5i
The added complex number c3 is: 6+8i
Result:
Thus the concept of a binary operator overloading program was verified successfully.
14
Ex:No:8
Single Inheritance
Aim:
To write a C++ program to demonstrate the concept of single inheritance.
Program:
#include <iostream.h>
class Base {
public:
Base() {
cout << "Base Constructor Called" << endl;
}
void Display() {
cout << "Base Class Method" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived Constructor Called" << endl;
}
void Show() {
cout << "Derived Class Method" << endl;
}
};
int main() {
Base b;
b.Display();
Derived d;
d.Display();
d.Show();
return 0;
15
}
Output:
Base Constructor Called
Base Class Method
Base Constructor Called
Derived Constructor Called
Base Class Method
Derived Class Method
Result:
Thus the program to implement a single inheritance has been successfully verified and
executed.
16
Ex:No:9
Multiple Inheritance
Aim :
To implement multiple inheritance for family details.
Program:
#include <iostream.h>
class parent {
protected:
char father[25];
char mother[25];
};
class child {
protected:
char child1[25];
char child2[25];
};
class family : public parent, public child {
public:
void getdata() {
cout << "Enter the names of father, mother, child1, and child2:\n";
cin >> father >> mother >> child1 >> child2;
}
void display() {
cout << "...........Family Details........\n";
cout << "Father Name: " << father << endl;
cout << "Mother Name: " << mother << endl;
cout << "Child1 Name: " << child1 << endl;
cout << "Child2 Name: " << child2 << endl;
}
};
int main() {
17
family f;
f.getdata();
f.display();
return 0;
}
Output:
Enter the names of father, mother, child1, and child2:
John Mary Alice Bob
...........Family Details........
Father Name: John
Mother Name: Mary
Child1 Name: Alice
Child2 Name: Bob
Result:
Thus the program to implement a multiple inheritance has been successfully verified
and executed.
18
Ex:No:10
Virtual Functions
Aim:
To find arithmetic value of any two numeric values using virtual function concept in
C++.
Program:
#include <iostream.h>
class Add {
protected:
float a, b, c;
public:
Add(float x, float y) : a(x), b(y) {}
virtual void calculate() {
c = a + b;
cout << "a + b = " << c << "\n";
}
};
class Sub : public Add {
public:
Sub(float x, float y) : Add(x, y) {}
void calculate() override {
c = a - b;
cout << "a - b = " << c << "\n";
}
};
class Mul : public Sub {
public:
Mul(float x, float y) : Sub(x, y) {}
void calculate() override {
c = a * b;
cout << "a * b = " << c << "\n";
19
}
};
class Div : public Mul {
public:
Div(float x, float y) : Mul(x, y) {}
void calculate() override {
if (b != 0) {
c = a / b;
cout << "a / b = " << c << "\n";
} else {
cout << "Error: Division by zero is not allowed.\n";
}
}
};
int main() {
float a, b;
cout << "Enter the value of a and b: \n";
cin >> a >> b;
Add x(a, b);
x.calculate();
Sub y(a, b);
y.calculate();
Mul z(a, b);
z.calculate();
Div z1(a, b);
z1.calculate();
return 0;
}
20
Output:
Enter the value of a and b:
10
5
a + b = 15
a-b=5
a * b = 50
a/b=2
Result:
Thus the virtual function program was verified and executed successfully.
21
Ex:No:11
Biggest Number
Aim:
To write a C++ program to find the biggest number using command line arguments.
Program:
#include <iostream.h>
#include <cstdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
cout << "Error: Please provide at least one integer argument." << endl;
return 1;
}
int big = atoi(argv[1]);
for (int i = 2; i < argc; i++) {
if (atoi(argv[i]) > big) {
big = atoi(argv[i]);
}
}
cout << "Biggest value is: " << big << endl;
return 0;
}
Output:
program 10 20 5 15
Biggest value is: 20
Result:
Thus the biggest number was successfully found using command line arguments.
22
Ex:No:12
Exception Handling
Aim:
Write a program to handle exceptions in C++.
Program:
#include <iostream.h>
int main() {
double numerator, denominator, divide;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try {
if (denominator == 0) {
throw "Division by zero is not allowed!";
}
divide = numerator / denominator;
cout << numerator << " / " << denominator << " = " << divide << endl;
} catch (const char* msg) {
cout << "Error: " << msg << endl;
}
return 0;
}
23
Output:
Enter numerator: 10
Enter denominator: 2
10 / 2 = 5
Enter numerator: 10
Enter denominator: 0
Error: Division by zero is not allowed!
Result:
Thus the exception handling program was executed successfully.
24
Ex:No:13
Traverse an array
Aim:
Write a program traverse an array using pointers.
Program:
#include <iostream.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
cout << "\nTraversing an array:" << endl;
for (int i = 0; i < 5; i++) {
cout << *ptr << endl;
ptr++;
}
return 0;
}
Output:
Traversing an array:
10
20
30
40
50
Result:
Thus the traverse of an array program was verified and executed successfully.
25
Ex:No:14
Create a text file
Aim:
To write a C++ program to create a text file and write some content into it.
Program:
#include <iostream.h>
#include <fstream.h>
#include <cstring.h>
int main() {
char fname[20], str[200];
fstream fp;
cout << "Enter the name of the file: ";
cin.getline(fname, 20);
fp.open(fname, fstream::out);
if (!fp) {
cout << "Error occurred while opening the file." << endl;
return 0;
}
cout << "Enter the data (press Enter on a blank line to stop):" << endl;
cin.getline(str, 200);
while (strlen(str) > 0) {
fp << str << "\n";
cin.getline(str, 200);
}
fp.close();
cout << "Data successfully written to the file." << endl;
return 0;
}
26
Output:
Enter the name of the file: myfile.txt
Enter the data (press Enter on a blank line to stop):
Hello World
C++ Programming
[Blank Line]
Data successfully written to the file.
Result:
Thus the text file has been created and the content can be written into it successfully.
27
Ex:No:15
Existing text file
Aim:
To write a C++ program to open an existing text file and display its content.
Program:
#include <iostream.h>
#include <fstream.h>
#include <cstring.h>
int main() {
char ch;
const char *filename = "abc.txt";
ifstream file;
file.open(filename);
if (!file) {
cout << "Error: File could not be opened." << endl;
return -1;
}
while (file.get(ch)) {
cout << ch;
}
file.close();
return 0;
}
Output:
Hello, World!
Result:
Thus the text file has been opened and the content can be displayed successfully.
28