الشرح التفصيلي لجميع شرائح المحاضرة (Slide-by-Slide) مدعوم بشرح الأكواد سطراً بسطر (Line-by-Line Code Explanation)
1. Which of the following is the correct syntax to dynamically allocate a single integer and initialize it with the value 10?
2. What happens if you forget to use the `delete` operator on memory allocated with `new`?
1. If `ptr` is a pointer to an object of class `Car` which has a method `startEngine()`, which of the following is the correct and most standard way to call it?
2. Inside a class method, what exactly does the `this` pointer represent?
new = حجز: بيحجز مساحة في الـ Heap ويرجع عنوانها في Pointer. لو كان Object بيشغل الـ Constructor أوتوماتيك.delete = تحرير: بيمسح المساحة. لو نسيتها = Memory Leak!new int[size] للحجز. delete[] arr للمسح (لازم الأقواس!).->): للوصول لدوال Object عن طريق Pointer: ptr->display().this Pointer: بوينتر مخفي بيشاور على الكائن الحالي. أهم استخدام: حل تعارض الأسماء.delete، خلي الـ Pointer = NULL عشان تتفادى Dangling Pointer.Question: Explain Dynamic Memory Allocation in C++ using `new` and `delete`. Discuss Stack vs Heap, dynamic objects/arrays, the Arrow Operator, and the `this` pointer.
Q1:
Q2:
Q1: Dynamically allocate a single integer and initialize it to 42.
Q2: Dynamically allocate an array of 10 integers.
Q3: Delete the dynamic array from Q2 correctly.
Q4: Create a dynamic object of class Student using a parameterized constructor with id=5.
a) What is the output?
b) Why is this->length used instead of just length = length?
c) What happens if we remove the line delete r;?