0% found this document useful (0 votes)
2 views

22 lab c++

The document contains multiple practical programming exercises focusing on C++ concepts such as inheritance, exception handling, operator overloading, and dynamic memory management. Each practical includes objectives, algorithms, and code examples demonstrating the implementation of these concepts. The output of each program is also provided, showcasing the results of the executed code.

Uploaded by

whymesoweird
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

22 lab c++

The document contains multiple practical programming exercises focusing on C++ concepts such as inheritance, exception handling, operator overloading, and dynamic memory management. Each practical includes objectives, algorithms, and code examples demonstrating the implementation of these concepts. The output of each program is also provided, showcasing the results of the executed code.

Uploaded by

whymesoweird
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME:yogesh paliwal

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:

Define a base class Employee with members name and ID.


1. Define a derived class Manager which inherits from the Employee class.
2. Add an additional data member salary to the Manager class.
3. Implement a constructor in the Manager class to initialize the name, ID, and salary.
4. Display the details of the manager, including name, ID, and salary.

Program:

#include <iostream>
#include <string>

using namespace std;

// Define the base class Employee


class Employee {
protected:
string name;
string ID;

public:
// Constructor
Employee(string empName, string empID) : name(empName), ID(empID) {}

// Method to display details


void displayDetails() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
}
};

// Define the derived class Manager which inherits from Employee


class Manager : public Employee {
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM

private:
float salary;

public:
// Constructor
Manager(string empName, string empID, float empSalary) : Employee(empName,
empID), salary(empSalary) {}

// Method to display details including salary


void displayDetails() {
Employee::displayDetails(); // Call base class method to display name and ID
cout << "Salary: " << salary << endl;
}
};

int main() {
// Creating an object of Manager class
Manager manager1("John Doe", "EMP001", 50000.0f);

// Displaying details of the manager


manager1.displayDetails();

return 0;
}

Output

Name: John Doe


ID: EMP001
Salary: 50000
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM

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;

// Step 5: If no exception is caught, print the result


std::cout << "Result: " << result << std::endl;
} catch (const std::exception& e) {
// Step 4: If an exception is caught, print an error message
std::cerr << "Exception caught: " << e.what() << std::endl;
}

return 0;
}
NAME:yogesh paliwal
roll no 112
BCA 2ND SEM

OUTPUT

Exception caught: integer divide by zero

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) {}

// Overloading + operator to add two Timer objects


Timer operator+(const Timer& t) {
Timer temp;
temp.seconds = seconds + t.seconds;
temp.minutes = minutes + t.minutes + temp.seconds / 60;
temp.hours = hours + t.hours + temp.minutes / 60;
temp.minutes %= 60;
temp.seconds %= 60;
return temp;
}

// Method to display the time


void displayTime() {
std::cout << "Time: " << hours << " hours, " << minutes << " minutes, " <<
seconds << " seconds" << std::endl;
}
};

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

// Adding the two Timer objects


Timer sum = t1 + t2;
// Displaying the result after addition
std::cout << "After addition:" << std::endl;
sum.displayTime();

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:

Value stored in memory loacation 10

You might also like