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

new (4)

The document contains multiple C++ programming examples, including exception handling, file reading and writing, function and class templates, and vector usage. It also includes an index of various programming tasks and concepts with corresponding dates and page numbers. The examples illustrate fundamental programming techniques and object-oriented principles.

Uploaded by

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

new (4)

The document contains multiple C++ programming examples, including exception handling, file reading and writing, function and class templates, and vector usage. It also includes an index of various programming tasks and concepts with corresponding dates and page numbers. The examples illustrate fundamental programming techniques and object-oriented principles.

Uploaded by

hi Me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Output:

Exception handling
#include <iostream>

using namespace std;

int divide(int a, int b)

if (b == 0) {

throw runtime_error("Division by zero is not possible");

return a / b;

int main()

{ int a,b;

cout<<"Enter numerator:

"; cin>>a;

cout<<"Enter denominator: ";

cin>>b;

try {

int result = divide(a, b);

cout << "Result: " << result << endl;

} catch (const runtime_error& e) {

cout << " error: " << e.what() << endl;

return 0;

}
FILE:

OUTPUT:
Reading from a file
#include <iostream>

#include <fstream>

#include <string>

using namespace

std;

int main() {

ifstream file("reading.txt");

string s;

while (getline(file,

s)) cout << s <<

endl;

file.close();

return 0;

}
OUTPUT:

FILE:
Writing to a file
#include <iostream>

#include <fstream>

#include <string>

using namespace

std;

int main() {

ofstream File("writing.txt");

File << "Hello, this is a test file." << endl;

File.close();

cout << "Data has been written to the file." << endl;

return 0;

}
Output:
Function template for two numbers

#include <iostream>

using namespace

std;

template<class T> T add(T &a,T &b)

T result =

a+b; return

result;

int main()

int i =2;

int j =3;

float m =

2.3; float n =

1.2;

cout<<"Addition of " <<i <<" and "<< j<< " is :"<<add(i,j)<<endl;

cout<<"Addition of " <<m <<" and " <<n <<" is

:"<<add(m,n); return 0;

}
Output:
Class template for two numbers
#include <iostream>

using namespace

std; template<class

T> class A

public:

T num1 =

5; T num2

= 6; void

add()

cout << "Addition of num1 and num2 : " << num1+num2<<endl;

};

int main()

A<int> d;

d.add();

return 0;

}
Output:
Using Vectors

#include <iostream>

#include <vector>

using namespace

std;

int main() {

vector<int> numbers = {10, 20, 30, 40, 50};

numbers.push_back(60);

cout << "Vector elements:

"; for (int num : numbers) {

cout << num << " ";

cout << endl;

return 0;

}
INDEX

S.N. Name of the program Date Page No. Faculty Sign


1 • Making calculator using Switch case
• To check leap year 20/08/24

• Calculate factorial of a number


2 •Print fibonacci series
•Reverse elements in an array 27/08/24

•Check if a number is prime or not


3 •Demonstrating the basic datatypes
•Sum of elements in an array 03/09/24

•Sum using functions


4 • function overloading with different
numbers and types of parameters
• function overloading with different 10/09/24
data types
• Area of square & rectangle using
function overloading
5 • Reverse a string
• Change letter to following letter 01/10/24

• Capitalize first letter of each word

6 •Pass objects by value


•Pass objects by reference 08/10/24

•Swap two numbers using pass by


reference
7 •Friend class
•Friend function 15/10/24

•Friend function of two classes


8 •Constructor overloading
•Copy constructor 22/10/24
•Destructor
9 •Single inheritance
•Multi-level inheritance 29/10/24
•Multiple inheritance
10 • Run time polymorphism (Virtual 05/11/24
function)
 Compile time polymorphism
(function overloading)
 Compile time polymorphism
(Operator Overloading)

11 • Exception handling
• Reading from a file
• Writing to a file 12/11/2024

12 • Function template for two numbers


• Class template for two numbers
• Using Vectors 12/11/2024

You might also like