Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

YAKSHIC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Name: Yakshi Chauhan Course: BCASection: H Univ.

Roll num: 2103199

// Q.No. 14 program to define 2 velocities

#include <iostream>

using namespace std;

class velocity2;

// class velocity1

class velocity1 {

private:

float v1;

public:

velocity1() {

v1 = 0;

velocity1(float v) {

v1 = v;

operator velocity2();

void show() {

cout << "velocity1: " << v1 << " km/h" << endl;

};

// class velocity2

class velocity2 {
private:

float v2;

public:

velocity2() {

v2 = 0;

velocity2(float v) {

v2 = v;

operator velocity1();

void display() {

cout << "velocity2: " << v2 << " m/s" << endl;

};

velocity1::operator velocity2() {

return velocity2(v1 / 3.6);

velocity2::operator velocity1() {

return velocity1(v2 * 3.6);

// main function

int main() {

velocity1 v1(60);
velocity2 v2;

v2=v1;

v1.show(); }

v2.display();

return 0;

Output:

velocity1: 60 km/h

velocity2: 16.6667 m/s

Name: Yakshi Chauhan Course: BCASection: H Univ. Roll num: 2103199

// Q.No. 15 program of bank

//Program 13 to create a class Bank and perform the operation

#include<iostream>

using namespace std;

class Bank {

public:

string name, Address;

int AC_num;

float balance;
void operator++() {

balance += 1000;

void operator+(float amountToadd) {

Bank sum;

sum.balance = balance + amountToadd;

cout << sum.balance << " + " << amountToadd << " = " <<
sum.balance<<endl;

void operator-(float amountToDeduct) {

Bank diff;

diff.balance = balance - amountToDeduct;

cout << diff.balance << " - " << amountToDeduct << " = " <<
diff.balance<<endl;

void compare(const Bank& other) const {

if (balance < other.balance) {

cout <<endl<< other.name << " has a larger balance than " << name;

} else {

cout <<endl<< name << " has a larger balance than " << other.name;

void getDetails() {
cout << "Enter the name of the customer" << endl;

cin >> name;

cout << "Enter the address of the customer" << endl;

cin >> Address;

cout << "Enter the bank A/C No." << endl;

cin >> AC_num;

cout << "Enter the initial balance" << endl;

cin >> balance;

void show() {

cout << "Name: " << name << endl;

cout << "Address: " << Address << endl;

cout << "A/C number: " << AC_num << endl;

cout << "Balance: " << balance << endl;

};

int main() {

Bank ac1, ac2;

//Getting Details

cout << "Enter the details of account 1" << endl;

ac1.getDetails();

cout << "Enter the details of account 2" << endl;

ac2.getDetails();
//Performing Operations

++ac1;

ac1.show();

ac1.operator+(194.8);

ac1.operator-(150.8);

// Comparing balances

ac1.compare(ac2);

return 0;

Output:

Enter the details of account 1

Enter the name of the customer

Harry

Enter the address of the customer

Hogwarts

Enter the bank A/C No.

9048253

Enter the initial balance

100

Enter the details of account 2

Enter the name of the customer

Hermoine

Enter the address of the customer


Hogwarts

Enter the bank A/C No.

9534235

Enter the initial balance

200

Name: Harry

Address: Hogwarts

A/C number: 9048253

Balance: 1100

1294.8 + 194.8 = 1294.8

949.2 - 150.8 = 949.2

Harry has a larger balance than Hermoine

Name: Yakshi Chauhan Course: BCA Section: H Univ. Roll num: 2103199

// Q.No. 16 program to implement template

#include <iostream>

#define MAX 10

using namespace std;

template < class T > class ARRAY

public:

int top;

T Data[MAX];
ARRAY ()

top = -1;

// Function to insert element

void insert ()

T ele;

if (top == MAX - 1)

cout << "Array is Full" << endl;

return;

else

cout << "Enter the element" << endl;

cin >> ele;

top++;

Data[top] = ele;

// Function to display elements

void display ()
{

if (top == -1)

cout << "Array is empty";

else

int i;

for (i = top; i >= 0; i--)

cout << Data[i] << " ";

cout << endl;

// Function to Sort

void sort() {

if (top == -1) {

cout << "Array is empty" << endl;

else {

for (int i = 0; i < top; i++) {

for (int j = 0; j < top - i; j++) {


if (Data[j] < Data[j + 1]) {

T temp = Data[j];

Data[j] = Data[j + 1];

Data[j + 1] = temp;

cout << "Array sorted successfully" << endl;

};

int main ()

int ch;

ARRAY < int >stk1;

do

cout << "Enter the your choice" << endl;

cout << "1.Push\t2.Display\t3.Sort\t4.Exit\n";

cin >> ch;

switch (ch)

case 1:
stk1.insert ();

break;

case 2:

stk1.display ();

break;

case 3:

stk1.sort ();

break;

while (ch != 4);

return 0;

Output:

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

Array is empty

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

Enter the element

89
Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

Enter the element

43

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

Enter the element

450

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

184 450 43 89

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

Array sorted successfully

Enter the your choice

1.Push 2.Display 3.Sort 4.Exit

43 89 184 450

Enter the your choice


1.Push 2.Display 3.Sort 4.Exit

….Program finished with exit code 0

Name: Yakshi Chauhan Course: BCA Section: H Univ. Roll num:


2103199

// Q.No.15 Program to show implementation of virtual function

#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base class display()" << endl;
}
};
class Derived1 : public Base {
public:
void display() override {
cout << "Derived1 class display()" << endl;
}
};
class Derived2 : public Derived1 {
public:
void display() override {
cout << "Derived2 class display()" << endl;
}
};

int main() {
Base* basePtr;
Derived1 derived1Obj;
Derived2 derived2Obj;
basePtr = &derived1Obj;
basePtr->display();
basePtr = &derived2Obj;
basePtr->display();
return 0;
}

Output:
Derived1 class display()
Derived2 class display()

Name: Yakshi Chauhan Course: BCASection: H Univ. Roll num: 2103199

// Q.No. 17 Program to receive command line arguments in C++

#include <iostream>

int main(int argc, char* argv[]) {

// Display the number of command line arguments

cout << "Number of arguments: " << argc << endl;

// Display each command line argument

cout << "Arguments:" << endl;

for (int i = 0; i < argc; i++) {

cout << "Argument " << i << ": " << argv[i] << endl;

return 0;
}

Output:

Number of arguments: 1

Arguments:

Argument 0:

Name: Yakshi Chauhan Course: BCA Section: H Univ.


Roll num: 2103199

//Q.No. 16 Program to perform file operation

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <algorithm>

#include <limits>

class Employee {

private:

string empName;

string department;

string empId;

int age;

double salary;

public:
// Default constructor

Employee() {}

// Parameterized constructor

Employee(const string& empName, const string& department, const


string& empId, int age, double salary)

: empName(empName), department(department), empId(empId), age(age),


salary(salary) {}

// Getter functions

string getEmpName() const {

return empName;

string getDepartment() const {

return department;

string getEmpId() const {

return empId;

int getAge() const {

return age;

double getSalary() const {

return salary;

// Function to insert an employee record into the file


void insertEmployee(const string& fileName) {

ofstream file(fileName, ios::app);

if (file.is_open()) {

file << empName << " " << department << " " << empId << " " <<
age << " " << salary << endl;

file.close();

cout << "Employee record inserted successfully." << endl;

else {

cout << "Error opening the file." << endl;

// Function to display all employee records from the file

static void displayAllEmployees(const string& fileName) {

ifstream file(fileName);

if (file.is_open()) {

string line;

while ( getline(file, line)) {

cout << line << endl;

file.close();

else {
cout << "Error opening the file." << endl;

// Function to search for an employee by employee ID

static void searchEmployee(const string& fileName, const string& empId)


{

ifstream file(fileName);

if (file.is_open()) {

string line;

bool found = false;

while ( getline(file, line)) {

string id = line.substr(line.find_last_of(' ') + 1);

if (id == empId) {

cout << "Employee found:" << endl;

cout << line << endl;

found = true;

break;

file.close();

if (!found) {

cout << "Employee with ID " << empId << " not found." <<
endl;

}
}

else {

cout << "Error opening the file." << endl;

// Function to delete an employee by employee ID

static void deleteEmployee(const string& fileName, const string& empId) {

ifstream inputFile(fileName);

if (inputFile.is_open()) {

string line;

vector< string> lines;

bool found = false;

while ( getline(inputFile, line)) {

string id = line.substr(line.find_last_of(' ') + 1);

if (id != empId) {

lines.push_back(line);

else {

found = true;

inputFile.close();
ofstream outputFile(fileName);

if (outputFile.is_open()) {

for (const auto& line : lines) {

outputFile << line << endl;

outputFile.close();

if (found) {

cout << "Employee with ID " << empId << " deleted
successfully." << endl;

else {

cout << "Employee with ID " << empId << " not found." <<
endl;

else {

cout << "Error opening the file." << endl;

else {

cout << "Error opening the file." << endl;

}
// Function to edit an employee by employee ID

static void editEmployee(const string& fileName, const string& empId) {

fstream file(fileName);

if (file.is_open()) {

string line;

vector< string> lines;

bool found = false;

while ( getline(file, line)) {

string id = line.substr(line.find_last_of(' ') + 1);

if (id != empId) {

lines.push_back(line);

else {

found = true;

cout << "Enter new details for the employee (empName


department empId age salary): ";

getline( cin, line);

lines.push_back(line);

file.close();
ofstream outputFile(fileName);

if (outputFile.is_open()) {

for (const auto& line : lines) {

outputFile << line << endl;

outputFile.close();

if (found) {

cout << "Employee with ID " << empId << " edited
successfully." << endl;

else {

cout << "Employee with ID " << empId << " not found." <<
endl;

else {

cout << "Error opening the file." << endl;

else {

cout << "Error opening the file." << endl;

}
// Function to sort the employee records by employee name

static void sortEmployees(const string& fileName) {

ifstream file(fileName);

if (file.is_open()) {

vector< string> lines;

string line;

while ( getline(file, line)) {

lines.push_back(line);

file.close();

sort(lines.begin(), lines.end(),

[](const string& a, const string& b) {

string nameA = a.substr(0, a.find(' '));

string nameB = b.substr(0, b.find(' '));

return nameA < nameB;

);

ofstream outputFile(fileName);

if (outputFile.is_open()) {

for (const auto& line : lines) {

outputFile << line << endl;

outputFile.close();
cout << "Employee records sorted by employee name." << endl;

else {

cout << "Error opening the file." << endl;

else {

cout << "Error opening the file." << endl;

};

int main() {

string fileName = "d:\\emp.dat";

int choice;

string empId;

while (true) {

cout << "Menu:\n";

cout << "1. Insert an employee\n";

cout << "2. Display all employees\n";

cout << "3. Search an employee\n";

cout << "4. Delete an employee\n";

cout << "5. Edit an employee\n";


cout << "6. Sort employees by name\n";

cout << "7. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: {

string empName, department, empId;

int age;

double salary;

cin.ignore( numeric_limits< streamsize>::max(), '\n');

cout << "Enter employee name: ";

getline( cin, empName);

cout << "Enter department: ";

getline( cin, department);

cout << "Enter employee ID: ";

getline( cin, empId);

cout << "Enter age: ";

cin >> age;

cout << "Enter salary: ";

cin >> salary;

Employee employee(empName, department, empId, age, salary);

employee.insertEmployee(fileName);

break;
}

case 2: {

Employee::displayAllEmployees(fileName);

break;

case 3: {

cout << "Enter the employee ID to search: ";

cin >> empId;

Employee::searchEmployee(fileName, empId);

break;

case 4: {

cout << "Enter the employee ID to delete: ";

cin >> empId;

Employee::deleteEmployee(fileName, empId);

break;

case 5: {

cout << "Enter the employee ID to edit: ";

cin >> empId;

Employee::editEmployee(fileName, empId);

break;

}
case 6: {

Employee::sortEmployees(fileName);

break;

case 7: {

cout << "Exiting the program.\n";

return 0;

default: {

cout << "Invalid choice. Please try again.\n";

break;

cin.ignore( numeric_limits< streamsize>::max(), '\n');

cout << "Press Enter to continue...";

cin.get();

cout << endl;

Output:

/tmp/ufzld80ys5.o

Menu:

1. Insert an employee
2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 1

Enter employee name: harry

Enter department: gryffindor

Enter employee ID: 3482

Enter age: 22

Enter salary: 4000

Employee record inserted successfully.

Press Enter to continue...

Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 1


Enter employee name: Ron

Enter department: gryffindor

Enter employee ID: 2891

Enter age: 23

Enter salary: 2500

Employee record inserted successfully.

Press Enter to continue...

Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 1

Enter employee name: Draco

Enter department: Slytherin

Enter employee ID: 8097

Enter age: 22

Enter salary: 5000

Employee record inserted successfully.

Press Enter to continue...


Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 2

harry gryffindor 3482 22 4000

Ron gryffindor 2891 23 2500

Draco Slytherin 8097 22 5000

Press Enter to continue...

Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 6

Employee records sorted by employee name.


Press Enter to continue...

Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 2

Draco Slytherin 8097 22 5000

Ron gryffindor 2891 23 2500

harry gryffindor 3482 22 4000

Press Enter to continue...

Menu:

1. Insert an employee

2. Display all employees

3. Search an employee

4. Delete an employee

5. Edit an employee

6. Sort employees by name

7. Exit

Enter your choice: 7


Exiting the program.

…Program exited with return code 1078

Name: Yakshi Chauhan Course: BCA Section: H Univ.


Roll num: 2103199

//Q.No. 13 program to find and replace string

#include <iostream>

#include <string>

using namespace std;

void replaceAllOccurrences(string& str, const string& substr, const string&


replacement) {

size_t pos = 0;

while ((pos = str.find(substr, pos)) != string::npos) {

str.replace(pos, substr.length(), replacement);

sos += replacement.length();

cnt main() {

string str;

string substr;

string replacement;

// Read the input string, substring, and replacement

cout << “Enter the string: “;

getline( cin, str);


cout << “Enter the substring to replace: “;

getline(cin, substr);

cout << “Enter the replacement string: “;

getline(cin, replacement);

// Replace all occurrences of the substring

replaceAllOccurrences(str, substr, replacement);

// Print the modified string

cout << “Modified string: “ << str << endl;

return 0;

Output:

Enter the string: I am harry from gryffindor

Enter the substring to replace: harry

Enter the replacement string: Hermione

Modified string: I am Hermione from gryffindor

Name: Yakshi Chauhan Course: BCASection: H Univ. Roll num: 2103199

//Q.No. 17 program for exception handling

#include<iostream>

using namespace std;

int main(){

double n,d,division;

cout<<"Enter numerator and denominator"<<endl;


cin>>n>>d;

try{

if(d==0){

throw d;

division=n/d;

cout<<n<<"/"<<d<<"="<<division<<endl;

catch(int num_exception){

cout<<"Error: can't divide by "<<num_exception;

return 0;

Output:

Enter numerator and denominator

30

terminate called after throwing an instance of 'double'

--------------------------------

Process exited after 17.27 seconds with return value 3

Press any key to continue . . .

You might also like