0% found this document useful (0 votes)
19 views8 pages

C

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)
19 views8 pages

C

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/ 8

Q1.

#include <iostream>

#include <string>

// Function to greet based on provided name and greeting (with default value)

void greet(const std::string& name, const std::string& greeting = "Hello") {

std::cout << greeting << ", " << name << std::endl;

int main() {

// Example usage

greet("Alice"); // Prints: Hello, Alice

greet("Bob", "Hi"); // Prints: Hi, Bob

return 0;

Q2.

#include <iostream>

#include <string>

#include <vector>

class Student {

private:

std::string name;

int age;

std::vector<double> grades;

public:

// Constructor: Initialize name and age

Student(const std::string& studentName, int studentAge) : name(studentName), age(studentAge) {

std::cout << "Creating student: " << name << std::endl;

// Destructor

~Student() {
std::cout << "Destroying student: " << name << std::endl;

// Method to add a grade

void addGrade(double grade) {

grades.push_back(grade);

// Method to calculate average grade

double calculateAverageGrade() const {

if (grades.empty()) {

return 0.0; // No grades yet

double sum = 0.0;

for (double grade : grades) {

sum += grade;

return sum / grades.size();

// Method to print student details

void printDetails() const {

std::cout << "Student Details:" << std::endl;

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

std::cout << "Age: " << age << std::endl;

std::cout << "Grades: ";

for (double grade : grades) {

std::cout << grade << " ";

std::cout << std::endl;

};

int main() {

// Create a student
Student alice("Alice", 20);

// Add some grades

alice.addGrade(85.5);

alice.addGrade(92.0);

alice.addGrade(78.3);

// Print student details

alice.printDetails();

// Calculate and print average grade

double avgGrade = alice.calculateAverageGrade();

std::cout << "Average Grade: " << avgGrade << std::endl;

return 0;

Q4.

#include <iostream>

#include <vector>

class Matrix {

private:

std::vector<std::vector<double>> data;

int rows;

int cols;

public:

// Constructor: Create an empty matrix with specified dimensions

Matrix(int numRows, int numCols) : rows(numRows), cols(numCols) {

data.resize(rows, std::vector<double>(cols, 0.0));

// Copy constructor

Matrix(const Matrix& other) : rows(other.rows), cols(other.cols), data(other.data) {}

// Method to set a specific element in the matrix

void setElement(int row, int col, double value) {


if (row >= 0 && row < rows && col >= 0 && col < cols) {

data[row][col] = value;

} else {

std::cerr << "Invalid indices." << std::endl;

// Overload + operator for matrix addition

Matrix operator+(const Matrix& other) const {

if (rows != other.rows || cols != other.cols) {

std::cerr << "Matrix dimensions must match for addition." << std::endl;

return *this; // Return the original matrix

Matrix result(rows, cols);

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

for (int j = 0; j < cols; ++j) {

result.data[i][j] = data[i][j] + other.data[i][j];

return result;

// Method to display the matrix

void display() const {

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

for (int j = 0; j < cols; ++j) {

std::cout << data[i][j] << " ";

std::cout << std::endl;

};
int main() {

// Example usage

Matrix A(2, 3);

A.setElement(0, 0, 1.0);

A.setElement(0, 1, 2.0);

A.setElement(0, 2, 3.0);

A.setElement(1, 0, 4.0);

A.setElement(1, 1, 5.0);

A.setElement(1, 2, 6.0);

Matrix B = A; // Copy constructor

Matrix C = A + B; // Matrix addition

std::cout << "Matrix A:" << std::endl;

A.display();

std::cout << "Matrix B:" << std::endl;

B.display();

std::cout << "Matrix C (A + B):" << std::endl;

C.display();

return 0;

Q3.

#include <iostream>

#include <vector>

#include <string>

using namespace std;

// Class for managing individual bank accounts

class Account {

private:

string accountNumber;

double balance;

public:
// Constructor to initialize account

Account(string accNum, double initialBalance)

: accountNumber(accNum), balance(initialBalance) {}

// Method to deposit money into the account

void deposit(double amount) {

if (amount > 0) {

balance += amount;

cout << "Deposited: $" << amount << endl;

} else {

cout << "Deposit amount must be positive." << endl;

// Method to withdraw money from the account

void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

cout << "Withdrew: $" << amount << endl;

} else {

cout << "Insufficient funds or invalid amount." << endl;

// Method to get the balance of the account

double getBalance() const {

return balance;

// Method to get the account number

string getAccountNumber() const {

return accountNumber;

};

// Class for managing customer details


class Customer {

private:

string name;

string address;

vector<Account> accounts;

public:

// Constructor to initialize customer details

Customer(string custName, string custAddress)

: name(custName), address(custAddress) {}

// Method to add an account to the customer

void addAccount(const Account& account) {

accounts.push_back(account);

// Method to display customer information

void displayCustomerInfo() const {

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

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

cout << "Accounts:" << endl;

for (const auto& account : accounts) {

cout << "Account Number: " << account.getAccountNumber()

<< ", Balance: $" << account.getBalance() << endl;

};

// Class for managing the bank with multiple customers

class Bank {

private:

vector<Customer> customers;

public:

// Method to add a customer to the bank

void addCustomer(const Customer& customer) {


customers.push_back(customer);

// Method to display all customers

void displayAllCustomers() const {

for (const auto& customer : customers) {

customer.displayCustomerInfo();

cout << "------------------------" << endl;

};

int main() {

// Create some accounts

Account acc1("123456789", 1000.00);

Account acc2("987654321", 500.00);

// Create a customer and add accounts to the customer

Customer cust1("John Doe", "123 Elm Street");

cust1.addAccount(acc1);

cust1.addAccount(acc2);

// Create the bank and add the customer

Bank myBank;

myBank.addCustomer(cust1);

// Perform some transactions

acc1.deposit(200);

acc1.withdraw(150);

acc2.deposit(100);

acc2.withdraw(50);

// Display all customer information

myBank.displayAllCustomers();

return 0;

You might also like