Astha doc
Astha doc
#include <iostream>
#include <iomanip> // For formatting the output
using namespace std;
int main() {
string name;
double units, totalCharges = 0.0, surcharge = 0.0;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string depositorName;
int accountNumber;
string accountType;
double balance;
public:
// Function to assign initial values
void initialize(string name, int accNum, string accType, double
initialBalance) {
depositorName = name;
accountNumber = accNum;
accountType = accType;
balance = initialBalance;
}
int main() {
// Array to store 5 customers' bank accounts
BankAccount customers[5];
cout << "\nEnter details for customer " << i + 1 << ":\n";
cout << "Name: ";
cin >> name;
cout << "Account Number: ";
cin >> accNum;
cout << "Account Type: ";
cin >> accType;
cout << "Initial Balance: ";
cin >> initialBalance;
// Deposit 5000
customers[customerIndex].deposit(5000);
// Withdraw 2000
customers[customerIndex].withdraw(2000);
return 0;
}
public:
// Function to read data for a person
void readPersonData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
public:
// Function to read data for a student
void readStudentData() {
readPersonData(); // Call base class function
cout << "Enter percentage: ";
cin >> percentage;
}
public:
// Function to read data for a teacher
void readTeacherData() {
readPersonData(); // Call base class function
cout << "Enter salary: ";
cin >> salary;
}
// Main function
int main() {
// Create a Student object and read/display data
cout << "\n--- Student Data ---" << endl;
Student student;
student.readStudentData();
student.displayStudentData();
return 0;
}
class Student {
private:
string name;
int rollNo;
float marks1, marks2, marks3;
float percentage;
public:
// Function to input student data
void inputData() {
cout << "Enter student's name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks for 3 subjects (out of 100): \n";
cout << "Subject 1: ";
cin >> marks1;
cout << "Subject 2: ";
cin >> marks2;
cout << "Subject 3: ";
cin >> marks3;
}
int main() {
// Create a Student object
Student student;
return 0;
}
5. Create one class time which has hour, minute and second as data
member. Now write input function to input class values and find time in
the form of minute
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
public:
// Function to input time values (hours, minutes, seconds)
void inputTime() {
cout << "Enter hours: ";
cin >> hours;
cout << "Enter minutes: ";
cin >> minutes;
cout << "Enter seconds: ";
cin >> seconds;
}
return 0;
}
public:
// Function to input vehicle data
void getData() {
cout << "Enter registration number: ";
cin >> registrationNumber;
cout << "Enter fuel type (Petrol/Diesel/Electric): ";
cin >> fuelType;
}
return 0;
}
7. Write a program that consists of two classes: Time12 and Time24. •
Time12: This class maintains time in a 12-hour format (e.g., 3:45 PM). •
Time24: This class maintains time in a 24-hour format (e.g., 15:45). The
program should allow: 1. Conversion between the two time formats
(Time12 to Time24 and vice versa). 2. Display of the time in both
formats. 3. Input of time in either format and updating it accordingly. 4.
Any additional functionality to manipulate or compare time objects if
required. Make sure to implement the necessary methods to achieve
these functionalities in both classes
#include <iostream>
#include <iomanip>
using namespace std;
public:
// Default constructor
Time12(int h = 12, int m = 0, bool pm = false) : hours(h), minutes(m),
isPM(pm) {}
public:
// Default constructor
Time24(int h = 0, int m = 0) : hours(h), minutes(m) {}
// Main program
int main() {
Time12 t12;
Time24 t24;
return 0;
}
class DM {
private:
float meters; // Distance in meters
float centimeters; // Distance in centimeters
public:
DM(float m = 0, float cm = 0) : meters(m), centimeters(cm) {}
class DB {
private:
float feet; // Distance in feet
float inches; // Distance in inches
public:
DB(float ft = 0, float in = 0) : feet(ft), inches(in) {}
int main() {
DM dm;
DB db;
// Add distances
DM result = addDistances(dm, db);
return 0;
}
class TelephoneDirectory {
private:
// Map to store the name as the key and phone number as the value
unordered_map<string, string> directory;
public:
// Method to add a new entry in the directory
void add(string name, string phoneNumber) {
directory[name] = phoneNumber;
cout << "Entry added for " << name << endl;
}
int main() {
TelephoneDirectory directory;
int choice;
string name, phoneNumber;
// Menu-driven program to manage the telephone directory
while (true) {
cout << "\nTelephone Directory" << endl;
cout << "1. Add new entry" << endl;
cout << "2. Show telephone number" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
// Add a new entry
cout << "Enter name: ";
cin.ignore(); // To consume the newline character left in the
buffer
getline(cin, name);
cout << "Enter phone number: ";
cin >> phoneNumber;
directory.add(name, phoneNumber);
break;
case 2:
// Show the telephone number for a given name
cout << "Enter name to search: ";
cin.ignore(); // To consume the newline character left in the
buffer
getline(cin, name);
directory.show(name);
break;
case 3:
// Exit the program
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
10. Create a base class named Shape to store a double-type value that
can be used to compare areas. Derive two specific classes, Triangle and
Rectangle, from the base class Shape. Include a member function,
getData, in the base class to initialize the base data members, and
another function, displayArea, to display the area.
#include <iostream>
using namespace std;
public:
// Function to get data for dimensions
void getData(double d1, double d2) {
dimension1 = d1;
dimension2 = d2;
}
int main() {
// Create objects of Triangle and Rectangle
Triangle triangle;
Rectangle rectangle;
// Display areas
triangle.displayArea();
rectangle.displayArea();
return 0;
}
int main() {
int n; // Number of disks
return 0;
}
12. Write Program to implement Stack Operations like PUSH, POP, PEEP,
UPDATE and DISPLAY using class and object.
#include <iostream>
using namespace std;
class Stack {
private:
int* arr; // Array to hold the stack elements
int top; // Index of the top element
int capacity; // Maximum number of elements in the stack
public:
// Constructor to initialize stack
Stack(int size) {
capacity = size;
arr = new int[capacity]; // Dynamic array allocation
top = -1; // Stack is initially empty
}
do {
cout << "\nStack Operations Menu:\n";
cout << "1. PUSH\n";
cout << "2. POP\n";
cout << "3. PEEP\n";
cout << "4. UPDATE\n";
cout << "5. DISPLAY\n";
cout << "6. EXIT\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
value = stack.peep();
if (value != -1) {
cout << "Top element: " << value << endl;
}
break;
case 4:
cout << "Enter new value for top element: ";
cin >> value;
stack.update(value);
break;
case 5:
stack.display();
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 6);
return 0;
}
13. Write Program to convert Infix to Postfix Expression using class and
object.
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
class InfixToPostfix {
private:
stack<char> operatorStack; // Stack to hold operators
public:
// Function to convert infix expression to postfix expression
string convertToPostfix(const string& infix) {
string postfix = "";
for (char token : infix) {
// If the token is an operand, add it to the postfix expression
if (isalnum(token)) {
postfix += token;
}
// If the token is '(', push it to the stack
else if (token == '(') {
operatorStack.push(token);
}
// If the token is ')', pop from the stack to the postfix expression
until '(' is encountered
else if (token == ')') {
while (!operatorStack.empty() && operatorStack.top() != '(') {
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.pop(); // Remove '(' from stack
}
// If the token is an operator
else {
while (!operatorStack.empty() &&
precedence(operatorStack.top()) >= precedence(token)) {
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.push(token); // Push current operator onto the
stack
}
}
int main() {
InfixToPostfix converter; // Create an object of InfixToPostfix class
string infix;
return 0;
}
14. Write Program to convert Infix to Prefix Expression using class and
object.
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
#include <cctype>
class InfixToPrefix {
private:
stack<char> operatorStack; // Stack to hold operators
// Function to determine precedence of operators
int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
}
if (op == '*' || op == '/') {
return 2;
}
if (op == '^') {
return 3;
}
return 0;
}
public:
// Function to convert infix expression to prefix expression
string convertToPrefix(const string& infix) {
string prefix = "";
string reversedInfix = reverse(infix);
int main() {
InfixToPrefix converter; // Create an object of InfixToPrefix class
string infix;
return 0;
}