22 lab c++
22 lab c++
roll no 112
BCA 2ND SEM
Practical-10
Objective:
Consider a base class employee and manager as derieved class.Use name and ID as base class
members.Inherit the member of the base class and Display the manager class with the salaryas an
additional data member.
Algorithm:
Program:
#include <iostream>
#include <string>
public:
// Constructor
Employee(string empName, string empID) : name(empName), ID(empID) {}
private:
float salary;
public:
// Constructor
Manager(string empName, string empID, float empSalary) : Employee(empName,
empID), salary(empSalary) {}
int main() {
// Creating an object of Manager class
Manager manager1("John Doe", "EMP001", 50000.0f);
return 0;
}
Output
Practical-11
Objective:
Illustrate the exception for divide overflow error for any mathematical expression
Algorithm:
Define integer variables for numerator and denominator.
1. Attempt to divide the numerator by the denominator.
2. Catch any exceptions thrown during the division.
3. If an exception is caught, print an error message indicating the divide overflow error.
4. If no exception is caught, print the result of the division.
CODE
#include <iostream>
#include <stdexcept>
int main() {
// Step 1: Define integer variables for numerator and denominator
int numerator = 10;
int denominator = 0;
int result;
try {
// Step 2: Attempt to divide the numerator by the denominator
result = numerator / denominator;
return 0;
}
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM
OUTPUT
Practical-12
Objective:
Create two objects for a class timer .Assume three data member hour,minutes and seconds use two
constructors for initilizing the objects .Add two time objects using operator overloading display
approriate value of hours ,minute and seconds after addition.
ALGORITHM
Define a class Timer with data members hour, minutes, and seconds.
1. Implement two constructors:
• One constructor initializes the object with provided values for hour, minutes, and
seconds.
• Another constructor initializes the object with default values.
2. Overload the + operator to add two Timer objects.
3. Create two Timer objects using both constructors.
4. Add the two Timer objects using the overloaded + operator.
5. Display the appropriate values of hours, minutes, and seconds after addition.
Program:
#include <iostream>
class Timer {
private:
int hours;
int minutes;
int seconds;
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM
public:
// Constructor with default values
Timer(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
int main() {
// Creating two Timer objects using different constructors
Timer t1(2, 30, 45); // 2 hours, 30 minutes, 45 seconds
Timer t2(1, 45, 20); // 1 hour, 45 minutes, 20 seconds
return 0;
}
OUTPUT
After addition:
Time: 4 hours, 16 minutes, 5 seconds
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM
PRACTICAL-09
Objective:WA cpp to demonstrate the use of new and delete operator for memory allocation
and de allocation
Algorithm:
Step1: Start
Step2: Include necessary header files:
Step3: Define the main function
Step4: Allocate memory for an integer dynamically using the new
operator:
Step5: Check if memory allocation was successful
Step6: Assign a value to the dynamically allocated memory:
Step7: Print the value:
Step8: Deallocate the memory using the delete operator:
Step9: Set the pointer to nullptr after deallocation:
Code
#include<iostream>
using namespace std;
int main()
{
int *ptr=new int;
if(ptr==NULL)
{
cout<<"Memory allocation is failed";
return 1;
}
*ptr=10;
cout<<"Value stored in memory loacation" <<*ptr<<endl;
delete ptr;
ptr=NULL;
return 0;
}
Output: