C++ Notes Final
C++ Notes Final
#include <iostream>
#include <string>
class BankAccount {
private:
public:
: accountHolderName(holderName), accountNumber(accNumber),
balance(initialBalance) {}
string getAccountHolderName() {
return accountHolderName;
}
string getAccountNumber() {
return accountNumber;
double getBalance() {
return balance;
accountHolderName = holderName;
if (amount > 0) {
balance += amount;
balance -= amount;
} else {
};
int main() {
// Perform a deposit
account.deposit(500.0);
// Perform a withdrawal
account.withdraw(300.0);
account.withdraw(1500.0);
return 0;
}Explanation
1. Private Members:
2. Constructor:
3. Getter Functions:
4. Setter Function:
5. Member Functions:
6. Main Function:
Encapsulation Benefits
Data Hiding: The private members are not accessible directly from outside
the class, ensuring that the internal state of the object can only be modified
through the public methods.
Question:
You are given the following code for a BankAccount class, which demonstrates
the principle of encapsulation. The BankAccount class contains private data
members for the account holder's name, account number, and balance. It
provides public methods to deposit and withdraw money, as well as to retrieve
the account details. The main function creates BankAccount objects for four
different people, displays their initial account details, and allows the user to
perform deposit and withdrawal transactions for each account.
#include <iostream>
#include <string>
private:
string accountHolderName;
string accountNumber;
double balance;
public:
: accountHolderName(holderName), accountNumber(accNumber),
balance(initialBalance) {}
string getAccountHolderName() {
return accountHolderName;
string getAccountNumber() {
return accountNumber;
}
double getBalance() {
return balance;
if (amount > 0) {
balance += amount;
cout << "Deposited: $" << amount << " into account: " << accountNumber
<< endl;
} else {
balance -= amount;
cout << "Withdrawn: $" << amount << " from account: " << accountNumber
<< endl;
} else {
};
double amount;
account.deposit(amount);
account.withdraw(amount);
int main() {
// Display initial details and handle user input for each account
showAccountDetails(account1);
showAccountDetails(account2);
showAccountDetails(account3);
showAccountDetails(account4);
handleUserInput(account1);
handleUserInput(account2);
handleUserInput(account3);
handleUserInput(account4);
return 0;
Explanation
1. Class Definition:
2. Helper Functions:
3. Main Function:
Interacts with the user to deposit and withdraw money for each
account using handleUserInput.
Key Points
Encapsulation: Each BankAccount object encapsulates its data,
ensuring that the balance and account details are only accessible through
the public methods.
Interactive User Input: The program interacts with the user to perform
transactions on each account, demonstrating the encapsulated behavior of
each BankAccount object independently.
This example shows how encapsulation works with multiple objects, each
maintaining its own state and behavior, and how user interaction can be handled
for each object individually.
Write a C++ program to create a class called Car that has private member
variables for company, model, and year. Implement member functions to get and
set these variables.
Sample Solution:
C ++ Code:
#include <iostream> // Including the necessary header for input/output
stream
#include <string> // Including the necessary header for string
operations
public:
// Constructor to initialize Car object with provided values
Car(const string & comp, const string & mdl, int yr):
company(comp), model(mdl), year(yr) {}
int main() {
// Create a car object with initial values
Car car("AUDI", "A6", 2023);
// Get and display the updated car details using getter functions
cout << "\nUpdated Company: " << car.getCompany() << endl; // Output
the updated car's company name
cout << "Updated Model: " << car.getModel() << endl; // Output the
updated car's model name
cout << "Updated Year: " << car.getYear() << endl; // Output the
updated car's year
The "Car" class represents a car with private member variables company, model,
and year. The constructor initializes these variables.
In the main() function, a Car object is created with the company as "AUDI",
model as "A6", and year as 2023. The getter functions (getCompany(),
getModel(), getYear()) are then called to retrieve and display the car details.
Next, the setter functions (setCompany(), setModel(), setYear()) are used to set
new values for the car's company, model, and year. Again, the getter functions
are called to retrieve and display updated car details.
Flowchart:
Write a C++ program to get the current date and time.
#include <iostream>
#include <ctime> // Including the C Standard Library header for time functions
int main()
// current date and time in current system using time_t data type
time_t current_dt = time(0); // Get the current system time in seconds since epoch (Jan 1, 1970)
char* result = ctime(¤t_dt); // Convert the time_t value to a string representing local time
cout << "The current date and time is:\n" << result << endl;
Sample Output:
Flowchart:
Sample Solution-2:
C++ Code:
#include <iostream>
#include <ctime>
int main() {
time_t crtime; // Declare a variable to hold the current time as seconds since epoch
time(&crtime); // Get the current system time in seconds since epoch and store it in crtime
time_info = localtime(&crtime); // Convert the time_t value into a local time representation using
localtime function
// Format the time_info into a string according to the specified format "%d-%m-%Y %H:%M:%S"
Sample Output:
15-03-2022 12:15:31
Flowchart:
Sample Solution-2:
C++ Code:
#include <iostream>
#include <ctime>
int main() {
time_t crtime; // Declare a variable to hold the current time as seconds since epoch
time(&crtime); // Get the current system time in seconds since epoch and store it in crtime
time_info = localtime(&crtime); // Convert the time_t value into a local time representation using
localtime function
// Format the time_info into a string according to the specified format "%d-%m-%Y %H:%M:%S"
return 0;
Sample Output:
15-03-2022 12:15:31
Flowchart:
Sample Solution-1:
C++ Code:
#include <iostream>
int main()
// current date and time in current system using time_t data type
cout << "The current date and time is:\n" << result << endl;
Sample Output:
The current date and time is:
Tue Mar 15 14:28:05 2022
Flowchart:
Sample Solution-2:
C++ Code:
#include <iostream>
#include <ctime>
int main() {
return 0;
Copy
Sample Output:
15-03-2022 12:15:31
Flowchart: