Name: Yuvraj Singh
Reg. no. – 23bce10169
Slot no.:c11+f11+c12+f12+c13
Cpp project work
Experiment No. 1
Aim: Inline functions
Code:
#include<iostream>
using namespace std;
#include<conio.h>
inline float mul(float x, float y)
{ return (x*y); }
int main( )
float a=2.5;
float b=3.5;
cout<<"Multiplication is :"<<mul(a, b);
return 0;
Ouput:
Experiment No. 2
Aim: Default argument function
Code:
// Default argument function
#include <iostream>
using namespace std;
#include <conio.h>
int sum(int x, int y, int z = 0, int w = 0) // assigning default values
return (x + y + z + w);
void main()
cout << sum(10, 15) << endl; // Statement 1
cout << sum(10, 15, 25) << endl; // Statement 2
cout << sum(10, 15, 25, 30) << endl; // Statement 3
Output:
Experiment No. 3
Aim: Pass by reference
Code:
// Pass by reference
#include <iostream>
using namespace std;
void func(int &); //function prototyping
int main()
int a=10;
cout <<"Value of 'a' is :" <<a<<endl;
func(a);
cout << "Now value of 'a' is :" <<a<< endl;
return 0;
void func(int &m)
m=8;
Output:
Experiment No. 4
Aim: Public Member function
Code:
#include <iostream>
using namespace std;
class Public {
public:
void printdata(){
cout<<"this is public class"<<endl;
};
int main()
Public p;
[Link]();
return 0;
Output:
Experiment No.5
Aim: Private member Function
#include <iostream>
using namespace std;
class Private
int a;
int b;
void printdata()
cout << "this is private member of class" <<a<<b<< endl;
};
int main()
Private p;
[Link](); // will show error because print data declared privately
return 0;
Output:
Experiment No.6
Aim: Protected Member Function
Code:
#include <iostream>
using namespace std;
class Protected{
protected:
void printdata(){
cout<<"this is a protected class"<<endl;
};
int main()
Protected p;
[Link](); //give error because protected data cant be accessd directly
return 0;
Output:
Experiment No. 7
Aim: Constructer Function
Code:
#include <iostream>
using namespace std;
class Constructershow{
public:
Constructershow(){
cout<<"this is a constructer"<<endl;
};
int main()
Constructershow show;
return 0;
Output:
Experiment No. 8
Aim: Destructer function
#include<iostream>
using namespace std;
#include<conio.h>
int count=0,a=10,b=20;
class Example
public:
Example()
cout<<"\nI am Constructor\n";
++a;
++b;
++count;
cout<<"Values :"<<"a="<<a<<"\t"<<"b="<<b;
cout<<"\tcount"<<count;
~Example()
cout<<"\nI am Destructor\n";
cout<<"Values :"<<"a="<<a<<"\t"<<"b="<<b;
a--;
b--;
count --;
cout<<"\ncount"<<count;
};
int main()
Example obj1, obj2,obj3;
return 0;
Output:
Experiment No. 9
Aim: Constructer overloading
Code:
#include <iostream>
using namespace std;
class construct
public:
float area;
construct() // Constructor with no parameters
{ area = 0;
construct(int a, int b)
{ area = a * b; }
// Constructor with two parameters
void disp()
{ cout<< area<< endl; }
};
int main()
construct o;
construct o2( 10, 20);
[Link]();
[Link]();
}
Output:
Experiment No.10
Aim: Copy Constructer
Code:
#include <iostream>
using namespace std;
#include <string.h>
class student
int rno;
char name[50];
double fee;
public:
student(int, char[], double);
student(student& t) // copy constructor
rno = [Link];
strcpy(name, [Link]);
fee = [Link];
void display();
};
student::student(int no, char n[], double f)
rno = no;
strcpy(name, n);
fee = f;
void student::display()
{
cout << endl << rno << "\t" ;
cout<< name << "\t" << fee;
int main()
student s(1001, "Manjeet", 10000);
[Link]();
student m(s);
// copy constructor called
[Link]();
Output:
Experiment No. 11
Aim: Friend Class
Code:
#include <iostream>
using namespace std;
class test
private:
int private_variable;
protected:
int protected_variable;
public:
test()
private_variable = 10;
protected_variable = 99;
friend class retest; // friend class
};
class retest
public:
void display(test &t1)
cout << "The value of Private Variable = "<<endl;
cout << t1.private_variable << endl;
cout << "The value of Protected Variable = "<<endl;
cout<< t1.protected_variable;
};
int main()
test t1;
retest rt1;
[Link](t1);
return 0;
}
Aim: Friend function
Code:
#include<iostream>
using namespace std;
class ABC ;// forward declaration
class XYZ
int x;
public:
void setvalue (int i) { x=i; }
friend void max(XYZ, ABC);
};
class ABC
public:
int a;
void setvalue (int i) { a=i; }
friend void max(XYZ, ABC);
};
void max (XYZ m, ABC n)
if (m.x>n.a) // Definition of friend
cout<<m.x;
else
cout<<n.a;
int main( )
{
ABC abc;
[Link](10);
XYZ xyz;
[Link] (20);
max(xyz, abc);
return 0;
}
Experiment No. 12
Aim: Dynamic Object
Code:
#include <iostream>
using namespace std;
class Test
int a, b;
// Data members
public:
// data members of class
Test() // Constructor to initialize
cout << "Constructor is called" << endl;
a = 1;
b = 2;
};
~Test()
// Destructor
cout << "Destructor is called" << endl;
void show() // Function to print values of data members
cout << "a = " << a << endl;
cout << "b = " << b << endl;
};
int main()
{
Test *ptr;
ptr = new Test;
ptr->show();
delete ptr;
return 0;
Output:
Experiment No. 13
Aim: Container class
Code:
#include <iostream>
using namespace std;
class first
public:
void showf()
cout << "Hello from first class\n";
};
class second // Container class
first f; // creating object of first
public:
second() // constructor
[Link]();// calling function of first class
};
int main()
second s; // creating object of second
}
Output:
Experiment No. 14(polymorphism)
Aim: Compile time polymorphism
Code:
#include<iostream>
using namespace std;
#include<conio.h>
class test
public:
void sum(int a,int b)
cout<<"...compile time polymorphism-function overloading...\n\n";
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Sum is: "<< a+b<<"\n";
void sum(int a, int b, int c)
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Enter the value of c:";
cin>>c;
cout<<" Sum is: "<< a+b+c<<"\n\n";
}
};
int main()
int x,y,z,i,j;
test t;
[Link](i,j);
[Link](x,y,z);
getch();
Output:
Aim: Run time polymorphism
Code:
#include<iostream>
using namespace std;
#include<conio.h>
class Base
public:
int i,j;
virtual void show()
cout<<" I am class Base \n";
cout<<"enter the value of i: ";
cin>>i;
cout<<"enter the value of j: ";
cin>>j;
cout<<"Answer is: "<<i+j;
};
class Derived: public Base
public:
int i,j;
void show()
cout<<"I am class Derived \n";
cout<<"enter the value of i: ";
cin>>i;
cout<<"enter the value of j: ";
cin>>j;
cout<<"Answer is: "<<i*j;
};
int main()
Base b;
Base *p,*q;
p=&b;
p->show();
cout<<"\n\n";
Derived d;
q=&d;
q->show();
return 0;
Output:
Aim: Function overloading
Code:
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
public:
void area(int);
void area(int,int);
void area(float ,int,int);
};
void fn::area(int a)
cout<<"Area of Circle:";
cout<<pi*a*a;
void fn::area(int a,int b)
cout<<"Area of rectangle:";
cout<<a*b;
void fn::area(float t,int a,int b)
cout<<"Area of triangle:" ;
cout<<t*a*b;
}
int main()
int a,b,r;
fn obj;
cout<<"Function Overloading\n\n";
cout<<"Enter Radius of the Circle:"<<endl;
cin>>r;
[Link](r);
cout<<"Enter 1st Sides of the Rectangle:"<<endl;
cin>>a;
cout<<"Enter 2nd Sides of the Rectangle:"<<endl;
cin>>b;
[Link](a,b);
cout<<"Enter 1st Sides of the Triangle:"<<endl;
cin>>a;
cout<<"Enter 2nd Sides of the Triangle:"<<endl;
cin>>b;
[Link](0.5,a,b);
return 0;
}
Output:
Aim: Operator overloading
Code:
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
class Distance
private:
int feet;
int inches;
public:
Distance()
feet = 0;
inches = 0;
Distance(int f, int i)
feet = f;
inches = i;
void displayDistance()
cout<<"Entered feet value is: ";
cout << feet<<"\n";
cout<<"Entered inches value is: ";\
cout << inches<<"\n\n";
}
Distance operator-()
feet = -feet;
inches = -inches;
return Distance(feet, inches);
};
int main()
Distance D1(11, 10), D2(-5,
11);
-D1;
[Link]();-D2;
[Link]();
getch();
Output:
Experiment No. 15(inheritance)
Aim: single inheritence
Code:
#include <iostream>
using namespace std;
#include <conio.h>
class A
public:
int i;
void getdata()
cout << "\nI am from Class A\n"
<< endl;
cout << "Enter the value of i:" ;
cin >>i;
};
class B : public A
public:
void showdata()
cout << "\nI am from Class B\n";
i++;
cout << "\n"
<< "Entered value is incremented by 1 so i= " << i;
}
};
int main()
B obj;
[Link]();
[Link]();
getch();
Output:
Aim: Multilevel inheritance
Code:
#include<conio.h>
#include<iostream>
using namespace std;
class A
public:
int i,j,k;
void getdata()
cout<<"\nI am from Class A\n";
cout<<"Enter the value of i: ";
cin>>i;
cout<<"Enter the value of j: ";
cin>>j;
k=i+j;
cout<<"Answer is ="<<k<<"\n";
};
class B:public A
public:
int k,l;
void putdata()
cout<<"\nI am from Class B\n";
cout<<"Enter the value of k:";
cin>>k;
l=i*k;
cout<<"Answer is ="<<l<<"\n";
};
class C:public B
public:
int x;
void showdata()
cout<<"\nI am from Class C\n";
x=j*k;
cout<<"\n"<<"Answer is: "<<x;
};
int main()
class C obj;
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Aim: Multiple inheritance
Code:
#include<iostream>
using namespace std;
#include<conio.h>
class A
public:
int i,j;
void getdata()
cout<<"\nI am from Class A\n";
cout<<"Enter the value of i: ";
cin>>i;
cout<<"Enter the value of j:" ;
cin>>j;
};
class B
public:
int k;
void putdata()
cout<<"\nI am from Class B\n";
cout<<"Enter the value of k: ";
cin>>k;
}
};
class D:public A, public B
public:
int x;
void showdata()
cout<<"\nI am from Class D\n";
x=i*j*k;
cout<<"\n"<<"Answer is: "<<x;
};
int main()
D obj;
[Link]();
[Link]();
[Link]();
getch();
Output:
Aim: Hiearchial inheritance
Code:
#include<iostream>
using namespace std;
#include<conio.h>
class A
public:
int i,j;
void getdata()
cout<<"\nI am from Class A\n";
cout<<"Enter the value of i: ";
cin>>i;
cout<<"Enter the value of j: ";
cin>>j;
};
class B:public A
public:
int l;
void putdata()
cout<<"\nI am from Class B\n";
cout<<"Square of i from class A is : ";
l=i*i;
cout<<l<<"\n";
}
};
class C:public A
public:
int x;
void showdata()
cout<<"\nI am from Class C\n";
cout<<"Square of j from class A is : ";
x=j*j;
cout<<x<<"\n";
};
class D:public A
public:
int y;
void data()
cout<<"\nI am from Class D\n";
cout<<"Multiplication of i & j from class A is : ";
y=i*j;
cout<<y<<"\n";
};
int main()
{
B o;
C ob;
D obj;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
getch();
Output:
Aim: Hybrid inheritance
#include <iostream>
using namespace std;
#include <conio.h>
class A
public:
int i, j;
void getdata()
cout << "\nI am from Class A\n";
cout << "Enter the value of i: ";
cin >> i;
cout << "Enter the value of j: ";
cin >> j;
};
class B : public virtual A
public:
int l;
void putdata()
cout << "\nI am from Class B\n";
cout << "Square of i from class A is : ";
l = i * i;
cout << l << "\n";
}
};
class C : public virtual A
public:
int x;
void showdata()
cout << "\nI am from Class C\n";
cout << "Square of j from class A is : ";
x = j * j;
cout << x << "\n";
};
class D : public B, public C
public:
int y;
void data()
cout << "\nI am from Class D\n";
cout << "Multiplication of l & x from class B &C is : ";
y = x * l;
cout << y << "\n";
};
int main()
D obj;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
Output:
Experiment No. 16
Aim: Abstract Base Class
Code:
#include <iostream>
using namespace std;
class Base
int x;
public:
virtual void fun() = 0; // pure virtual function
int getX() { return x; }
};
class Derived : public Base //class inherits from Base and implements fun()
int y;
public:
void fun() // implementation of the pure virtual function
{ cout << "fun() called"; }
};
int main(void)
Derived d;
[Link]();
return 0;
}
Experiment No. 17
Aim: Design a class to represent a bank account.
Code:
#include <iostream>
#include <string>
class BankAccount {
private:
std::string name;
std::string account_number;
std::string account_type;
double balance;
public:
// Constructor to initialize the bank account with initial values
BankAccount(std::string depositor_name, std::string acc_number, std::string acc_type, double
initial_balance = 0.0)
: name(depositor_name), account_number(acc_number), account_type(acc_type),
balance(initial_balance) {}
// Method to deposit an amount into the account
void deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposited " << amount << ". New balance: " << balance << std::endl;
} else {
std::cout << "Deposit amount must be positive." << std::endl;
}
// Method to withdraw an amount from the account after checking balance
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrew " << amount << ". New balance: " << balance << std::endl;
} else if (amount > balance) {
std::cout << "Insufficient balance." << std::endl;
} else {
std::cout << "Withdrawal amount must be positive." << std::endl;
// Method to display the name of the depositor and the current balance
void display() const {
std::cout << "Name: " << name << std::endl;
std::cout << "Balance: " << balance << std::endl;
};
// Example usage
int main() {
// Creating an account with initial values
BankAccount account("John Doe", "123456789", "Savings", 1000.0);
// Displaying initial details
[Link]();
// Depositing money
[Link](500);
// Withdrawing money
[Link](200);
// Displaying updated details
[Link]();
return 0;
Output:
Experiment No.18
Aim: Guess-the-number-game
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
void playGame() {
// Initialize random seed
std::srand(static_cast<unsigned int>(std::time(nullptr)));
// Generate a random number between 1 and 1000
int numberToGuess = std::rand() % 1000 + 1;
int guess = 0;
std::cout << "I have a number between 1 and 1000.\n";
std::cout << "Can you guess my number?\n";
std::cout << "Please type your first guess: ";
// Loop until the player guesses the correct number
while (true) {
std::cin >> guess;
if (guess == numberToGuess) {
std::cout << "Excellent! You guessed the number!\n";
break;
} else if (guess < numberToGuess) {
std::cout << "Too low. Try again: ";
} else {
std::cout << "Too high. Try again: ";
int main() {
char playAgain = 'y';
// Loop to allow the player to play multiple times
while (playAgain == 'y' || playAgain == 'Y') {
playGame();
std::cout << "Would you like to play again (y or n)? ";
std::cin >> playAgain;
std::cout << "Thank you for playing!\n";
return 0;
Output:
Experiment No.19
Aim: bank account system
Code:
#include <iostream>
#include <string>
#include <cmath>
class BankAccount {
protected:
std::string name;
std::string account_number;
double balance;
public:
BankAccount(const std::string& depositor_name, const std::string& acc_number, double
initial_balance)
: name(depositor_name), account_number(acc_number), balance(initial_balance) {}
virtual void deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposited " << amount << ". New balance: " << balance << std::endl;
} else {
std::cout << "Deposit amount must be positive." << std::endl;
virtual void withdraw(double amount) = 0; // Pure virtual function
virtual void display() const {
std::cout << "Name: " << name << std::endl;
std::cout << "Account Number: " << account_number << std::endl;
std::cout << "Balance: " << balance << std::endl;
virtual ~BankAccount() = default;
};
class SavingsAccount : public BankAccount {
private:
double interest_rate; // Annual interest rate
public:
SavingsAccount(const std::string& depositor_name, const std::string& acc_number, double
initial_balance, double rate)
: BankAccount(depositor_name, acc_number, initial_balance), interest_rate(rate) {}
void applyInterest() {
balance += balance * (interest_rate / 100);
void withdraw(double amount) override {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrew " << amount << ". New balance: " << balance << std::endl;
} else if (amount > balance) {
std::cout << "Insufficient balance." << std::endl;
} else {
std::cout << "Withdrawal amount must be positive." << std::endl;
void display() const override {
std::cout << "Savings Account:" << std::endl;
BankAccount::display();
std::cout << "Interest Rate: " << interest_rate << "%" << std::endl;
};
class CurrentAccount : public BankAccount {
private:
double minimum_balance;
double service_charge;
public:
CurrentAccount(const std::string& depositor_name, const std::string& acc_number, double
initial_balance, double min_balance, double charge)
: BankAccount(depositor_name, acc_number, initial_balance), minimum_balance(min_balance),
service_charge(charge) {}
void withdraw(double amount) override {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrew " << amount << ". New balance: " << balance << std::endl;
if (balance < minimum_balance) {
balance -= service_charge;
std::cout << "Balance fell below minimum. Service charge of " << service_charge << " applied.
New balance: " << balance << std::endl;
}
} else if (amount > balance) {
std::cout << "Insufficient balance." << std::endl;
} else {
std::cout << "Withdrawal amount must be positive." << std::endl;
void display() const override {
std::cout << "Current Account:" << std::endl;
BankAccount::display();
std::cout << "Minimum Balance: " << minimum_balance << std::endl;
std::cout << "Service Charge: " << service_charge << std::endl;
};
int main() {
SavingsAccount savings("Alice", "SA123456", 1000.0, 5.0);
CurrentAccount current("Bob", "CA123456", 500.0, 200.0, 50.0);
std::cout << "Savings Account Details:" << std::endl;
[Link]();
[Link](200);
[Link](100);
[Link]();
[Link]();
std::cout << "\nCurrent Account Details:" << std::endl;
[Link]();
[Link](300);
[Link](100);
[Link](600);
[Link]();
return 0;
}
Experiment No. 20
Aim: program that reads ballots for an election contested by 5 candidates
Code:
#include <iostream>
#include <vector>
int main() {
const int numCandidates = 5;
std::vector<int> votes(numCandidates, 0);
int spoiltBallots = 0;
int ballot;
std::cout << "Enter the ballots (enter 0 to stop):" << std::endl;
while (true) {
std::cin >> ballot;
if (ballot == 0) {
break;
} else if (ballot >= 1 && ballot <= numCandidates) {
votes[ballot - 1]++;
} else {
spoiltBallots++;
std::cout << "\nElection Results:" << std::endl;
for (int i = 0; i < numCandidates; ++i) {
std::cout << "Candidate " << (i + 1) << ": " << votes[i] << " votes" << std::endl;
}
std::cout << "Spoilt Ballots: " << spoiltBallots << std::endl;
return 0;
Output:
Experiment No. 21
Aim:
Code:
#include <iostream>
class Date {
private:
int day, month, year;
bool isLeapYear(int year) const {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
int daysInMonth(int month, int year) const {
switch (month) {
case 2: return isLeapYear(year) ? 29 : 28;
case 4: case 6: case 9: case 11: return 30;
default: return 31;
public:
Date(int d, int m, int y) : day(d), month(m), year(y) {}
bool operator<(const Date& other) const { return year < [Link] || (year == [Link] && (month <
[Link] || (month == [Link] && day < [Link]))); }
bool operator<=(const Date& other) const { return *this < other || *this == other; }
bool operator>(const Date& other) const { return !(*this <= other); }
bool operator>=(const Date& other) const { return !(*this < other); }
bool operator==(const Date& other) const { return day == [Link] && month == [Link] &&
year == [Link]; }
bool operator!=(const Date& other) const { return !(*this == other); }
Date& operator++() {
if (++day > daysInMonth(month, year)) {
day = 1;
if (++month > 12) {
month = 1;
++year;
return *this;
Date operator+(int days) const {
Date newDate = *this;
while (days-- > 0) ++newDate;
return newDate;
operator int() const {
int daysElapsed = day;
for (int i = 1; i < month; ++i)
daysElapsed += daysInMonth(i, year);
return daysElapsed;
void print() const {
std::cout << day << "/" << month << "/" << year << std::endl;
};
int main() {
Date dt1(25, 12, 2023), dt2(1, 1, 2024);
if (dt1 < dt2) std::cout << "dt1 is less than dt2\n";
if (dt1 <= dt2) std::cout << "dt1 is less than or equal to dt2\n";
if (dt2 > dt1) std::cout << "dt2 is greater than dt1\n";
if (dt2 >= dt1) std::cout << "dt2 is greater than or equal to dt1\n";
if (dt1 != dt2) std::cout << "dt1 is not equal to dt2\n";
std::cout << "Date before increment: "; [Link]();
++dt1;
std::cout << "Date after increment: "; [Link]();
Date dt3 = dt1 + 10;
std::cout << "Date after adding 10 days: "; [Link]();
int daysElapsed = dt3;
std::cout << "Days elapsed in current year for dt3: " << daysElapsed << std::endl;
return 0;
Output:
Experiment No.22
Aim:
Code:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;
// Convert the string to uppercase to ensure case-insensitive sorting
std::transform([Link](), [Link](), [Link](), ::toupper);
// Sort the string alphabetically
std::sort([Link](), [Link]());
std::cout << "String in alphabetical order: " << input << std::endl;
return 0;
Output:
Experiment No. 23
Aim:
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
struct Book {
std::string book_id;
std::string author_name;
double price;
int no_of_pages;
std::string publisher;
int year_of_publishing;
void display() const {
std::cout << book_id << " " << author_name << " " << price << " " << no_of_pages << " "
<< publisher << " " << year_of_publishing << std::endl;
};
// Comparator function to sort by author_name
bool compareByAuthor(const Book &a, const Book &b) {
return a.author_name < b.author_name;
// Function to read books from a file
std::vector<Book> readBooksFromFile(const std::string &filename) {
std::ifstream file(filename);
std::vector<Book> books;
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
Book book;
iss >> book.book_id >> book.author_name >> [Link] >> book.no_of_pages >> [Link]
>> book.year_of_publishing;
books.push_back(book);
return books;
// Function to write books to a file
void writeBooksToFile(const std::string &filename, const std::vector<Book> &books) {
std::ofstream file(filename);
for (const auto &book : books) {
file << book.book_id << " " << book.author_name << " " << [Link] << " " << book.no_of_pages
<< " "
<< [Link] << " " << book.year_of_publishing << "\n";
int main() {
std::string input_filename = "[Link]";
std::string output_filename = "sorted_books.txt";
// Read books from file
std::vector<Book> books = readBooksFromFile(input_filename);
// Sort books by author_name
std::sort([Link](), [Link](), compareByAuthor);
// Write sorted books to file
writeBooksToFile(output_filename, books);
// Display sorted books
std::cout << "Sorted books by author name:" << std::endl;
for (const auto &book : books) {
[Link]();
return 0;
Output:
Experiment No. 24
Aim: C++ class template for a Vector that performs the required operations
Code:
#include <iostream>
#include <vector>
#include <limits>
template <typename T>
class Vector {
private:
std::vector<T> elements;
public:
void addElement(T element) {
elements.push_back(element);
// Find the smallest element in the Vector
T findSmallest() const {
if ([Link]()) {
throw std::runtime_error("Vector is empty");
T smallest = elements[0];
for (const T& element : elements) {
if (element < smallest) {
smallest = element;
return smallest;
}
// Search for an element in the Vector
bool searchElement(T element) const {
for (const T& elem : elements) {
if (elem == element) {
return true;
return false;
// Find the average of the elements in the Vector
double findAverage() const {
if ([Link]()) {
throw std::runtime_error("Vector is empty");
T sum = 0;
for (const T& element : elements) {
sum += element;
return static_cast<double>(sum) / [Link]();
void display() const {
for (const T& element : elements) {
std::cout << element << " ";
std::cout << std::endl;
}
};
int main() {
Vector<int> intVector;
// Adding elements to the vector
[Link](10);
[Link](20);
[Link](5);
[Link](15);
// Displaying the vector elements
std::cout << "Vector elements: ";
[Link]();
// Finding the smallest element
try {
std::cout << "Smallest element: " << [Link]() << std::endl;
} catch (const std::runtime_error& e) {
std::cerr << [Link]() << std::endl;
// Searching for an element
int searchElement = 20;
if ([Link](searchElement)) {
std::cout << "Element " << searchElement << " found in the vector." << std::endl;
} else {
std::cout << "Element " << searchElement << " not found in the vector." << std::endl;
}
// Finding the average of the elements
try {
std::cout << "Average of elements: " << [Link]() << std::endl;
} catch (const std::runtime_error& e) {
std::cerr << [Link]() << std::endl;
return 0;
Output:
Experiment No. 25
Aim: Design a generic function for finding the largest of three numbers.
Code:
#include <iostream>
// Generic function to find the largest of three numbers
template <typename T>
T findLargest(T a, T b, T c) {
T largest = a; // Assume 'a' is the largest initially
if (b > largest) largest = b;
if (c > largest) largest = c;
return largest;
int main() {
// Test with integers
int a = 10, b = 20, c = 15;
std::cout << "Largest integer: " << findLargest(a, b, c) << std::endl;
// Test with doubles
double x = 10.5, y = 20.5, z = 15.5;
std::cout << "Largest double: " << findLargest(x, y, z) << std::endl;
// Test with characters
char p = 'a', q = 'z', r = 'm';
std::cout << "Largest character: " << findLargest(p, q, r) << std::endl;
return 0;
}
Output: