0% found this document useful (0 votes)
14 views33 pages

Program 8

The document describes creating a class called TEACHER with data members for salary and experience. It implements two constructors - a default constructor and a parameterized constructor. In main, it creates objects using each constructor and displays the output to illustrate their usage.

Uploaded by

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

Program 8

The document describes creating a class called TEACHER with data members for salary and experience. It implements two constructors - a default constructor and a parameterized constructor. In main, it creates objects using each constructor and displays the output to illustrate their usage.

Uploaded by

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

PROGRAM-8

Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86

OBJECTIVE: Design EMPLOYEE class contains the following members: data


members: Employee number, Employee name, Basic, DA, IT, Net Salary. Write member
function read () and print () to enter the data and display the employee records.

ALOGORITHM:
Input: A string st
Output: The counts of digits, whitespaces, and characters
1. Initialize variables digit, whitespace, and characters to 0.
2. Initialize an index variable i to 0.
3. While the character at st[i] is not the null character '\0':
a. If st[i] is an alphabetical character:
- Increment the characters count.
b. If st[i] is a whitespace character:
- Increment the whitespace count.
c. If st[i] is a digit:
- Increment the digit count.
d. Increment i.
4. Output the counts of digits, whitespaces, and characters.
PROGRAM:
#include<iostream>
#include<cstring>
using namespace std;
class EMPLOYEE{
private:
string name;
int Employee_number,basic;
float DA,TA,Net_salary;
public:
void write(EMPLOYEE *ep,string name,int number, int basic,int i);
void read(EMPLOYEE *ep,int size);
};
int main(){
int size;
cout<<"Enter the number of employee ";
cin>>size;
EMPLOYEE ep[size];
for(int i=0;i<size;i++){
cin.ignore();
string Name;
int Number,basic;
cout<<"Enter the name of employee "<<i+1<<" ";
getline(cin,Name);
cout<<"Enter the employee number of employee "<<i+1<<" ";
cin>>Number;
cout<<"Enter the basic pay of employee "<<i+1<<" ";
cin>>basic;
ep[i].write(ep,Name,Number,basic,i);}
ep[0].read(ep,size);
return 0;}
void EMPLOYEE:: write(EMPLOYEE *ep,string name,int number, int basic,int i){
ep[i].name=name;
ep[i].Employee_number=number;
ep[i].basic=basic;
ep[i].DA=basic*0.10;
ep[i].TA=basic*0.05;
ep[i].Net_salary=basic+ep[i].DA+ep[i].TA;
}
void EMPLOYEE :: read(EMPLOYEE *ep,int size){
for(int i=0;i<size;i++){
cout<<"The name of Employee "<<i+1<<" is "<<ep[i].name<<endl;
cout<<"The employee number of Employee "<<i+1<<" is
"<<ep[i].Employee_number<<endl;
cout<<"The basic pay of Employee "<<i+1<<" is "<<ep[i].basic<<endl;
cout<<"The net salary of Employee "<<i+1<<" is "<<ep[i].Net_salary<<endl;
}
}
OUTPUT:
PS C:\Users\amitm\OneDrive\Desktop\New folder\ c++> cd "c:\Users\amitm\OneDrive\
Desktop\New folder\ c++\" ; if ($?) { g++ practical5.cpp -o practical5 } ; if ($?) { .\practical5
}
Enter the number of employee 2
Enter the name of employee 1 bella
Enter the employee number of employee 1 1
Enter the basic pay of employee 1 10000
Enter the name of employee 2 king
Enter the employee number of employee 2 2
Enter the basic pay of employee 2 1000
The name of Employee 1 is bella
The employee number of Employee 1 is 1
The basic pay of Employee 1 is 10000
The net salary of Employee 1 is 11500
The name of Employee 2 is king
The employee number of Employee 2 is 2
The basic pay of Employee 2 is 1000
The net salary of Employee 2 is 1150

PROGRAM-9
Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86

OBJECTIVE:
Create a class TEACHER with data members’ salary and experience. Implement the
following:
a) Initialize using parametrized constructors.
b) Illustrate the use of the default constructor.

ALOGORITHM:
 Include necessary libraries.
 Declare a class Teacher with private member variables salary and experience.
 Declare constructors: default and parameterized.
 Define a member function print() to display salary and experience.
 In the main function:
 Create a Teacher object t1 using the default constructor and print its details.
 Create another Teacher object t2 using the parameterized constructor and print its
details.
 Define the constructors:
 Default constructor initializes salary to 100000 and experience to 1.
 Parameterized constructor initializes salary and experience based on the arguments
passed.

PROGRAM:
#include<iostream>
using namespace std;
class Teacher{
int salary;
int experience;
public:
Teacher();
Teacher(int a,int b);
void print(){
cout<<"The salary is "<<salary<<" and experience is "<<experience<<"year"<<endl;
}
};
int main(){
Teacher t1;
t1.print();
Teacher t2(200000,2);
t2.print();
return 0;
}
Teacher :: Teacher(){
salary=100000;
experience=1;
}
Teacher :: Teacher(int a,int b){
salary=a;
experience=b;
}

OUTPUT:
PS D:\NIVEDITA NAYAL\C++\c++ practice> cd "d:\NIVEDITA NAYAL\C++\c++
practice\" ; if ($?) { g++ practical8.cpp -o practical8 } ; if ($?) { .\practical8 }
The salary is 100000 and experience is 1year
The salary is 200000 and experience is 2year

PROGRAM-10
Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86

OBJECTIVE:
Create two objects for a class timer. Assume three data members’ hours, minutes, and
seconds. Use two constructors for initializing the objects. Add two-time objects using
operator overloading. Display appropriate values of hours, minutes, and seconds after
addition.

ALOGORITHM:
1. Define a class timer to represent time with three private member variables: hours,
minutes, and seconds.
2. Implement constructors to initialize time, including a default constructor and a
parameterized constructor.
3. Implement a copy constructor to create a copy of a timer object.
4. Declare a friend function display() to display the time.
5. Declare an overloaded friend function operator+ to add two timer objects.
6. In the display() function, output the time in hours, minutes, and seconds.
7. In the operator+ function, add the corresponding components of two timer objects and
return a new timer object with the sum of the times.
8. In the main() function:
 Prompt the user to enter time in hours, minutes, and seconds.
 Create a t1 object with the entered time.
 Display the time of t1.
 Create a t2 object by copying t1.
 Display the time of t2.
 Add t1 and t2 using the overloaded + operator.
 Display the sum of the times.

PROGRAM:
#include<iostream>
using namespace std;
class timer{
int hours;
int minutes;
int seconds;
public:
timer():hours(0),minutes(0),seconds(0){}
timer(int h,int m,int s):hours(h),minutes(m),seconds(s){
}
timer(const timer &t){
hours=t.hours;
minutes=t.minutes;
seconds=t.seconds;
}
friend void display(timer &t);
friend timer operator+(timer &t1,timer &t2);
};
void display(timer &t){
cout<<t.hours<<"hours "<<t.minutes<<"min "<<t.seconds<<"sec"<<endl;
}
timer operator+(timer &t1,timer &t2){
int hour,min,sec;
sec=t1.seconds+t2.seconds;
min=t1.minutes+t2.minutes+sec/60;
sec=sec%60;
hour=t1.hours+t2.hours+min/60;
min=min%60;
timer t(hour,min,sec);
return t;
}
int main(){
int x,y,z;
cout<<"Enter the time in hours "<<endl;
cin>>x;
cout<<"Enter the time in minutes "<<endl;
cin>>y;
cout<<"Enter the time in second "<<endl;
cin>>z;
timer t1(x,y,z);
display(t1);
timer t2(t1);
display(t2);
timer t3=t1+t2;
cout<<"The sum of two object is "<<endl;
display(t3);
return 0;
}

OUTPUT:
PS C:\Users\amitm\OneDrive\Desktop> cd "c:\Users\amitm\OneDrive\Desktop\" ; if ($?)
{ g++ mukulc++.cpp -o mukulc++ } ; if ($?) { .\mukulc++ }
Enter the time in hours
3
Enter the time in minutes
45
Enter the time in second
45
3hours 45min 45sec
3hours 45min 45sec
The sum of two object is
7hours 31min 30sec

PROGRAM NO. 08
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Create a class TEACHER with data members’ salary and


experience. Implement the following:
a) Initialize using parametrized constructors
b) Illustrate the use of the default constructor

