01 Name and age input
Lab 1. Write a program that prompts the user for their name and age, and then outputs a message that says "Hello, [name]! You are [age] years old."
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
cout << "Hello, " << name << "! You are "
<< age << " years old." << endl;
return 0;
}02 For loop: 1 to 10
Lab 2. Write a program that uses a for loop to output the numbers from 1 to 10.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++)
cout << i << " ";
return 0;
}03 While loop: 1 to 10
Lab 3. Write a program that uses a while loop to output the numbers from 1 to 10.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 10) {
cout << i << " ";
i++;
}
return 0;
}04 Array of 5 integers and sum
Lab 4. Write a program that uses an array to store 5 integers, and then outputs the sum of those integers.
#include <iostream>
using namespace std;
int main() {
int a[5], sum = 0;
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> a[i];
sum += a[i];
}
cout << "Sum = " << sum << endl;
return 0;
}05 Function to return the sum of two integers
Lab 5. Write a program that defines a function that takes two integers as arguments and returns their sum. then, call that function and output the result.
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
cout << "Sum = " << add(10, 20) << endl;
return 0;
}06 Calculator function using an operator character
Lab 6. Write a program that define a function that takes two integers and a character as arguments and returns the result of the operation specified by the character. The possible characters are '+', '-', '*', '/'.
#include <iostream>
using namespace std;
double calculate(int a, int b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b != 0) return static_cast<double>(a) / b;
cout << "Error: division by zero." << endl;
return 0;
default:
cout << "Invalid operator." << endl;
return 0;
}
}
int main() {
cout << calculate(20, 5, '+') << endl;
cout << calculate(20, 5, '-') << endl;
cout << calculate(20, 5, '*') << endl;
cout << calculate(20, 5, '/') << endl;
return 0;
}07 Function overloading
Lab 7. Write a program that demonstrates function overloading by defining two functions with the same name but different parameter lists. One function should take two integers as arguments and return their sum, and the other function should take three integers as arguments and return their sum. Call both functions in the main () function and print the results.
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
int main() {
cout << "Two integers: " << add(10, 20) << endl;
cout << "Three integers: " << add(10, 20, 30) << endl;
return 0;
}08 Function with a default string argument
Lab 8. Write a program that defines a function that takes two arguments: an integer and a string. The function should have a default argument for the string, so that if it is not provided when the function is called, the default value "Default String" will be used.
#include <iostream>
#include <string>
using namespace std;
void display(int number, string text = "Default String") {
cout << "Integer: " << number
<< ", String: " << text << endl;
}
int main() {
display(10);
display(20, "Hello C++");
return 0;
}09 Local, global, static and extern variables
Lab 9. Write a program that demonstrates the use of the different storage classes in C++ (local, global, static, and extern). Define a variable with each storage class and print the value of the variable in the main () function.
#include <iostream>
using namespace std;
int globalVar = 100; // global variable
extern int externalVar; // extern declaration
void show() {
int localVar = 10; // local variable
static int staticVar = 20; // static local variable
cout << "Local: " << localVar << endl;
cout << "Static: " << staticVar << endl;
cout << "Global: " << globalVar << endl;
cout << "Extern: " << externalVar << endl;
}
int externalVar = 40; // definition of the extern variable
int main() {
show();
return 0;
}
10 Function returning a pointer to an integer
Lab 10. Write a program that defines a function that takes an integer as an argument and returns a pointer to that integer.
#include <iostream>
using namespace std;
int* getPointer(int &x) {
return &x;
}
int main() {
int n = 25;
int *p = getPointer(n);
cout << "Value = " << *p << endl;
return 0;
}11 Function returning a reference to a string
Lab 11. Write a program that defines a function that takes a string as an argument and returns a reference to that string.
#include <iostream>
#include <string>
using namespace std;
string& getString(string &text) {
return text;
}
int main() {
string message = "Hello";
string &ref = getString(message);
ref += " C++";
cout << message << endl;
return 0;
}12 Inline function for maximum of three integers
Lab 12. Write a program that defines an inline function that takes three integers as arguments and returns the maximum of those integers. Call this function in the main () function and print the result.
#include <iostream>
using namespace std;
inline int maximum(int a, int b, int c) {
int maxValue = a;
if (b > maxValue) maxValue = b;
if (c > maxValue) maxValue = c;
return maxValue;
}
int main() {
cout << "Maximum = " << maximum(12, 45, 30) << endl;
return 0;
}13 Class Interest
Lab 13. Define a class Interest with following specifications: a. Data Members i. principal ii. time iii. rate b. Member Functions i. getdata() to assign initial values ii. display () to display values iii. Findinterest() to find and display the interest
#include <iostream>
using namespace std;
class Interest {
float principal, time, rate;
public:
void getdata() {
cout << "Enter principal, time and rate: ";
cin >> principal >> time >> rate;
}
void display() {
cout << "Principal = " << principal << endl;
cout << "Time = " << time << endl;
cout << "Rate = " << rate << "%" << endl;
}
void Findinterest() {
float interest = (principal * time * rate) / 100;
cout << "Simple Interest = " << interest << endl;
}
};
int main() {
Interest obj;
obj.getdata();
obj.display();
obj.Findinterest();
return 0;
}14 Class Room
Lab 14. Define a class Room with following specifications: a. Data Members i. length ii. width b. Member Functions i. getdata() to assign initial values ii. display () to display length and width iii. area () to find and display the area
#include <iostream>
using namespace std;
class Room {
float length, width;
public:
void getdata() {
cout << "Enter length and width: ";
cin >> length >> width;
}
void display() {
cout << "Length = " << length << endl;
cout << "Width = " << width << endl;
}
void area() {
cout << "Area = " << length * width << endl;
}
};
int main() {
Room r;
r.getdata();
r.display();
r.area();
return 0;
}15 Distance class with constructor and addition
Lab 15. Create a class called Distance with two data members inch and feet. Provide Constructor and different member function with the following operations. ▪ To input data for Distance objects. ▪ To show the data of Distance objects. ▪ Member function to add two Distance object
#include <iostream>
using namespace std;
class Distance {
int feet, inch;
public:
Distance(int f = 0, int i = 0) : feet(f), inch(i) {}
void input() {
cout << "Enter feet and inches: ";
cin >> feet >> inch;
}
void show() {
cout << feet << " feet " << inch << " inches" << endl;
}
void add(Distance d1, Distance d2) {
feet = d1.feet + d2.feet;
inch = d1.inch + d2.inch;
if (inch >= 12) {
feet += inch / 12;
inch %= 12;
}
}
};
int main() {
Distance d1, d2, d3;
d1.input();
d2.input();
d3.add(d1, d2);
cout << "First: "; d1.show();
cout << "Second: "; d2.show();
cout << "Sum: "; d3.show();
return 0;
}16 Destruction in reverse order of creation
Lab 16. Write a program to demonstrate the objects are destroyed in the reverse order from their creation in the constructor.
#include <iostream>
using namespace std;
class Demo {
int id;
public:
Demo(int n) : id(n) {
cout << "Constructor: Object " << id << endl;
}
~Demo() {
cout << "Destructor: Object " << id << endl;
}
};
int main() {
Demo a(1);
Demo b(2);
Demo c(3);
return 0;
}
17 Complex number addition
Lab 17. Write a class Complex to represent complex numbers with real and imaginary parts. Define a member function to add two complex numbers. Pass objects of the Complex class to this function and display the result.
#include <iostream>
using namespace std;
class Complex {
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
Complex add(Complex c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << real;
if (imag >= 0) cout << " + " << imag;
else cout << " - " << -imag;
cout << "i" << endl;
}
};
int main() {
Complex c1(3, 4), c2(5, 2);
Complex c3 = c1.add(c2);
cout << "Sum = ";
c3.display();
return 0;
}18 Static counter for number of objects
Lab 18. Create a class Counter with a static data member to count the number of objects created. Define a member function to display the count. Create multiple objects of this class and display the count after each creation.
#include <iostream>
using namespace std;
class Counter {
static int count;
public:
Counter() {
++count;
}
void display() {
cout << "Objects created = " << count << endl;
}
};
int Counter::count = 0;
int main() {
Counter c1;
c1.display();
Counter c2;
c2.display();
Counter c3;
c3.display();
return 0;
}19 BankAccount with functions defined outside the class
Lab 19. Define a class BankAccount with data members for the account number and balance. Implement member functions to deposit and withdraw money, defined outside the class using the scope resolution operator. Create an object and perform some transactions to demonstrate these functions.
#include <iostream>
using namespace std;
class BankAccount {
long accountNumber;
double balance;
public:
BankAccount(long acc, double bal) : accountNumber(acc), balance(bal) {}
void deposit(double amount);
void withdraw(double amount);
void display();
};
void BankAccount::deposit(double amount) {
if (amount > 0)
balance += amount;
}
void BankAccount::withdraw(double amount) {
if (amount > 0 && amount <= balance)
balance -= amount;
else
cout << "Invalid withdrawal." << endl;
}
void BankAccount::display() {
cout << "Account Number: " << accountNumber << endl;
cout << "Balance: " << balance << endl;
}
int main() {
BankAccount account(10001, 5000);
account.deposit(1500);
account.withdraw(2000);
account.display();
return 0;
}20 Default, parameterized and copy constructors
Lab 20. Write a program for illustrating default constructor, parameterized constructor and copy constructor.
#include <iostream>
using namespace std;
class Demo {
int value;
public:
// Default constructor
Demo() : value(0) {
cout << "Default constructor" << endl;
}
// Parameterized constructor
Demo(int v) : value(v) {
cout << "Parameterized constructor" << endl;
}
// Copy constructor
Demo(const Demo &d) : value(d.value) {
cout << "Copy constructor" << endl;
}
void display() {
cout << "Value = " << value << endl;
}
};
int main() {
Demo a;
Demo b(50);
Demo c = b;
a.display();
b.display();
c.display();
return 0;
}21 Add private data members of two classes using a friend function
Lab 21. WAP to add, private data members of two classes using friend function.
#include <iostream>
using namespace std;
class B;
class A {
int x;
public:
A(int v) : x(v) {}
friend int add(A, B);
};
class B {
int y;
public:
B(int v) : y(v) {}
friend int add(A, B);
};
int add(A a, B b) {
return a.x + b.y;
}
int main() {
A a(10);
B b(20);
cout << "Sum = " << add(a, b) << endl;
return 0;
}22 Swap private data members of two classes using a friend function
Lab 22. WAP to swap private data members of two classes using friend function.
#include <iostream>
using namespace std;
class B;
class A {
int x;
public:
A(int v) : x(v) {}
friend void swapData(A&, B&);
void display() { cout << "A = " << x << endl; }
};
class B {
int y;
public:
B(int v) : y(v) {}
friend void swapData(A&, B&);
void display() { cout << "B = " << y << endl; }
};
void swapData(A &a, B &b) {
int temp = a.x;
a.x = b.y;
b.y = temp;
}
int main() {
A a(10);
B b(20);
cout << "Before swap:" << endl;
a.display();
b.display();
swapData(a, b);
cout << "After swap:" << endl;
a.display();
b.display();
return 0;
}23 Overload == operator using a friend function
Lab 23. WAP to overload == operator using friend function.
#include <iostream>
using namespace std;
class Number {
int value;
public:
Number(int v) : value(v) {}
friend bool operator==(const Number&, const Number&);
};
bool operator==(const Number &a, const Number &b) {
return a.value == b.value;
}
int main() {
Number n1(10), n2(10);
if (n1 == n2)
cout << "Objects are equal." << endl;
else
cout << "Objects are not equal." << endl;
return 0;
}24 Unary minus operator for a Distance object
Lab 24. WAP to overload unary minus (-) operator to invert sign of data members of a distance object.
#include <iostream>
using namespace std;
class Distance {
int feet, inch;
public:
Distance(int f = 0, int i = 0) : feet(f), inch(i) {}
Distance operator-() {
return Distance(-feet, -inch);
}
void display() {
cout << feet << " feet " << inch << " inches" << endl;
}
};
int main() {
Distance d1(5, 8);
Distance d2 = -d1;
cout << "Original: ";
d1.display();
cout << "After unary minus: ";
d2.display();
return 0;
}25 Overload + operator for Length
Lab 25. Create a class called Length that has data members meter and centimeter. Overload + operator to add two objects of class Length. (For example, L3 = L1 + L2).
#include <iostream>
using namespace std;
class Length {
int meter, centimeter;
public:
Length(int m = 0, int cm = 0) : meter(m), centimeter(cm) {}
Length operator+(const Length &l) {
int totalCm = centimeter + l.centimeter;
int totalM = meter + l.meter + totalCm / 100;
totalCm %= 100;
return Length(totalM, totalCm);
}
void display() {
cout << meter << " m " << centimeter << " cm" << endl;
}
};
int main() {
Length L1(5, 75);
Length L2(3, 50);
Length L3 = L1 + L2;
cout << "L1 = "; L1.display();
cout << "L2 = "; L2.display();
cout << "L3 = "; L3.display();
return 0;
}26 User-defined Distance to basic float conversion
Lab 26. Write a conversion routine in c++ that can convert user-defined data distance to basic data float. Assume that the class distance contains two data members (feet (integer type) and inch (floating point type). NOTE 1-meter = 3.33 feet and 1 feet = 12 inches).
#include <iostream>
using namespace std;
class Distance {
int feet;
float inch;
public:
Distance(int f = 0, float i = 0) : feet(f), inch(i) {}
// Conversion operator: Distance -> float (meters)
operator float() const {
float totalFeet = feet + inch / 12.0f;
return totalFeet / 3.33f;
}
};
int main() {
Distance d(6, 6);
float meters = d;
cout << "Distance in meters = " << meters << endl;
return 0;
}
27 User-defined to user-defined conversion in the destination class
Lab 27. Write a program to demonstrate the user-defined to user-defined data conversion routine located in the destination class.
#include <iostream>
using namespace std;
class FeetInch {
int feet;
float inch;
public:
FeetInch(int f = 0, float i = 0) : feet(f), inch(i) {}
float toFeet() const {
return feet + inch / 12.0f;
}
void display() const {
cout << feet << " feet " << inch << " inches" << endl;
}
};
class Meter {
float meter;
public:
Meter(float m = 0) : meter(m) {}
// Conversion constructor in the destination class.
Meter(const FeetInch &d) {
meter = d.toFeet() / 3.33f;
}
void display() const {
cout << meter << " meters" << endl;
}
};
int main() {
FeetInch d(6, 6);
Meter m = d;
cout << "Source: ";
d.display();
cout << "Converted: ";
m.display();
return 0;
}
Meter as a conversion constructor that accepts a FeetInch object.