0% found this document useful (0 votes)
15 views10 pages

CPP Programs With Output

The document contains multiple C++ code examples demonstrating various programming concepts, including sorting arrays, calculating sums, function overloading, inheritance, file handling, exception handling, and binary file operations. Each example includes code snippets along with their expected output. The examples are structured to illustrate fundamental programming techniques and error handling in C++.

Uploaded by

khanrufee24
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)
15 views10 pages

CPP Programs With Output

The document contains multiple C++ code examples demonstrating various programming concepts, including sorting arrays, calculating sums, function overloading, inheritance, file handling, exception handling, and binary file operations. Each example includes code snippets along with their expected output. The examples are structured to illustrate fundamental programming techniques and error handling in C++.

Uploaded by

khanrufee24
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

1.

Sort Elements in Ascending and Descending Order

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int arr[] = {5, 2, 9, 1, 7};
int n = sizeof(arr)/sizeof(arr[0]);

sort(arr, arr + n);


cout << "Ascending: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;

sort(arr, arr + n, greater<int>());


cout << "Descending: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}

Output:
Ascending: 1 2 5 7 9
Descending: 9 7 5 2 1
2. Sum of Natural Numbers from 1 to n

#include <iostream>
using namespace std;

int main() {
int n = 5, sum = 0;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}

Output:
Sum = 15
3. Swap Values using Call by Reference

#include <iostream>
using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

int main() {
int x = 5, y = 10;
swap(x, y);
cout << "x = " << x << ", y = " << y;
return 0;
}

Output:
x = 10, y = 5
4. Function Overloading

#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

double add(double a, double b) {


return a + b;
}

int main() {
cout << "Int sum: " << add(5, 3) << endl;
cout << "Double sum: " << add(2.5, 3.7);
return 0;
}

Output:
Int sum: 8
Double sum: 6.2
5. Inheritance Example with Shape

#include <iostream>
using namespace std;

class Shape {
public:
void show() { cout << "This is a shape" << endl; }
};

class Polygon : public Shape {


public:
void show() { cout << "Polygon is a shape" << endl; }
};

class Rectangle : public Polygon {


public:
void show() { cout << "Rectangle is a polygon" << endl; }
};

class Triangle : public Polygon {


public:
void show() { cout << "Triangle is a polygon" << endl; }
};

class Square : public Rectangle {


public:
void show() { cout << "Square is a rectangle" << endl; }
};

int main() {
Shape s; Polygon p; Rectangle r; Triangle t; Square sq;
s.show(); p.show(); r.show(); t.show(); sq.show();
return 0;
}

Output:
This is a shape
Polygon is a shape
Rectangle is a polygon
Triangle is a polygon
Square is a rectangle
6. Multilevel Inheritance: Vehicle, FourWheeler, Car

#include <iostream>
using namespace std;

class Vehicle {
public:
void vehicle() { cout << "I am a vehicle" << endl; }
};

class FourWheeler : public Vehicle {


public:
void fourWheeler() { cout << "I have four wheels" << endl; }
};

class Car : public FourWheeler {


public:
void car() { cout << "I am a car" << endl; }
};

int main() {
Car c;
c.car();
c.fourWheeler();
c.vehicle();
return 0;
}

Output:
I am a car
I have four wheels
I am a vehicle
7. File Creation and Read in C++

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream file("example.txt");
if (!file) {
cout << "File not created!";
return 1;
}
file << "Hello, this is a test file.";
file.close();

ifstream readFile("example.txt");
string text;
while (getline(readFile, text)) {
cout << text << endl;
}
readFile.close();
return 0;
}

Output:
Hello, this is a test file.
8. Read/Write Time using Binary File

#include <iostream>
#include <fstream>
using namespace std;

class Time {
public:
int h, m, s;
};

int main() {
Time t1 = {10, 30, 45};
ofstream out("time.dat", ios::binary);
out.write((char*)&t1, sizeof(t1));
out.close();

Time t2;
ifstream in("time.dat", ios::binary);
in.read((char*)&t2, sizeof(t2));
in.close();

cout << "Time: " << t2.h << ":" << t2.m << ":" << t2.s;
return 0;
}

Output:
Time: 10:30:45
9. Exception Handling - Division by Zero

#include <iostream>
using namespace std;

void divide(int a, int b) {


if (b == 0) throw "Division by zero!";
cout << "Result: " << a / b << endl;
}

int main() {
try {
divide(10, 0);
} catch (const char* msg) {
cout << "Exception: " << msg << endl;
}
return 0;
}

Output:
Exception: Division by zero!
10. Array Index Out of Bounds Exception Handling

#include <iostream>
using namespace std;

int main() {
int arr[5] = {1, 2, 3, 4, 5};
try {
for (int i = 0; i <= 5; i++) {
if (i >= 5) throw "Array index out of bounds";
cout << arr[i] << " ";
}
} catch (const char* msg) {
cout << "\nException: " << msg << endl;
}
return 0;
}

Output:
1 2 3 4 5
Exception: Array index out of bounds

You might also like