ALGORITHM:
STEP1: START.
STEP2:Define the class Teacher:
Include two data members: salary and experience.
STEP3: Implement a parameterized constructor to initialize salary and
experience.
STEP4: Implement a default constructor.
Parameterized Constructor:
Implement a constructor that takes parameters for salary and experience and
initializes the respective data members.
STEP5: Default Constructor:
Implement a default constructor that initializes salary and experience to default
values.
STEP6: Illustrate the use of the default constructor:
STEP7: Create an object of the Teacher class using the default constructor.
STEP8: Display the values of salary and experience to verify they are initialized
correctly
STEP9: STOP.

PROGRAM:
#include <iostream>
using namespace std;
class TEACHER {
private:
float salary;
int experience;

public:
// Parameterized constructor
TEACHER(float s, int exp) {
salary = s;
experience = exp;
}

// Default constructor
TEACHER() {
salary = 0;
experience = 0;
}

// Function to set salary and experience


void setDetails(float s, int exp) {
salary = s;
experience = exp;
}

// Function to display details


void display() {
cout << "Salary: " << salary << endl;
cout << "Experience: " << experience << " years" << endl;
}
};

int main() {
float s;
int exp;

// Illustrating the use of default constructor


TEACHER teacher1; // Default constructor called
cout << "Details of Teacher 1 (default constructor):" << endl;
teacher1.display();

// Illustrating the use of parameterized constructor with user input


cout << "\nEnter salary of Teacher 2: ";
cin >> s;
cout << "Enter experience of Teacher 2 (in years): ";
cin >> exp;

TEACHER teacher2(s, exp); // Parameterized constructor called


cout << "\nDetails of Teacher 2 (parameterized constructor):" << endl;
teacher2.display();

return 0;
}

OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Details of Teacher 1 (default constructor):
Salary: 0
Experience: 0 years
Enter salary of Teacher 2: 50000
Enter experience of Teacher 2 (in years): 2

Details of Teacher 2 (parameterized constructor):


Salary: 50000
Experience: 2 years

--------------------------------
Process exited after 19.06 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 09
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Create two objects for a class timer. Assume three data
members’ hours, minutes, and seconds. Use two constructors for initializing
the objects. Add two-time objects using operator overloading. Display
appropriate values of hours, minutes, and seconds after addition.
ALGORITHM:
STEP1: start
STEP2: Define the class Timer:
Include three data members: hours, minutes, and seconds.
STEP3: Implement two constructors to initialize objects: one with default
values and another with parameters for hours, minutes, and seconds.
STEP4: Overload the + operator to add two Timer objects together.
Define the constructor:
Implement a constructor with default values (0 hours, 0 minutes, 0 seconds).
Implement another constructor with parameters to set hours, minutes, and
seconds.
Implement operator overloading:
STEP5: Overload the + operator to add two Timer objects together.
The addition operation should handle carry-over appropriately for seconds,
minutes, and hours.
STEP6: Display the result:
After adding two Timer objects, display the appropriate values of hours,
minutes, and seconds.
STEP 7: STOP
PROGRAM:
#include <instreams>
using namespace std;

class Timer {
private:
int hours;
int minutes;
int seconds;

public:
// Parameterized constructor
Timer(int h = 0, int m = 0, int s = 0) {
hours = h;
minutes = m;
seconds = s;
}
// Overloaded + operator to add two Timer objects
Timer operator+(const Timer& t) {
Timer sum;
sum.seconds = seconds + t.seconds;
sum.minutes = minutes + t.minutes + sum.seconds / 60;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.seconds %= 60;
sum.minutes %= 60;
return sum;
}

// Function to display time


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

int main() {
int h1, m1, s1, h2, m2, s2;

// Input for first Timer object


cout << "Enter hours, minutes, and seconds for Timer 1 (separated by
spaces): ";
cin >> h1 >> m1 >> s1;

// Input for second Timer object


cout << "Enter hours, minutes, and seconds for Timer 2 (separated by
spaces): ";
cin >> h2 >> m2 >> s2;

// Create Timer objects using the two constructors


Timer t1(h1, m1, s1);
Timer t2(h2, m2, s2);

// Add two Timer objects


