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

Ques Ans

Uploaded by

noorjohar99
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)
19 views

Ques Ans

Uploaded by

noorjohar99
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/ 11

3.

Design a class 'employee' with following data members:

Employee Number, Employee Name, Department, Basic Salary, Bonus and Tax.

Write a program to input data of any number of employee and display the same input data with net
salary of each employee, Where Net Salary - Basic Salary + Bonus-Tax

#include <iostream>

#include <vector>

#include <string>

using namespace std;

class Employee {

private:

int employeeNumber;

string employeeName;

string department;

double basicSalary;

double bonus;

double tax;

public:

// Constructor

Employee(int number, string name, string dept, double salary, double bonusAmt, double taxAmt) {

employeeNumber = number;

employeeName = name;

department = dept;

basicSalary = salary;

bonus = bonusAmt;

tax = taxAmt;

}
// Method to calculate net salary

double calculateNetSalary() {

return basicSalary + bonus - tax;

// Method to display employee data

void displayEmployeeData() {

cout << "Employee Number: " << employeeNumber << endl;

cout << "Employee Name: " << employeeName << endl;

cout << "Department: " << department << endl;

cout << "Basic Salary: " << basicSalary << endl;

cout << "Bonus: " << bonus << endl;

cout << "Tax: " << tax << endl;

cout << "Net Salary: " << calculateNetSalary() << endl;

};

int main() {

int n;

cout << "Enter the number of employees: ";

cin >> n;

vector<Employee> employees;

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

int empNum;

string empName, dept;

double basicSal, bonusAmt, taxAmt;

cout << "Enter details for employee " << i + 1 << ":" << endl;

cout << "Employee Number: ";


cin >> empNum;

cout << "Employee Name: ";

cin.ignore();

getline(cin, empName);

cout << "Department: ";

getline(cin, dept);

cout << "Basic Salary: ";

cin >> basicSal;

cout << "Bonus: ";

cin >> bonusAmt;

cout << "Tax: ";

cin >> taxAmt;

employees.push_back(Employee(empNum, empName, dept, basicSal, bonusAmt, taxAmt));

// Displaying employee data

cout << "\nEmployee Data:\n";

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

cout << "\nEmployee " << i + 1 << ":\n";

employees[i].displayEmployeeData();

cout << endl;

return 0;

}
4. Design an application for a bank with account number, account holder, city and opening balance as
data members. Use constructor and destructor in your application. Use two additional functions
deposit) to deposit cash in a particular account number account and withdraw() to withdraw cash
from a particular account number account.

#include <iostream>

#include <string>

#include <map>

using namespace std;

class Bank {

private:

int accountNumber;

string accountHolder;

string city;

double openingBalance;

public:

// Constructor

Bank(int accNum, string accHolder, string cityName, double openingBal) {

accountNumber = accNum;

accountHolder = accHolder;

city = cityName;

openingBalance = openingBal;

cout << "Account created successfully." << endl;

// Destructor

~Bank() {

cout << "Account deleted successfully." << endl;

}
// Function to deposit cash

void deposit(double amount) {

openingBalance += amount;

cout << "Amount deposited successfully. Current balance: " << openingBalance << endl;

// Function to withdraw cash

void withdraw(double amount) {

if (amount > openingBalance) {

cout << "Insufficient balance. Withdrawal unsuccessful." << endl;

} else {

openingBalance -= amount;

cout << "Amount withdrawn successfully. Current balance: " << openingBalance << endl;

// Function to display account details

void displayAccountDetails() {

cout << "Account Number: " << accountNumber << endl;

cout << "Account Holder: " << accountHolder << endl;

cout << "City: " << city << endl;

cout << "Opening Balance: " << openingBalance << endl;

};

int main() {

// Creating a map to store account objects with account number as key

map<int, Bank*> accounts;

// Creating accounts
Bank *acc1 = new Bank(1001, "John Doe", "New York", 5000);

Bank *acc2 = new Bank(1002, "Jane Smith", "Los Angeles", 7000);

// Adding accounts to the map

accounts[1001] = acc1;

accounts[1002] = acc2;

// Depositing and withdrawing cash

accounts[1001]->deposit(2000);

accounts[1002]->withdraw(3000);

// Displaying account details

cout << "\nAccount Details:" << endl;

accounts[1001]->displayAccountDetails();

cout << endl;

accounts[1002]->displayAccountDetails();

cout << endl;

// Deleting accounts

delete acc1;

delete acc2;

return 0;

}
6. b) What do you mean by default parameter(s) of a function? Explain with the help of an example.

Default parameters in a function refer to the values assigned to parameters that are automatically
used if the caller doesn't provide a value for those parameters. They allow functions to be more
flexible by providing default behavior while still allowing customization when necessary.

Here's an example in C++:

cpp

#include <iostream>

using namespace std;

// Function with default parameters

void greet(string name = "Guest") {

cout << "Hello, " << name << "!" << endl;

int main() {

// Calling the function without providing a parameter

greet(); // Output: Hello, Guest!

// Calling the function with a parameter

greet("Alice"); // Output: Hello, Alice!

return 0;

In this example, the greet function has a default parameter name set to "Guest". When the function
is called without providing a parameter, it uses the default value "Guest". However, if a parameter is
provided (e.g., "Alice"), it overrides the default value and uses the provided value instead.
8. b) Write a program on data conversions

Object to float data type and Float data type to object.

Sure, here's a simple C++ program that demonstrates data conversions from object to float data type
and from float data type to object:

cpp

#include <iostream>

#include <typeinfo>

using namespace std;

int main() {

// Object to float conversion

Object obj = 10; // Assuming Object is a class that holds an integer value

float floatValue = static_cast<float>(obj.getValue()); // Assuming getValue() returns the integer


value

cout << "Float value: " << floatValue << endl;

// Float to object conversion

float floatValue2 = 3.14;

Object obj2 = static_cast<Object>(floatValue2); // Assuming Object has a constructor that takes a


float value

cout << "Object type: " << typeid(obj2).name() << endl;

return 0;

Replace Object with the appropriate class name you are using, and adjust the conversion logic
accordingly based on your actual implementation.
9. b) How will you use class templates in a C++ program? Explain with example.

In C++, class templates provide a way to create generic classes that can work with any data type.
They allow you to define a blueprint for a class without specifying the actual data type it will work
with. This provides flexibility and reusability in code, as you can create instances of the class with
different data types while still maintaining a common structure and behavior.

Here's an example to illustrate the usage of class templates in C++:

```cpp

#include <iostream>

// Define a generic class template for a Stack

template <typename T>

class Stack {

private:

T* stackArray; // Array to store elements of type T

int capacity; // Maximum capacity of the stack

int top; // Index of the top element in the stack

public:

// Constructor to initialize the stack

Stack(int size) : capacity(size), top(-1) {

stackArray = new T[capacity];

// Destructor to deallocate memory

~Stack() {

delete[] stackArray;

// Function to push an element onto the stack


void push(T element) {

if (top == capacity - 1) {

std::cout << "Stack Overflow! Cannot push more elements." << std::endl;

return;

stackArray[++top] = element;

// Function to pop an element from the stack

T pop() {

if (top == -1) {

std::cout << "Stack Underflow! Cannot pop from an empty stack." << std::endl;

return T(); // Return default value of type T

return stackArray[top--];

// Function to check if the stack is empty

bool isEmpty() {

return (top == -1);

// Function to check if the stack is full

bool isFull() {

return (top == capacity - 1);

};

int main() {

// Create a stack of integers

Stack<int> intStack(5);
// Push some integers onto the stack

intStack.push(10);

intStack.push(20);

intStack.push(30);

// Pop elements from the stack

std::cout << "Popped element: " << intStack.pop() << std::endl;

std::cout << "Popped element: " << intStack.pop() << std::endl;

// Create a stack of characters

Stack<char> charStack(3);

// Push some characters onto the stack

charStack.push('a');

charStack.push('b');

charStack.push('c');

// Pop elements from the character stack

std::cout << "Popped element: " << charStack.pop() << std::endl;

std::cout << "Popped element: " << charStack.pop() << std::endl;

return 0;

```

In this example, we define a template class `Stack` that can work with any data type `T`. We can
create instances of `Stack` with different data types, such as `int` and `char`, by specifying the desired
data type within angle brackets (`<>`) when declaring objects. This allows us to reuse the same stack
implementation for different types of data while maintaining type safety and code clarity.

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