Make Answer Easy and Long
Make Answer Easy and Long
Key Uses:
b. Access global variables: If a local variable has the same name as a global
variable, use ::variable to access the global one.
c. Specify namespaces: For example, std::cout tells the compiler to use the cout
function from the std library.
Example:
class Pizza {
public:
void bake(); // Declaration
};
Stack: Fixed size (like a small closet). Used for local variables.
Heap: Flexible size (like a warehouse). Used for dynamic allocation.
Large data: Storing a 10,000-element array that won’t fit on the stack.
Unknown size: Creating an array where the size is decided by user input.
Long lifespan: Objects that need to exist even after a function ends (e.g., a game
character that stays alive between levels).
Example:
Risk: Forgetting to use delete causes memory leaks (like leaving lights on in an unused
room).
Friend Class: All functions in the friend class can access private members.
Example:
class Diary {
private:
string secret = "I love coding!";
friend class BestFriend; // BestFriend can read the secret
};
class BestFriend {
public:
void readSecret(Diary &d) {
cout << d.secret; // Access allowed!
}
};
Caution: Overusing friend breaks encapsulation (the OOP rule of hiding data).
4. Discuss dynamic initialization of objects
Dynamic initialization means setting up an object’s values at runtime (when the
program runs) instead of compile time (when the code is built). Constructors are used to
achieve this.
Example:
class BankAccount {
int balance;
public:
BankAccount(int initial) {
balance = initial; // Balance set at runtime
}
};
int main() {
int userInput;
cin >> userInput; // User decides the balance
BankAccount account(userInput); // Dynamic initialization
}
This allows objects to adapt to real-time data (e.g., user inputs, file contents).
Example:
class Math {
public:
inline int square(int x) {
return x * x;
}
};
int main() {
Math m;
cout << m.square(5); // Compiler replaces this with "5 * 5"
}
Trade-off:
Example:
class Burger {
public:
// Constructor Overloading
Burger() { } // Default: plain burger
Burger(string topping) { } // Burger with cheese/chili
// Function Overloading
void serve() { cout << "Here's your burger!"; }
void serve(string side) { cout << "Burger with " << side; }
};
Why use it? Makes code cleaner—no need for names like serveBurger() or
serveBurgerWithFries().
Example:
class Student {
public:
static int count; // Tracks total students
Student() { count++; }
};
int main() {
Student s1, s2;
cout << s1.count; // Output: 2 (shared value)
}
Types of Constructors:
Example:
class Phone {
public:
string model;
Phone() { model = "Unknown"; } // Default
Phone(string m) { model = m; } // Parameterized
};
9. Purpose of destructors
A destructor cleans up resources when an object is destroyed (e.g., closing files, freeing
memory). It’s like a cleanup crew after a concert.
Example:
class File {
FILE *file;
public:
File() { file = fopen("data.txt", "r"); }
~File() { fclose(file); } // Destructor closes the file
};
If no destructor: The compiler creates a default one, but it won’t handle dynamic
memory (e.g., new/delete).
class Song {
string title;
public:
Song(string t) { title = t; } // Regular constructor
Song(const Song &s) { title = s.title; } // Copy constructor
};
int main() {
Song s1("Happy");
Song s2 = s1; // Uses copy constructor
}
Example:
class Dog {
private:
string name;
public:
void bark() { cout << "Woof!"; }
void setName(string n) { name = n; }
};
int main() {
Dog myDog;
myDog.setName("Buddy");
myDog.bark();
}
Example:
class Cookie {
public:
string flavor;
Cookie(string f) { flavor = f; }
};