Timer sum = t1 + t2;

// Display the result


cout << "\nResult after adding Timer 1 and Timer 2:" << endl;
sum.displayTime();

return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter hours, minutes, and seconds for Timer 1 (separated by spaces): 2
12
32
Enter hours, minutes, and seconds for Timer 2 (separated by spaces): 5
16
45

Result after adding Timer 1 and Timer 2:


Time: 7 hours, 29 minutes, 17 seconds
PROGRAM NO. 10
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Consider a base class EMPLOYEE and MANAGER as


derived classes. Use name and ID as base class members. Inherit the
members of the base class and display the MANAGER class with the salary
as an additional data member.
ALGORITHM:
STEP1: Start.
STEP2: Define the base class EMPLOYEE : Declare the base class
EMPLOYEE.

STEP3:Add two data members: name and ID.

STEP4:Define the derived class MANAGER:

STEP5:Declare the derived class MANAGER.

STEP6:Inherit the members of the base class EMPLOYEE.

STEP7:Add an additional data member salary.

STEP8:Create a function to display the MANAGER details:


STEP9:Declare a function in the MANAGER class to display the details.
STEP10:Display the name, ID, and salary of the manager.
STEP11:Stop.

PROGRAM:
#include <iostream>
#include <string>
using namespace std;

// Base class EMPLOYEE


class EMPLOYEE {
protected:
string name; ok
int ID;

public:
// Parameterized constructor
EMPLOYEE(string n, int id) {
name = n;
ID = id;
}

// Function to display employee details


void displayEmployee() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
}
};
// Derived class MANAGER
class MANAGER : public EMPLOYEE {
private:
float salary;

public:
// Parameterized constructor
MANAGER(string n, int id, float sal) : EMPLOYEE(n, id) {
salary = sal;
}

// Function to display manager details


void displayManager() {
displayEmployee(); // Call base class function to display employee details
cout << "Salary: $" << salary << endl;
}
};

int main() {
string name;
int ID;
float salary;

// Input for manager details


cout << "Enter name of the manager: ";
getline(cin, name);
cout << "Enter ID of the manager: ";
cin >> ID;
cout << "Enter salary of the manager: ";
cin >> salary;

// Create manager object


MANAGER manager(name, ID, salary);

// Display manager details


cout << "\nManager Details:" << endl;
manager.displayManager();

return 0;
}

OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter name of the manager: NIVEDITA NAYAL
Enter ID of the manager: 34562
Enter salary of the manager: 800000

Manager Details:
Name: NIVEDITA NAYAL
ID: 34562
Salary: $800000

--------------------------------
Process exited after 20.29 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 11
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Write a C++ program to demonstrate the use of new and


delete operators for memory allocation and deallocation.
ALGORITHM:
STEP1: Start.
Allocate memory using the new operator:
STEP2: Declare a pointer of the desired data type (int, double, char, etc.).

STEP3: Use the new operator to allocate memory dynamically.

STEP4: Check if memory allocation was successful:

STEP5: Check if the pointer is nullptr to verify if memory allocation was


successful.

STEP: Assign values to the dynamically allocated memory (optional):


Use the dereference operator (*) to assign values to the dynamically allocated
memory.

STEP6: Deallocate memory using the delete operator:


Use the delete operator to release the dynamically allocated memory.
This step is essential to prevent memory leaks.
STEP7: Stop.

PROGRAM:
#include <iostream>
using namespace std;
int main() {
// Declare a pointer to int
int* ptr;
// Allocate memory for an integer using the new operator
ptr = new int;
// Check if memory allocation was successful
if (ptr == nullptr) {
cout << "Memory allocation failed!" << endl;
return 1;
}
// Assign a value to the dynamically allocated memory (optional)
*ptr = 42;
// Display the value
cout << "Value stored in dynamically allocated memory: " << *ptr << endl;
// Deallocate memory using the delete operator
delete ptr;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Value stored in dynamically allocated memory: 42

--------------------------------
Process exited after 1.139 seconds with return value 0

PROGRAM NO. 12
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Store a list of integers in a file. Check whether the number is


ODD or EVEN. Send the odd numbers to the ODD.dat file and even
numbers to the EVEN.dat file.
ALGORITHM:
STEP1: START
STEP2: Open an output file stream to create or overwrite the "ODD.dat" file for
odd numbers.
STEP3: Open another output file stream to create or overwrite the "EVEN.dat"
file for even numbers.

STEP4: Prompt the user to enter the number of integers to be stored in the list.

STEP5: Read each integer from the user and store it in a list or array.

STEP6: Loop through each integer in the list or array:

STEP7: Check whether the current integer is odd or even using the modulo (%)
operator.
If the integer is odd, write it to the "ODD.dat" file.
If the integer is even, write it to the "EVEN.dat" file.
Close both output file streams.
STEP8: STOP.

PROGRAM:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream inputFile("input.txt"); // Open input file
ofstream oddFile("ODD.dat"); // Open ODD.dat file
ofstream evenFile("EVEN.dat"); // Open EVEN.dat file

if (!inputFile) {
cerr << "Error: Unable to open input file." << endl;
return 1;
}

if (!oddFile || !evenFile) {
cerr << "Error: Unable to create output files." << endl;
return 1;
}

int number;
while (inputFile >> number) {
if (number % 2 == 0) {
evenFile << number << endl; // Write even number to EVEN.dat
} else {
oddFile << number << endl; // Write odd number to ODD.dat
}
}

cout << "Numbers categorized and stored in files successfully." << endl;

// Close all file streams


inputFile.close();
oddFile.close();
evenFile.close();

return 0;
}

OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Error: Unable to open input file.

--------------------------------
Process exited after 1.287 seconds with return value 1
Press any key to continue . . .
PROGRAM NO. 14
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Illustrate the exception for divide overflow error for any
mathematical expression.
ALGORITHM:
STEP1: START.
STEP2: Prompt the user for input:
Ask the user to enter two integer operands for the mathematical expression.
Perform the division operation:

STEP3: Divide the first operand by the second operand.


Check for divide overflow:
If the second operand is zero, throw a divide by zero exception.
If the first operand is INT_MIN (-2147483648) and the second operand is -1,
throw a divide overflow exception.

STEP4: Display the result (optional):


If no exception is thrown, display the result of the division operation.
Handle exceptions:

STEP5: Catch and handle any exceptions thrown during the division operation.

STEP6: STOP.
PROGRAM:
#include <iostream>
#include <limits>
using namespace std;

int main() {
int numerator, denominator;
double result;

// Prompt the user for input


cout << "Enter the numerator: ";
cin >> numerator;

cout << "Enter the denominator: ";


cin >> denominator;

try {
// Check for divide overflow
if (denominator == 0) {
throw "Divide by zero error!";
}

if ((numerator == numeric_limits<int>::min()) && (denominator == -1)) {


throw "Divide overflow error!";
}

// Perform the division


result = static_cast<double>(numerator) / denominator;
cout << "Result: " << result << endl;
}
catch (const char* msg) {
cerr << "Error: " << msg << endl;
}

return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter the numerator: 10
Enter the denominator: 0
Error: Divide by zero error!
PROGRAM NO. 13
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86

OBJECTIVE: Create a vector for storing a list of integers using the STL.
ALGORITHM:
STEP1: Include the necessary header file:
#include <vector> to use the vector container provided by the STL.

STEP2: Declare a vector to store integers:


Declare a vector named numbers of type vector<int>.

STEP 3: Prompt the user for input:

STEP4: Ask the user to enter the number of integers they want to store in the
vector.

STEP 5: Input integers from the user and store them in the vector:
Use a loop to input each integer from the user and add it to the vector using the
push_back() method.

STEP 6: Display the elements of the vector (optional):


Use a loop or iterator to traverse the vector and display its elements.
PROGRAM:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Declare a vector to store integers
vector<int> numbers;
// Prompt the user for input
int n;
cout << "Enter the number of integers: ";
cin >> n;
// Input integers from the user and store them in the vector
cout << "Enter " << n << " integers:" << endl;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
numbers.push_back(num); // Add the number to the vector
}
// Display the elements of the vector
cout << "Elements of the vector:" << endl;
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter the number of integers:
3
Enter 3 integers:
23
3
43
Elements of the vector:
23 3 43

--------------------------------
Process exited after 12.53 seconds with return value 0
Press any key to continue . .

You might also like