C++ Programming Lab Exercises
C++ Programming Lab Exercises
Lab 2.8 Write a program for adding two numbers 13 Aug 2025
in C++ using classes and objects.
Code:
#include <iostream>
int main(){
cin>>num1>>num2;
int sum=num1+num2;
Output:
PROGRAM 2: Write a C++ program to check if a number is even or odd.
Code:
#include <iostream>
int main(){
int num;
cin>>num;
if (num&1){
cout<<"Number is odd"<<endl;
}else{
cout<<"Number is even"<<endl;
Output:
PROGRAM 3: Write a program to swap two numbers in C++.
Code:
#include <iostream>
int main(){
int a=45;
int b=64;
cout<<"Before:"<<endl;
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
cout<<endl;
int temp=a;
a=b;
b=temp;
cout<<"After:"<<endl;
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
Output:
PROGRAM 4: Write a C++ program to find the largest number among three numbers.
Code:
#include <iostream>
int main(){
int num1,num2,num3;
cin>>num1>>num2>>num3;
}else{
Output:
PROGRAM 5: Write a C++ Program to Find the sum of all the natural numbers from 1 to n.
Code:
#include <iostream>
int main(){
int n;
cin>>n;
int sum=0;
sum+=i;
Output:
Code:
#include <iostream>
int main(){
int n;
cin>>n;
bool prime=true;
if (n%i==0){
prime=false;
if (prime){
cout<<"It is prime"<<endl;
}else{
Output:
PROGRAM 7: Write a C++ program to compute the power a given number to a given power.
Code:
#include <iostream>
using namespace std;
int main(){
int num,power;
cin>>num>>power;
int ans=num;
if(power==0){
cout<<1<<endl;
}else if(power==1){
cout<<ans<<endl;
}else{
ans*=num;
cout<<ans;
Output:
PROGRAM 8: Write a program for adding two numbers in C++ using classes and objects.
Code:
#include <iostream>
using namespace std;
class Add{
public:
};
int main(){
Add add1;
add1.num1=5;
add1.num2=12;
[Link](add1.num1,add1.num2);
Output:
class OddOrEven{
public:
int num;
if (num&1){
cout<<"Number is odd"<<endl;
}else{
cout<<"Number is even"<<endl;
};
int main(){
OddOrEven program;
[Link]=10;
[Link]([Link]);
Output:
PROGRAM 10: Write a program to swap two numbers in C++ using classes and
objects.
Code:
#include <iostream>
class Swap{
public:
int num1,num2;
cout<<"Before:"<<endl;
cout<<"Num1:"<<num1<<" "<<"Num2:"<<num2<<endl;
int temp=num1;
num1=num2;
num2=temp;
cout<<"After:"<<endl;
cout<<"Num1:"<<num1<<" "<<"Num2:"<<num2<<endl;
};
int main(){
Swap check;
check.num1=15;
check.num2=30;
[Link](check.num1,check.num2);
Output:
PROGRAM 11: Write a program to find the largest number among three numbers
in C++ using classes and objects.
Code:
#include <iostream>
class Largest{
public:
int num1,num2,num3;
}else{
};
int main(){
Largest check;
check.num1=12;
check.num2=34;
check.num3=1;
[Link](check.num1,check.num2,check.num3);
Output:
PROGRAM 12: Write a program to find the sum of all the natural
numbers from 1 to n in C++ using classes and objects.
Code:
#include <iostream>
class Sum{
public:
int num;
int sum=0;
sum+=i;
};
int main(){
Sum check;
[Link]=5;
[Link]([Link]);
Output:
PROGRAM 13: Write a program to check whether a number is prime or not in C++
using classes and objects.
Code:
#include <iostream>
class Prime{
public:
int num;
bool result=true;
if (num%i==0){
result=false;
if (result){
cout<<"Num is prime"<<endl;
}else{
};
int main(){
Prime check;
[Link]=45;
[Link]([Link]);
Output:
PROGRAM 14: Write a program to compute the power a given number
to a given power in C++ using classes and objects.
Code:
#include <iostream>
class Power{
public:
int ans=1;
ans*=num;
};
int main(){
Power check;
[Link]=2;
[Link]=3;
[Link]([Link], [Link]);
Output:
PROGRAM 15: Write a C++ program using class and object to check
whether a given year is a leap year or not.
Code:
#include <iostream>
class Leap{
public:
int year;
bool leap=false;
leap=true;
if (leap){
}else{
};
int main(){
Leap check;
[Link]=2015;
[Link]([Link]);
Output:
PROGRAM 16: Write a C++ program using class and object to accept a
coordinate point (x, y) in the XY coordinate system and determine in
which quadrant the point lies.
Code:
#include <iostream>
class Coordinate{
public:
int x,y;
};
int main(){
Coordinate check;
check.x=0;
check.y=0;
[Link](check.x,check.y);
Output:
PROGRAM 17: Write a C++ program using class and object to read a
student’s Roll Number, Name, and marks in three subjects. Calculate the
total marks, percentage, division, and grade of the student, and display
the result.
Code:
#include <iostream>
class Student{
public:
int id;
string name;
int marks1,marks2,marks3;
cout<<"ID is:"<<id<<endl;
cout<<"Name is:"<<name<<endl;
int sum=marks1+marks2+marks3;
cout<<"Sum is:"<<sum<<endl;
cout<<"Percentage is:"<<percentage<<endl;
if (percentage>=60){
cout<<"Grade A"<<endl;
}else if(percentage>=45){
cout<<"Grade B"<<endl;
}else if(percentage>=33){
cout<<"Grade C"<<endl;
}else{
cout<<"Fail"<<endl;
};
int main(){
Student student1;
[Link]=101;
[Link]="OOPS";
student1.marks1=78;
student1.marks2=87;
student1.marks3=92;
[Link](student1.marks1,student1.marks2, student1.marks3);
Output:
PROGRAM 18: Write a C++ program using class and object to display the first N
terms of odd natural numbers and their sum.
Code:
#include <iostream>
class OddNatural{
public:
int num;
int sum=0;
cout<<i<<" ";
sum+=i;
cout<<endl;
cout<<"Sum is:"<<sum<<endl;
};
int main(){
OddNatural check;
[Link]=5;
[Link]([Link]);
Output:
PROGRAM 19: Write a C++ program using class and object to check
whether a given number is a palindrome or not.
Code:
#include <iostream>
class Palindrome{
public:
int num;
int n=num;
int rev=0;
while(n>0){
int digit=n%10;
rev=rev*10+digit;
n=n/10;
if(rev==num){
cout<<"It is a palindrome"<<endl;
}else{
};
int main(){
Palindrome p1;
[Link]=12321;
[Link]([Link]);
Output:
PROGRAM 20: Basic Array Input and Output Using Class. Create a class
ArrayHandler that stores an array of integers. Include methods to Input n elements
into the array and Display the array elements.
Code:
#include <iostream>
class ArrayHandler{
public:
int size;
cin>>arr[i];
cout<<"Elements are:";
cout<<arr[i]<<" ";
};
int main(){
ArrayHandler array1;
[Link]=5;
int arr[[Link]];
[Link](arr, [Link]);
Output:
PROGRAM 21: Write C++ program to Find Maximum and Minimum
Element in an Array using Class.
Code:
#include <iostream>
#include <algorithm>
class ArrayMinMax{
public:
int size;
int mini=INT_MAX;
int maxi=INT_MIN;
cin>>arr[i];
cout<<"Elements are:";
cout<<arr[i]<<" ";
}cout<<endl;
mini=min(arr[i],mini);
maxi=max(arr[i],maxi);
};
int main(){
ArrayMinMax array1;
[Link]=5;
int arr[[Link]];
[Link](arr,[Link]);
[Link](arr,[Link]);
Output:
PROGRAM 22: Write C++ program using class to Sort the Array in
Ascending Order.
Code:
#include <iostream>
#include <algorithm>
class SortArray{
public:
int size;
cin>>arr[i];
cout<<"Elements are:";
cout<<arr[i]<<" ";
}cout<<endl;
if (arr[i]>arr[j]){
swap(arr[i],arr[j]);
}
cout<<"The sorted array is:";
cout<<arr[i]<<" ";
};
int main(){
SortArray array1;
[Link]=5;
int arr[[Link]];
[Link](arr, [Link]);
[Link](arr,[Link]);
Output:
PROGRAM 23: Write C++ program using class to Search for an Element in
the Array.
Code:
#include <iostream>
class LinearArray{
public:
int size;
int target;
cin>>arr[i];
cout<<"Elements are:";
cout<<arr[i]<<" ";
}cout<<endl;
if (arr[i]==target){
return true;
return false;
}
};
int main(){
LinearArray array1;
[Link]=5;
int arr[[Link]];
[Link](arr,[Link]);
cin>>[Link];
if (found){
cout<<"Target found"<<endl;
}else{
Output:
PROGRAM 24: Write a C++ Program using class that Count Frequency of
Elements in the Array. Create a method that Counts how many times
each unique element appears in the array and Displays the frequency.
Code:
#include <iostream>
class FrequencyCounter{
public:
int size;
cin>>arr[i];
cout<<"Elements are:";
cout<<arr[i]<<" ";
}cout<<endl;
bool visited[size];
visited[i]=false;
continue;
int count=1;
if (arr[i]==arr[j]){
visited[j]=true;
count++;
cout<<arr[i]<<"->"<<count<<endl;
};
int main(){
FrequencyCounter array1;
[Link]=8;
int arr[[Link]];
[Link](arr,[Link]);
[Link](arr, [Link]);
Output:
PROGRAM 25: Write a C++ program that defines a function int
square(int num) to return the square of a given number. In the main()
function, prompt the user to enter an integer and display its square
using the function.
Code:
#include <iostream>
class Square{
public:
int num;
int sq=num*num;
};
int main(){
Square s1;
cin>>[Link];
[Link]([Link]);
Output:
PROGRAM 26: Write a C++ program using an inline function float
area(float radius) to calculate and return the area of a circle. In the
main() function, take input for the radius and display the result.
Code:
#include <iostream>
int main(){
float radius;
cin>>radius;
Output:
PROGRAM 27: Write a C++ program to define a function float
calculateInterest(float principal, float rate = 5.0, int time = 2) that
calculates and returns simple interest using the formula SI = (P * R * T) /
100. Call the function with different combinations of arguments (all,
two, one) to demonstrate default argument behavior.
Code:
#include <iostream>
int si=(principal*rate*time)/100;
return si;
int main(){
int si1=calculateInterest(1000,6,3);
int si2=calculateInterest(1000,6);
int si3=calculateInterest(1000);
Output:
PROGRAM 28: Write a C++ program to compare the behavior of function
overloading and default arguments by defining:
One function int add(int a, int b)
Another function int add(int a, int b = 0, int c = 0)
Call the function with different sets of arguments and explain what happens.
Code:
#include <iostream>
return num1+num2;
return num1+num2+num3;
int main(){
int test1=add(5,10);
cout<<test1<<endl;
cout<<endl;
int test2=add(5,10,15);
cout<<test2<<endl;
Output:
The error occurs because C++ does not allow function overloading where the only difference is
default arguments.
PROGRAM 29: Create a C++ program that uses an inline function int
max(int a, int b) to return the maximum of two numbers. In the main()
function, ask the user for two integers and print the maximum using the
function.
Code:
#include <iostream>
int main(){
int num1,num2;
cin>>num1>>num2;
int ans=maxi(num1,num2);
Output:
PROGRAM 30: Write a C++ program that defines a function void printDetails(string
name, int age = 18, string city = "Unknown"). In the main() function, call
printDetails():
with all arguments
with one or two arguments
Demonstrate how default values are used for the missing parameters.
Code:
#include <iostream>
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"City:"<<city<<endl;
int main(){
printDetails("Mila",20,"CA");
cout<<endl;
printDetails("Alicia",19);
cout<<endl;
printDetails("John");
Output:
PROGRAM 31: Create a class BitwiseDemo with two integers and methods to
perform AND, OR, and XOR.
Code:
#include <iostream>
class BitwiseDemo{
public:
int num1;
int num2;
};
int main(){
BitwiseDemo demo1;
demo1.num1=9;
demo1.num2=3;
[Link](demo1.num1,demo1.num2);
[Link](demo1.num1,demo1.num2);
[Link](demo1.num1,demo1.num2);
Output:
PROGRAM 32: Create a class NumberCheck with a method isEven(int n) that
uses the bitwise AND operator (n & 1) to check if a number is even.
Code:
#include <iostream>
class NumberCheck{
public:
int num;
if (num&1){
}else{
};
int main(){
NumberCheck demo1;
[Link]=4;
[Link]([Link]);
Output:
PROGRAM 33: Create a class Swapper with a method swap(int a, int b) that
swaps two numbers using the XOR (^) operator, and display the values before
and after swapping.
Code:
#include <iostream>
class Swapper{
public:
int num1;
int num2;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
};
int main(){
Swapper demo1;
demo1.num1=7;
demo1.num2=4;
[Link](demo1.num1,demo1.num2);
Output:
class BitCounter{
public:
int num;
int count=0;
while (num>0){
if (num&1){
count++;
num=num>>1;
return count;
}
};
int main(){
BitCounter demo1;
[Link]=7;
int result=[Link]([Link]);
cout<<"Count of setbits:"<<result<<endl;
Output:
class ShiftDemo{
public:
int num;
return num<<1;
return num>>1;
}
};
int main(){
ShiftDemo demo1;
[Link]=9;
int prod=demo1.multiplyBy2([Link]);
int div=demo1.divideBy2([Link]);
cout<<"Multiply by 2:"<<prod<<endl;
cout<<"Divide by 2:"<<div<<endl;
Output:
class Pointer{
public:
int num;
int *ptr;
ptr=#
cout<<"Value:"<<num<<endl;
cout<<"Address:"<<ptr<<endl;
};
int main(){
Pointer demo1;
[Link]=9;
[Link]([Link]);
Output:
class Access{
public:
int arr[5];
int size;
cout<<arr[i]<<" ";
};
int main(){
Access demo1;
[Link][0]=1;
[Link][1]=2;
[Link][2]=3;
[Link][3]=4;
[Link][4]=5;
[Link]=5;
[Link]([Link],[Link]);
Output:
PROGRAM 38: Write a function swap(int *a, int *b) that swaps two numbers
using pointers.
Code:
#include <iostream>
class Swap{
public:
int num1;
int num2;
cout<<"Before swapping:"<<endl;
cout<<"num1:"<<*num1<<endl;
cout<<"num2:"<<*num2<<endl;
cout<<endl;
int temp=*num1;
*num1=*num2;
*num2=temp;
cout<<"After swapping:"<<endl;
cout<<"num1:"<<*num1<<endl;
cout<<"num2:"<<*num2<<endl;
};
int main(){
Swap demo1;
demo1.num1=5;
demo1.num2=8;
[Link](&demo1.num1,&demo1.num2);
Output:
PROGRAM 39: Write a program to calculate the sum of elements of an array
using pointers.
Code:
#include <iostream>
class Add{
public:
int *p=arr;
int total=0;
total+=(*p+i);
return total;
};
int main(){
Add demo1;
int arr[5]={1,2,3,4,5};
int result=[Link](arr,5);
Output:
PROGRAM 40: Write a function that takes a pointer to an integer and increments
its value by 1.
Code:
#include <iostream>
class Pointer{
public:
int num;
int *p=#
cout<<"Before increment:";
cout<<*p<<endl;
cout<<endl;
cout<<"After increment:";
(*p)++;
cout<<(*p)<<endl;
}
};
int main(){
Pointer p1;
[Link]=5;
[Link]([Link]);
Output:
PROGRAM 41:
Create a class BankAccount with private members accountNumber and balance.
Write public functions to set and display values.
Try accessing balance directly in main() (should give error – comment it out).
(Concept: Data Hiding)
Code:
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
double balance;
public:
void setValues(int accNum, double bal) {
accountNumber = accNum;
balance = bal;
}
void displayValues() {
cout << "Account Number: " << accountNumber << endl;
cout << "Balance: ₹" << balance << endl;
}
};
int main() {
BankAccount b1;
[Link](12345, 5000.75);
[Link]();
return 0;
}
Output-
PROGRAM 42:
Create a class Student with private members name and marks.
Use a friend function to display student details.
(Concept: Friend Function)
Code:
#include <iostream>
class Student {
private:
string name;
int marks;
public:
Student(string n, int m) {
name = n;
marks = m;
};
void displayDetails(Student s) {
int main() {
displayDetails(s1);
return 0;
}
Output:
PROGRAM 43:
Code:
#include <iostream>
class Counter {
private:
public:
void increment() {
count++;
void displayCount() {
}
};
int Counter::count = 0;
int main() {
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
Output:
PROGRAM 44:
Code:
#include <iostream>
class MyClass {
private:
public:
};
int MyClass::data = 0;
MyClass::data += value;
cout << "Data after modification: " << MyClass::data << endl;
int main() {
modifyData(10);
modifyData(5);
modifyData(-3);
return 0;
Output:
PROGRAM 45:
Create class Employee with private members empID, salary, and a static empCount.
Use a setter function to assign values and increment empCount.
Use a static function to return total employees.
(Concept: Data Hiding + Static Variable)
Code:
#include <iostream>
class Employee {
private:
int empID;
double salary;
public:
empID = id;
salary = sal;
empCount++;
void displayDetails() {
cout << "Employee ID: " << empID << ", Salary: ₹" << salary << endl;
return empCount;
};
int Employee::empCount = 0;
int main() {
[Link](101, 45000);
[Link](102, 55000);
[Link]();
[Link]();
return 0;
Output:
PROGRAM 46:
Create a base class Person with data members name and age, and a derived class Student that
adds a studentID and a method to display all details.
(Concept: Single Inheritance)
Code:
#include <iostream>
class Person {
protected:
string name;
int age;
public:
name = n;
age = a;
};
private:
int studentID;
public:
studentID = id;
void displayDetails() {
};
int main() {
Student s1;
[Link]("Ishh", 20);
[Link](1001);
[Link]();
return 0;
Output:
PROGRAM 47:
Define a base class Employee with protected members empID and salary.
Create a derived class Manager that adds bonus and a function calculateTotalSalary() to compute
total pay.
(Concept: Inheritance + Protected Members)
Code:
#include <iostream>
class Employee {
protected:
int empID;
double salary;
public:
empID = id;
salary = sal;
};
private:
double bonus;
public:
void setBonus(double b) {
bonus = b;
double calculateTotalSalary() {
void displayDetails() {
};
int main() {
Manager m1;
[Link](201, 50000);
[Link](10000);
[Link]();
return 0;
Output:
PROGRAM 48:
Define a base class Account that stores accountNumber and balance (private).
Provide public functions to set and get these values. Derive a class SavingsAccount that adds
interestRate and a function to calculate interest.
(Concept: Inheritance + Data Hiding)
Code:
#include <iostream>
class Account {
private:
int accountNumber;
double balance;
public:
accountNumber = accNo;
balance = bal;
int getAccountNumber() {
return accountNumber;
double getBalance() {
return balance;
}
};
private:
double interestRate;
public:
interestRate = rate;
double calculateInterest() {
void displayAccountDetails() {
cout << "Interest Rate: " << interestRate << "%" << endl;
};
int main() {
SavingsAccount s1;
[Link](55501, 10000);
[Link](5.5);
[Link]();
return 0;
}
Output:
PROGRAM 49: Class for a geometric shape with constructor and destructor
Code:
#include <iostream>
class Rectangle {
public:
cout << "Rectangle created (w=" << width << ", h=" << height << ")\n";
~Rectangle() {
cout << "Rectangle destroyed (w=" << width << ", h=" << height << ")\n";
};
int main() {
return 0;
}
Output:
PROGRAM 50: Simple bank account class with constructor and destructor
Code:
#include <iostream>
#include <string>
class BankAccount {
string owner;
int accountNumber;
double balance;
public:
cout << "Account opened for " << owner << " (Acc#: " << accountNumber << ")\n";
balance -= amt;
return true;
cout << "Owner: " << owner << ", Acc#: " << accountNumber
<< ", Balance: " << balance << endl;
~BankAccount() {
cout << "Account object for " << owner << " is being destroyed.\n";
};
int main() {
[Link]();
[Link](1500.0);
[Link]();
return 0;
Output:
PROGRAM 51: University course class with constructor and destructor releasing dynamic memory
Code:
#include <iostream>
#include <string>
class Course {
string courseName;
int studentCount;
public:
cout << "Course '" << courseName << "' created for " << studentCount << " students.\n";
~Course() {
delete[] students;
cout << "Course '" << courseName << "' destroyed and student list memory freed.\n";
};
int main() {
[Link]();
return 0;
Output:
PROGRAM 52: Book class with constructor parameters and destructor releasing dynamic memory
Code:
#include <iostream>
#include <string>
class Book {
string title;
string author;
int year;
public:
cout << "Title: " << title << "\nAuthor: " << author
<< "\nYear: " << year << "\nSynopsis: " << *synopsis << endl;
~Book() {
delete synopsis;
cout << "Book '" << title << "' destroyed and synopsis memory freed.\n";
}
};
int main() {
Book b("The Tales", "A. Writer", 2020, "A short adventure story.");
[Link]();
return 0;
Output:
PROGRAM 53: Base class Person and derived class Student (adds studentID and display)
Code:
#include <iostream>
#include <string>
class Person {
public:
string name;
int age;
};
class Student : public Person {
string studentID;
public:
cout << "Name: " << name << "\nAge: " << age << "\nStudent ID: " << studentID << endl;
};
int main() {
[Link]();
return 0;
Output:
PROGRAM 54: Base class Employee (protected empID, salary) and derived Manager with bonus
and calculateTotalSalary()
Code:
#include <iostream>
#include <string>
class Employee {
protected:
int empID;
double salary;
public:
};
double bonus;
public:
cout << "Manager ID: " << empID << ", Base Salary: " << salary
<< ", Bonus: " << bonus << ", Total: " << calculateTotalSalary() << endl;
};
int main() {
[Link]();
return 0;
Output:
PROGRAM 55: Base class Account (private accountNumber, balance) with setters/getters;
SavingsAccount derived adds interestRate and calculateInterest()
Code:
#include <iostream>
class Account {
private:
long accountNumber;
double balance;
public:
};
public:
cout << "Acc#: " << getAccountNumber() << ", Balance: " << getBalance()
<< ", Interest Rate: " << interestRate << "%, Interest: " << calculateInterest() << endl;
};
int main() {
SavingsAccount s(4.5);
[Link](987654321);
[Link](10000.0);
[Link]();
return 0;
Output:
PROGRAM 56: Single program illustrating single, multiple and multilevel inheritance
Code:
#include <iostream>
// Single inheritance
class A {
public:
};
public:
};
// Multiple inheritance
class B {
public:
};
class C {
public:
};
public:
};
public:
};
public:
};
int main() {
SingleDerived s;
[Link]();
[Link]();
MultipleDerived m;
[Link]();
[Link]();
[Link]();
cout << "\n--- Multilevel Inheritance ---\n";
Level2 L;
[Link](); // from A
return 0;
Output:
PROGRAM 57: Illustration of public, private, protected derivation and virtual base class (diamond
problem demo)
Code:
#include <iostream>
class Base {
protected:
int x;
public:
Base(int v = 0) : x(v) {}
void setX(int v) { x = v; }
int getX() const { return x; }
void show() const { cout << "Base x = " << x << endl; }
};
public:
void showPub() { cout << "PubDerived accessing x: " << x << endl; }
};
public:
void setAndShow(int v) { setX(v); cout << "PrivDerived set x=" << getX() << endl; }
};
public:
void showProt() { cout << "ProtDerived accessing x: " << x << endl; }
};
class VBase {
protected:
int val;
public:
};
public:
};
public:
};
public:
void showDiamond() {
cout << "Diamond sees VBase::val = " << getVal() << endl;
};
int main() {
PubDerived p(10);
[Link]();
[Link](20);
[Link]();
PrivDerived pr;
[Link](30); // allowed via wrapper
ProtDerived pt(40);
[Link]();
Diamond d(99);
[Link]();
[Link]();
return 0;
Output:
Code:
#include <iostream>
class Shape {
public:
virtual void draw() const { cout << "Drawing a generic shape\n"; }
};
public:
};
public:
};
int main() {
s1->draw();
s2->draw();
delete s1;
delete s2;
return 0;
Output:
PROGRAM 59: Illustrating function overloading
Code:
#include <iostream>
return a + b;
return a + b;
return a + b + c;
int main() {
return 0;
Output:
PROGRAM 60: Illustrating operator overloading (Complex numbers, operator +)
Code:
#include <iostream>
class Complex {
public:
if ([Link] >= 0)
else
os << [Link] << " - " << (-[Link]) << "i"; // manually flipping the sign
return os;
};
int main() {
Complex a(2, 3), b(1, -4);
Complex c = a + b;
return 0;
Output:
Code:
#include <iostream>
class Point {
int x, y;
public:
Point(int a) : x(a), y(a) { cout << "Single-arg ctor: (" << x << "," << y << ")\n"; }
Point(int a, int b) : x(a), y(b) { cout << "Two-arg ctor: (" << x << "," << y << ")\n"; }
void show() const { cout << "Point = (" << x << "," << y << ")\n"; }
};
int main() {
Point p1;
Point p2(5);
Point p3(3,7);
[Link]();
[Link]();
[Link]();
return 0;
Output:
PROGRAM 62: Division by zero with try-catch (prompt user for two integers)
Code:
#include <iostream>
#include <stdexcept>
int main() {
int a, b;
return 1;
try {
return 0;
Output:
PROGRAM 63: Handle out_of_range when accessing array elements (prompt index)
Code:
#include <iostream>
#include <vector>
#include <stdexcept>
int main() {
cout << "Array has " << [Link]() << " elements (indices 0-" << [Link]()-1 << ")\n";
try {
cout << "Value at index " << idx << " is " << value << '\n';
cout << "Caught out_of_range exception: " << [Link]() << '\n';
return 0;
Output:
Code:
#include <iostream>
#include <stdexcept>
void func() {
try {
int main() {
try {
func();
cout << "main caught rethrown exception: " << [Link]() << '\n';
return 0;
Output:
PROGRAM 54: Prompt name and age, display formatted using setw and left manipulators
Code:
#include <iostream>
#include <iomanip>
#include <string>
int main() {
string name;
int age;
cout << "Enter your name: ";
getline(cin, name);
cout << left << setw(12) << "Name:" << name << '\n';
cout << left << setw(12) << "Age:" << age << '\n';
return 0;
Output:
PROGRAM 55: Read a floating-point number and display with precision up to 3 decimal places
Code:
#include <iostream>
#include <iomanip>
int main() {
double x;
Output: