Seneca BTP 305
Constructors and Copy Assignment Operator
Lab 1We’ll start by creating our constructors and copy assignment operator.
The reason we need a copy assignment operator is because in the main function, the program makes copies of FoodOrder objects.
Since our class uses dynamic memory (a char* pointer), we must perform a deep copy instead of a shallow copy.
A shallow copy would make multiple objects point to the same memory, which can cause crashes or double deletion errors.
Default constructor
Let’s begin with the default constructor. This constructor doesn’t need to perform any setup because our member variables are already initialized using {} in their declarations.
Inside your FoodOrder.h, add the following code inside the class:
class FoodOrder {
char m_customerName[10]{};
char* m_orderDesc{};
double m_price{};
bool m_dailyspecial{};
public:
FoodOrder() {};
};Because this constructor does not need to run any code, it can be defined directly inside the header file using empty curly brackets {}. Normally, we define functions in the .cpp file, but for an empty constructor like this, it’s fine to keep it inline to save space and keep things simple.
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 17 more lessons in Seneca BTP 305.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.