0% found this document useful (0 votes)
11 views21 pages

Document From SHAMI

The document outlines several C++ programming tasks related to functions, classes, and object-oriented programming concepts. It includes examples of calculating interest for a bank, swapping integers using different methods, calculating volume with default arguments, and managing customer information using dynamic memory. Each section provides sample code, input/output examples, and the aim of the task, demonstrating practical applications of C++ programming principles.

Uploaded by

sshamini111
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)
11 views21 pages

Document From SHAMI

The document outlines several C++ programming tasks related to functions, classes, and object-oriented programming concepts. It includes examples of calculating interest for a bank, swapping integers using different methods, calculating volume with default arguments, and managing customer information using dynamic memory. Each section provides sample code, input/output examples, and the aim of the task, demonstrating practical applications of C++ programming principles.

Uploaded by

sshamini111
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
You are on page 1/ 21

Ex: No: 1A Programs Using Functions

Date: Functions with Default Arguments

Question:

You are tasked with developing a C++ program for a bank. The program should calculate the interest
for a fixed deposit account. The interest is calculated based on the principal amount, rate of interest,
and the duration of the deposit. However, if the rate of interest and the duration are not provided by the
user, the program should use default values.
Create a function that will calculate the interest, where:
● The principal amount is mandatory (must be provided by the user).
● The rate of interest and the duration are optional (the program should use default values if they
are not provided).
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
float calc_Interest(float principal,float rate,int duration)
{
return (principal*rate*duration)/100;
}

float calc_Interest(float principal)


{
return calc_Interest(principal,5.0,1);
}
int main()
{

float principal,rate;
int duration;
cout << "Enter principal amount: ";
cin >> principal;
cout << "Enter rate of interest and duration(or press 0 to use default values): ";
cin >> rate >> duration;
float interest = (rate==0 && duration==0) ? calc_Interest(principal) :
calc_Interest(principal,rate,duration);
cout << "Interest: " << interest << endl;
return 0;
}

Sample Input and Output 1:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter principal amount: 1000
Enter rate of interest and duration(or press 0 to use default values): 0 0
Interest: 50

Sample Input and Output 2:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter principal amount: 2000
Enter rate of interest and duration(or press 0 to use default values): 5 2
Interest: 200

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 1B Implementation of Call by Value, Call by Address and
Date: Call by Reference

Question:

You are developing a utility for a programming course to demonstrate different ways to swap two
integers. The goal is to write a C++ program that swaps the values of two integers in three different
ways:
1. Call by Value: The function should take the values of the integers and attempt to swap them.
2. Call by Address: The function should use pointers to swap the integers by modifying their memory
addresses.
3. Call by Reference: The function should swap the integers by directly accessing them using
references.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include <iostream>
using namespace std;
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swapByAddress(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x,y;
cout<<"Enter the values for x and y:";
cin>>x>>y;
cout << "Original values: x = " << x << ", y = " << y << endl;
swapByValue(x, y);
cout << "After swapByValue: x = " << x << ", y = " << y << endl;
swapByAddress(&x, &y);
cout << "After swapByAddress: x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swapByReference: x = " << x << ", y = " << y << endl;
return 0;
}

Sample Input and Output:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the values for x and y:2 3
Original values: x = 2, y = 3
After swapByValue: x = 2, y = 3
After swapByAddress: x = 3, y = 2
After swapByReference: x = 2, y = 3

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 1C Volume of a Box with Default Arguments
Date:

Question:

You are developing a 3D modeling application that allows users to calculate the volume of various shapes,
including rectangular boxes. To make the calculation process easier for users, the system should
automatically use default dimensions for a box if the user does not provide specific values for its length,
width, and height. Your task is to write a C++ program that calculates the volume of a rectangular box. The
program should allow the user to input the dimensions (length, width, and height) of the box. If any of these
dimensions are not provided by the user, the system should use default values.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
int Volume(int length,int width,int height)
{
return length * width * height;

int Volume() {
float def_length = 10, def_width = 8, def_height = 4;
cout << "Using default length=" << def_length << ",width=" << def_width << ",height=" <<
def_height << endl;
return Volume(def_length, def_width, def_height);

int main() {
int length, width, height;
cout << "Enter length,width,height (or press 0 to use default values):";
cin >> length >> width >> height;
int result = (length == 0 && width == 0 && height == 0) ? Volume() : Volume(length, width,
height);
cout << "Volume:" << result << endl;
return 0;
}

Sample Input and Output 1:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter length,width,height (or press 0 to use default values):0 0 0
Using default length=10,width=8,height=4
Volume:320

Sample Input and Output 2:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter length,width,height (or press 0 to use default values):5 4 6
Volume:120

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 1D Call by Address using Default Arguments
Date:

Question:

You are developing a program for a financial application where the system needs to perform division
calculations. The program will divide two numbers, but in some cases, the user might not provide the second
number. If the second number is not provided, the system should default to dividing by 1 to avoid
errors.Your task is to write a C++ program where a function takes pointers to modify the values of the
numbers. If the user provides both numbers, the function should divide them. However, if the second number
is not provided, the function should default to using 1 as the divisor.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include <iostream>
using namespace std;
void divide(float *num1, float *num2) {
*num1= *num1 / *num2;

float divide(float num1) {


float num2 = 1;
divide(&num1, &num2);
return num1;
}

int main() {
float num1, num2;
cout << "Enter the first number:";
cin >> num1;
cout << "Enter the second number (or press 0 to use default value 1): ";
cin >> num2;
float result = (num2 == 0) ? divide(num1) : (num1 / num2);
cout << "Result: " << result << endl;
return 0;

Sample Input and Output 1:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the first number:4
Enter the second number (or press 0 to use default value 1): 2
Result: 2

Sample Input and Output 2:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the first number:4
Enter the second number (or press 0 to use default value 1): 0
Result: 4

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 2A Classes with Objects, Member Functions and Constructors
Date: Classes with Primitive Data Members

Question:

You are working on a time management application that needs to perform time calculations. One of the key
features of the application is to add two time durations. The times are provided in the format of hours,
minutes, and seconds (hh:mm:ss). Your task is to write a C++ program that will add two time objects and
output the resulting time in the same hh:mm:ss format.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
class time
{
int hours,minutes,seconds;
public:
time()
{
hours=0;
minutes=0;
seconds=0;
}
void input()
{
cout<<"Enter the time in (hh:mm:ss):";
cin>>hours>>minutes>>seconds;
}
void add(time t1,time t2)
{
seconds=t1.seconds+t2.seconds;
minutes=t1.minutes+t2.minutes+(seconds/60);
hours=t1.hours+t2.hours+(minutes/60);
seconds%=60;
minutes%=60;
}
void display()
{
cout<<"Total time:"<<hours<<":"<<minutes<<":"<<seconds<<endl;}
}t1,t2,t3;
int main()
{
t1.input();
t2.input();
t3.add(t1,t2);
t3.display();
return 0;
}

Sample Input and Output:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the time in (hh:mm:ss):2 45 50
Enter the time in (hh:mm:ss):1 20 30
Total time:4:6:20

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 2B Classes with Arrays as Data Members
Date:

Question:

You are developing a student management system for a school, where the system needs to calculate the
Grade Point Average (GPA) of multiple students. Each student has a set of grades for different subjects, and
you need to calculate their GPA based on these grades. Your task is to develop a program that defines a
Student class with an array as a data member to store the grades of each student.
The program should:
● Accept the number of students (n) from the user.
● Store each student's grades in an array.
● Calculate the GPA based on the grades (you can assume a simple GPA calculation like averaging the
grades).
● Display the GPA for each student.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
class Student
{
public:
int grade[10];
int Grades(char grade)
{
if(grade=='A')
{
return 4;
}
if(grade=='B')
{
return 3;
}
if(grade=='C')
{
return 2;
}
if(grade=='D')
{
return 1;
}
if(grade=='F')
{
return 0;
}
return -1;
}
int calc_GPA()
{
int total=0;
for(int i=0;i<5;i++)
{
total+=grade[i];
}
return total/5;
}
};
int main()
{
int n;
cout<<"Enter the number of students:";
cin>>n;
Student students[n]; for(int i=0;i<n;i++)
{
cout<<"Enter the grades for student"<<i+1<<":"<<endl;
for(int j=0;j<5;j++)
{
char grade;

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


cin>>grade;
students[i].grade[j]=students[i].Grades(grade);
}
}
for(int i=0;i<n;i++)
{
cout<<"GPA of Student"<<i+1<<":"<<students[i].calc_GPA()<<endl;
}
return 0;
}

Sample Input and Output:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the number of students:2
Enter the grades for student1:
AAAAA
Enter the grades for student2:
ABABB
GPA of Student1:4
GPA of Student2:3

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 2C Classes with Pointers as Data Members – String Class
Date:

Question:

You are developing a customer management system for a retail store. The system needs to store customer
information, including their name, using the C++ string class. Additionally, the system should utilize pointers
to manage the name of the customer dynamically. Your task is to develop a program that defines a Customer
class. The class should have a pointer as a data member to store the customer's name dynamically using the
string class. The program should allow the user to input the customer's name and display it when requested.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
#include<string>
using namespace std;
class Customer
{
public:
string *name; int *age;
string *address;
Customer()
{
name=new string;
age=new int;
address=newstring;
}
~Customer()
{
delete name;
delete age;
delete address;
}
void getdata()
{
cout<<"Enter customer name:";
cin>>*name;cout<<"Enter customer age:";
cin>>*age;cout<<"Enter customer address:";
cin>>*address;
}
void display()const
{
cout<<"Customer Information:"<<endl;
cout<<"Name:"<<*name<<endl;
cout<<"Age:"<<*age<<endl;
cout<<"Address:"<<*address<<endl;
}
};
void CustomerInfo()
{
Customer*customer=new Customer();
customer->getdata();
customer->display();
delete customer;
}
int main()
{
CustomerInfo();
return 0;
}

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Sample Input and Output:
[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter customer name:Krishlee
Enter customer age:18
Enter customer address:41/2,Vattakottai,Kanyakumari
Customer Information:
Name:Krishlee
Age:18
Address:41/2,Vattakottai,Kanyakumari

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 2D Classes with Constant Data Members
Date:

Question:

You are tasked with developing a geometry calculator for a math application. One of the features of the
application is to calculate the area and circumference of a circle. The formula for the area of a circle is
π×r2\pi \times r^2π×r2 and for the circumference, it’s 2×π×r2 \times \pi \times r2×π×r, where rrr is the radius
of the circle.
Your task is to develop a program that defines a Circle class. This class should have a constant data member
PI to store the value of pi, and it should have functions to calculate both the area and the circumference of the
circle based on its radius.
The program should:
● Use a constant data member to represent the value of pi.
● Allow the user to input the radius of the circle.
● Compute and display the area and circumference using the appropriate formulas.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
class circle
{
const float PI;
float radius;
public:
circle(float r):PI(3.14),radius(r)
{}
float getArea()
{
return PI*radius*radius;
}
float getCircumference()
{
return 2*PI*radius;
}
void display()
{
cout<<"Radius:"<<radius<<endl;
cout<<"Area:"<<getArea()<<endl;
cout<<"Circumference:"<<getCircumference()<<endl;
}
};
int main()
{
float r;
cout<<"Enter the radius of the circle:";
cin>>r;
circle c(r);
c.display();
return 0;
}

Sample Input and Output:


[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the radius of the circle:4
Radius:4
Area:50.24
Circumference:25.12

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Ex: No: 2E Classes with Data Members and Member Functions
Date:

Question:

You are developing a supermarket billing system for a retail store. The system needs to handle the billing of
multiple products purchased by a customer. Each product has a price and a quantity, and the system should
calculate the total cost for the customer, including a final bill with the itemized list of purchased products.
Your task is to develop a program to implement the billing system using classes, member functions, and
constructors.
The program should:
● Store product details such as product name, price, and quantity.
● Calculate the total price for each product and generate the final bill.
● Use constructors to initialize product details when a customer adds items to their cart.
● Display the bill with the total cost for all items.
Aim:

Algorithm:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Program:

#include<iostream>
using namespace std;
class Product
{
public:
stringname;
float price;
int quantity;
void input()
{
cout<<"Product Name:";
cin>>name;
cout<<"Price:";
cin>>price;
cout<<"Quantity:";
cin>>quantity;
}
float totalprice()
{
return price*quantity;
}
void display()
{
cout<<name<<"\t"<<price<<"\t\t\t"<<quantity<<"\t\t"<<totalprice()<<endl;
}
};
int main()
{
int numProducts;
float totalcost=0;
Product products[5];
cout<<"Enter the number of Products:";
cin>>numProducts;
for(int i=0;i<numProducts;i++)
{
cout<<"Product:"<<(i+1)<<"\t"<<"details:
\n"; products[i].input();
}
cout<<"\nBill:"<<endl;
cout<<"Name\tPrice\tQuantity\tTotal\n";
for(int i=0;i<numProducts;i++)
{
products[i].display();
totalcost+=products[i].totalprice();
}
cout<<"\n Total Cost:"<<totalcost<<endl;
return 0;
}

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :


Sample Input and Output:
[cs24c132@cslinux~]$ g++ -o main main.cpp
[cs24c132@cslinux~]$ ./main
Enter the number of Products:2
Product:1 details:
Product Name:Apple
Price:100
Quantity:10
Product:2 details:
Product Name:Banana
Price:50
Quantity:10

Bill:
Name Price Quantity Total
Apple 100 10 1000
Banana 50 10 500
Total Cost:1500

Result:

Reg No : 2127240501132 CS22212 Object Oriented Programming Laboratory Page No :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy