Problem Questions Answers
Problem 1:
#include <iostream>
#include <string>
using namespace std;
// Step 1: Base Class
class Device {
private:
string brand;
public:
// Parameterized constructor for Base Class
Device(string b) {
brand = b;
}
void turnOn() {
cout << brand << " device is now ON." << endl;
}
};
// Step 2: Derived Class inheriting from Device
class Smartphone : public Device {
private:
int storageGB;
public:
// Parameterized constructor passing the brand 'b' up to the Base constructor
Smartphone(string b, int s) : Device(b) {
storageGB = s; // Initialize the child's own data member
}
void displaySpecs() {
cout << "Storage Capacity: " << storageGB << " GB" << endl;
}
};
int main() {
// Step 3: Create a derived object, providing values for both parent and child
classes
Smartphone myPhone("Samsung", 128);
// Calling inherited function from base class
[Link]();
// Calling child class function
[Link]();
return 0;
}
Problem 2:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Account {
private:
int accNum;
double balance;
public:
// Constructor with default parameters
Account() {
accNum = 0;
balance = 0.0l;
}
// Getter functions to read private data
int getAccNum() {
return accNum;
}
double getBalance() {
return balance;
}
// Setter function to modify private data
void setAccNum(int id) {
accNum = id;
}
Void setBalance(int bal){
balance = bal;
}
// Function to print details
void display() {
cout << "Account Number: " << accNum << " | Balance: $" << balance <<
endl;
}
};
int main() {
// Step 5: Create object with data and write to file
Account acc1(5543, 2500.50);
ofstream outFile("[Link]");
// Check if the stream is valid without using built-in functions
if (outFile) {
// Write data separated by a space
outFile << [Link]() << " " << [Link]() << endl;
[Link]();
cout << "Data successfully written to [Link]" << endl;
} else {
cout << "Error: Could not open file for writing!" << endl;
}
// Step 6: Read data from file into a completely separate object
Account acc2; // Starts empty with default values (0, 0.0)
ifstream inFile("[Link]");
// Check if the stream is valid without using built-in functions
if (inFile) {
int id;
double bal;
// Extract data in the exact same order it was written
inFile >> id >> bal;
// Use the setter function to pass file data into our empty object
[Link](id);
[Link](bal);
[Link]();
} else {
cout << "Error: Could not open file for reading!" << endl;
}
// Display the newly populated object to verify success
cout << "\nDisplaying data from the new object (loaded from text file):" <<endl;
[Link]();
return 0;
}
Problem 3:
#include <iostream>
using namespace std;
class ScoreTracker {
private:
int* score; // Pointer as a data member pointing to a single integer
public:
// Step 2: Default Constructor allocates memory for a single int on the heap
ScoreTracker() {
score = new int; // Allocate space for 1 integer
*score = 0; // Initialize the value at that address to 0
}
// Step 3: Setter function updates the heap memory value
void setScore(int s) {
*score = s; // De-reference the pointer to change the actual value
}
// Step 4: Getter function retrieves the heap memory value
int getScore() {
return *score; // De-reference the pointer to return the value
}
// Step 5: Destructor cleans up the single heap allocation automatically
~ScoreTracker() {
delete score; // Delete single item (DO NOT use delete[] here)
score = NULL; // Prevent dangling pointer
cout << "\nDestructor called: Single integer memory cleanly freed from
heap." << endl;
}
};
int main() {
// Step 6: Create the object (this dynamically allocates the integer behind the
scenes)
ScoreTracker tracker;
int userValue = 0;
cout << "Enter the current match score: ";
cin >> userValue;
// Save the input value into the object's dynamic memory
[Link](userValue);
// Retrieve and display the value from the object's dynamic memory
cout << "Stored Score: " << [Link]() << endl;
return 0; // The 'tracker' object dies here, automatically calling the destructor
}