Examples Codes 2
Example 1
a. Create a Wallet class with the following features:
1. Private attributes:
• owner: Name of the wallet owner.
• balance: Current balance in the wallet.
2. A constructor to initialize wallet details.
3. A destructor to display a message when the wallet object is destroyed.
b. Implement the following member functions:
1. addMoney(amount): Adds money to the wallet if the amount is positive.
2. spendMoney(amount): Deducts money from the wallet if there are sufficient funds.
3. displayWalletInfo(): Displays the wallet owner's name and the current balance.
c. In the main() function:
1. Create a Wallet object with initial values for the owner's name and balance.
2. Perform the following operations:
• Display wallet details.
• Attempt to spend more money than the current balance.
• Add money to the wallet.
• Successfully spend money.
SOLUTION
#include <iostream>
#include <string>
using namespace std;
// class
class Wallet {
private:
string owner;
double balance;
public:
// Constructor
Wallet(string walletOwner, double initialBalance)
: owner(walletOwner), balance(initialBalance) // constructor initializer list for wallet
Attributes (owner and balance)
cout << "Wallet created for " << owner << " with initial balance: $" << balance << endl;
// Destructor
~Wallet() {
cout << "Wallet object for " << owner << " is destroyed.\n";
// Member function to add money to the wallet
void addMoney(double amount) {
if (amount > 0) {
balance += amount; // equivalent to balance = balance + amount;
cout << "Added $" << amount << " to the wallet. New balance: $" << balance << endl;
} else {
cout << "Invalid amount. Cannot add $" << amount << " to the wallet.\n";
// Member function to spend money from the wallet
void spendMoney(double amount) {
if (amount > balance) {
cout << "Insufficient funds! Cannot spend $" << amount << ". Current balance: $" << balance <<
endl;
} else {
balance -= amount;
cout << "Spent $" << amount << " from the wallet. Remaining balance: $" << balance << endl;
// Member function to display wallet details
void displayWalletInfo() const {
cout << "Owner: " << owner << ", Balance: $" << balance << endl;
};
int main() {
// Create a Wallet object
Wallet myWallet("John", 100.0);
// Display wallet details
cout << "\nWallet Details:\n";
myWallet.displayWalletInfo();
// Attempt to spend more money than available
cout << "\nAttempt to spend $150:\n";
myWallet.spendMoney(150.0);
// Add money to the wallet
cout << "\nAdding $50:\n";
myWallet.addMoney(50.0);
// Successfully spend money
cout << "\nSpending $80:\n";
myWallet.spendMoney(80.0);
return 0;
Example 2
Write a C++ program to model a Cube class and demonstrate the use of constructors and a destructor.
a. Create a Cube class with a private attribute: side (length of the side of the cube).
b. Implement two constructors:
1. A default constructor to initialize the side length to 1.0.
2. A parameterized constructor to initialize the side length with a given value.
c. Implement a destructor to display a message when a Cube object is destroyed.
d. Add a member function volume() to calculate and return the cube's volume.
e. In main(), create two Cube objects using the different constructors and display their volumes.
SOLUTION
#include <iostream>
using namespace std;
class Cube
private:
double side; // Private attribute: side length of the cube
public:
// Default constructor
Cube() {
side = 1.0; // Initialize side length to 1.0
cout << "Default constructor called. Side initialized to " << side << endl;
}
// Parameterized constructor
Cube(double sideLength)
side = sideLength; // Initialize side length with given value
cout << "Parameterized constructor called. Side initialized to " << side << endl;
// Destructor
~Cube() {
cout << "Destructor called for cube with side " << side << endl;
// Member function to calculate and return the volume
double volume() const {
return side * side * side;
};
int main() {
// Create a Cube object using the default constructor
Cube cube1;
cout << "Volume of cube1: " << cube1.volume() << endl;
// Create a Cube object using the parameterized constructor
Cube cube2(3.5);
cout << "Volume of cube2: " << cube2.volume() << endl;
return 0;