0
0
C++programming~20 mins

new operator in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the new Operator in C++
📖 Scenario: You are creating a simple program to store the ages of three friends using dynamic memory allocation in C++. This helps you understand how to use the new operator to create variables on the heap.
🎯 Goal: Build a program that dynamically allocates memory for three integer variables representing ages, assigns values to them, and then prints these ages.
📋 What You'll Learn
Create three integer pointers using the new operator
Assign the ages 25, 30, and 35 to these pointers
Print the values stored in the dynamically allocated memory
Use delete to free the allocated memory
💡 Why This Matters
🌍 Real World
Dynamic memory allocation is used in programs where the amount of data is not known before running the program, such as storing user input or handling large datasets.
💼 Career
Understanding how to manage memory manually is important for systems programming, game development, and performance-critical applications.
Progress0 / 4 steps
1
Create three integer pointers using new
Create three integer pointers called age1, age2, and age3 using the new operator to allocate memory for each.
C++
Need a hint?

Use int* age1 = new int; to create a pointer that points to a new integer.

2
Assign values to the dynamically allocated integers
Assign the value 25 to *age1, 30 to *age2, and 35 to *age3.
C++
Need a hint?

Use the dereference operator * to assign values to the memory locations.

3
Print the values stored in the pointers
Use std::cout to print the values pointed to by age1, age2, and age3 on separate lines.
C++
Need a hint?

Use std::cout << *age1 << std::endl; to print the value pointed to by age1.

4
Free the allocated memory
Use delete to free the memory allocated for age1, age2, and age3.
C++
Need a hint?

Use delete age1; to free the memory allocated for age1.