V.Vidhya Logo

V.Vidhya

Access Functions, Constructor & Destructor (OOP)


Q1.

A social media platform wants to manage user text posts efficiently.

Create a program that:

Instructions
  • Create a class for TextPost to store:

    • Author (e.g., “Radha Mohan”)
    • Timestamp (e.g., “2025-06-05 14:30”)
    • Content (e.g., Hello, world! Excited to share my first text post.”)
    • Mood (e.g., “Excitement”)
    • Likes (initially 0)
  • Implement member functions to:

    • Display all post details (author, timestamp, likes, content, mood).
    • Set or update the content.
    • Increment likes.
    • Decrement likes (if likes > 0).
  • When a post object is destroyed, print this as a notification:

    TextPost by <author> at <timestamp> is being deleted. Final likes: <likes>

Expected Output
// Post example 1 (valid):
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 2
Content: Hello, world! Excited to share my first text post.
Mood: Happy

// Updating the content to "Hello, World! Excited to share my first text post here."
Post content updated successfully.

// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 2
Content: Hello, World! Excited to share my first text post here.
Mood: Happy

// Decrementing likes:

// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 1
Content: Hello, World! Excited to share my first text post here.
Mood: Happy

// Incrementing likes multiple times:

// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 10
Content: Hello, World! Excited to share my first text post here.
Mood: Happy

// Post example 2 (empty author & timestamp):
Post:
Author: UNKNOWN
Timestamp: UNKNOWN
Likes: 0
Content: Guess, who am I!
Mood: 

// When the program ends:
Post by radha_mohan at 2025-06-05 14:30 is being deleted. Final likes: 2
Post by UNKNOWN at UNKNOWN is being deleted. Final likes: 0

Q2.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q3.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q4.

A Smart Thermostat is installed in a home to maintain a comfortable and safe room temperature.

Expected Output
// --- Initializing Thermostat with 18°C ---
Current Temperature: 18°C
Status: Heating Mode ON (Room is too cold)
Energy-Saving Mode: OFF

// --- Attempting to Set Temperature to 32°C ---
Warning: Temperature too high! Setting to maximum 30°C.

Current Temperature: 30°C
Status: Cooling Mode ON (Room is too warm)
Energy-Saving Mode: OFF

// --- Attempting to Set Temperature to 22°C ---
Temperature set to 22°C.

Current Temperature: 22°C
Status: Temperature is comfortable. No heating or cooling needed.
Energy-Saving Mode: ON

// --- Attempting to Set Temperature to 10°C ---
Warning: Temperature too low! Setting to minimum 16°C.

Current Temperature: 16°C
Status: Heating Mode ON (Room is too cold)
Energy-Saving Mode: OFF

Q5.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q6.

A Vending Machine allows users to insert money, select a product, and receive change. However, if a user inserts money but does not select a product, the machine should automatically return the money when the session ends (e.g., due to inactivity or power failure). This ensures that no money remains stuck in the machine.

Instructions

Create a VendingMachine class that allows users to:

  • Insert money into the machine.
  • Select a product (if enough money is inserted).
  • Dispense the product and return any change.
  • Automatically return the money if the user does not select a product before exiting or if the machine shuts down unexpectedly.
Expected Output
// case 1: Successful Purchase
Inserted ₹50 into the vending machine.
Selected: Chips (₹30)
Product dispensed. Change returned: ₹20

// case 2: User exits without selecting a product
Inserted ₹50 into the vending machine.
User exited without selecting a product.
Money returned: ₹50

// case 3: Unexpected Shutdown
Inserted ₹100 into the vending machine.
Unexpected shutdown detected! Returning ₹100 to the user.
System exited.

Q7.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q8.

A text editor should ensure that users do not lose their progress, whether they manually save or forget to do so. The system should always create a backup in case of an unexpected shutdown.

Instructions

Design a TextEditor class that manages a document’s lifecycle. The system should:

  • Display a message when a new document is created.
  • Allow the user to write and edit the document.
  • Provide an option to manually exit the editor.
  • When exiting, prompt the user to save manually or not.
    • If the user chooses to save, save the document normally.
    • If the user chooses not to save, still create a backup save for safety.
  • If the program closes unexpectedly, the destructor should automatically perform a backup save to prevent data loss.
Expected Output
// case 1: User chooses to manually save
Document 'ProjectNotes.txt' created.
Editing document...
User exits the editor.
Do you want to save manually? (y/n): y
Document saved successfully!
Backup save created.
Closing the text editor...
System exited.

// case 2: User chooses not to save
Document 'ProjectNotes.txt' created.
Editing document...
User exits the editor.
Do you want to save manually? (y/n): n
Backup save created.
Closing the text editor...
System exited.

// case 3: Unexpected shutdown
Document 'ProjectNotes.txt' created.
Editing document...
Unexpected shutdown detected!
Auto-saving unsaved changes...
Backup save created.
System exited.

Q9.

A Smart Car Energy Management System is designed to optimize battery usage and adjust driving modes for efficient performance.

Expected Output
// --- Initializing Smart Car with 50% Battery ---
Drive Mode: Normal Mode (Balanced)
Battery Level: 50%
Car Status: Car is running smoothly.

// --- Attempting to switch to Sport Mode ---
Error: Sport Mode requires at least 50% battery to activate.

// --- Driving 10 km in Normal Mode ---
Drove 10 km. Battery consumed: 30%.
Drive Mode: Eco Mode (Energy Saving)

Drive Mode: Eco Mode (Energy Saving)
Battery Level: 20%
Car Status: Low battery! Recharge soon.

// --- Boosting Battery by 40% ---
Boosted battery by 40%. Current level: 60%.
Drive Mode: Normal Mode (Balanced)

Drive Mode: Normal Mode (Balanced)
Battery Level: 60%
Car Status: Car is running smoothly.

// --- Driving 30 km in Normal Mode ---
Drove 30 km. Battery consumed: 90%.
Drive Mode: Eco Mode (Energy Saving)

Drive Mode: Eco Mode (Energy Saving)
Battery Level: 15%
Car Status: Low battery! Recharge soon.

// --- Attempting to switch to Sport Mode ---
Error: Sport Mode requires at least 50% battery to activate.

// --- Attempting to override to Sport Mode while in Eco Mode ---
Error: Cannot switch to Sport Mode. Battery is too low.
Drive Mode: Normal Mode (Balanced)
Battery Level: 60%
Car Status: Car is running smoothly.

Q10.

A bank wants to manage customer accounts efficiently.

Create a program that:

Instructions
  • Create a class for BankAccount to store:
    • Account Holder’s Name (e.g., Ramesh Panipuriwala)
    • Account Number (e.g., 78563249)
    • Balance (e.g., rs.50000)
  • Implement member functions to:
    • Display the account details.
    • Deposit money into the account (ignore if amount < 0).
    • Withdraw money (if there is enough balance, apply the transaction fees according to the rules).
    • Getter function to return the balance.
  • In main(), demonstrate:
    • Creating an object of BankAccount class, setting valid relevant details.
    • Deposite some money, then withdraw some money multiple times (with and without transaction fees), and display the account details after each transaction.
    • Use the access function to check the current balance.
    • Try to withdraw more money than the current balance to verify that the withdrawal is blocked.
    • Finally, create an object of BankAccount class with invalid inputs (e.g., empty name, negative balance, or account number).
Expected Output
// Creating a new account (Valid)
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.50000.00

// After depositing rs.15000:
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.65000.00

// After withdrawing rs.2500 (no fee):
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.62500.00

// After withdrawing rs.12500:
// Checking the balance:
Balance: rs.49981.25
// Extra transaction fee of rs.18.75 (0.15% of rs.12500) applied for amount > 10000:

// After withdrawing rs.23000:
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.26923.75
// Extra transaction fee of rs.57.5 (0.25% of rs.23000) applied for amount > 20000:

// Attempting to withdraw rs.30000 (insufficient balance):
Insufficient balance! Withdrawal failed.

// Creating a new account (Invalid)
Account Details:
Name: UNKNOWN
Account Number: 00000000
Balance: rs.0.00
// Setting default values for invalid account details like empty name, negative balance etc.:

Prev Post
Classes, Member Functions & Access Specifiers (OOP)
Next Post
Object Relationships & Inheritance (OOP)