337 C++ (22) Flashcards
What is the following?
`
int a = 40 //???
int b;
b=a; //???
`
`
int a = 40 //initialization
int b;
b=a; //assignment
`
Is the following assignment or initialization? Why or why not?
`
Aclass obj1;
Aclassobj2 = obj1
`
This is initialization. You may initialize the object of a class by an existing object of the same class.
Every data member of instance obj1 will be copied into instance obj2. This is called member-wise copying.
The following lines of code are labeled 1, 2, and 3. Determine whether they call a constructor or not.
`
MyString s1(“World);
MyString s2=s1;
`
MyString s1(“World); //call to constructor
MyString s2=s1; //Not a call to constructor.
What is the general format forthe declaration of a copy constructor where X is the class name.
`
X(const X& source);
`
What is the format for copying an object by assignment? (Using the assignment operator)
X &operator = (const X&rhs);
Example
MyString &operator = (MyString& s);
MyString& MyString::operator =(const MyString& s) { if (this != &s) { delete[] storageM; // Deallocate memory of the current object lengthM = s.lengthM; // Update the length storageM = new char[lengthM + 1]; // Allocate new memory assert(storageM != nullptr); // Ensure memory allocation is successful strcpy(storageM, s.storageM); // Copy the content from the source to the current object } return *this; // Return a reference to the current object }
In the following assignment operator for copying a string, fill in the blanks
It has members storageM, and lengthM
MyString& MyString::operator =(MyString& s) { if (this != &s) { delete[] \_\_\_\_\_\_\_\_; // Deallocate memory of the current object \_\_\_\_\_\_\_ = \_\_\_\_\_\_\_\_\_; // Update the length storageM = new char[\_\_\_\_\_\_\_\_ + 1]; // Allocate new memory assert(storageM != NULL); // Ensure memory allocation is successful strcpy(\_\_\_\_\_\_\_, \_\_\_\_\_\_\_\_); // Copy the content from the source to the current object } return \_\_\_\_\_\_\_; // Return a reference to the current object }
MyString& MyString::operator =(MyString& s) { if (this != &s) { delete[] storageM; // Deallocate memory of the current object lengthM = s.lengthM; // Update the length storageM = new char[lengthM + 1]; // Allocate new memory assert(storageM != NULL); // Ensure memory allocation is successful strcpy(storageM, s.storageM); // Copy the content from the source to the current object } return *this; // Return a reference to the current object }
In the assignment operator why do we want to avoid self copying? Let’s say we do s1 = s1; What line must we include to avoid any issues?
When we delete the old space, this will cause an issue for the new object. Therefore to avoid this issue we MUST include if (this != &s).
What header file must be included in order to use the assert function?
You must #include <cassert>
True or false, s1 = s3 calls the copy constructor
False, this is an assignment operation
Why does a copy constructor not delete storageM but the overloaded Assignment Operator does?
The copy constructor is responsible for creating a new object and initializing it with the contents of an existing object. It is invoked when a new object is being created as a copy of an existing object. Since the new object is being constructed from scratch, there is no need to delete any existing memory because there is no existing memory to delete.
The overloaded assignment operator (operator=) is responsible for assigning the value of one existing object to another. When overwriting the value of an existing object, it’s crucial to first deallocate any existing memory in the destination object to avoid memory leaks. Therefore, the overloaded assignment operator often includes a delete operation to release the existing resources.
True or false MyString s2 = s1; is a call to a copy constructor.
True
What is the return value for a copy constructor?
It has no return value.
For the following copy constructor, fill in the blanks:
MyString::MyString(const MyString& s) : lengthM(s.lengthM) { storageM = new char [\_\_\_\_\_\_\_\_+1]; assert (\_\_\_\_\_\_\_\_\_ !=0); strcpy(\_\_\_\_\_\_\_\_\_, \_\_\_\_\_\_\_\_\_\_); }
\MyString::MyString(const MyString& s) : lengthM(s.lengthM) { storageM = new char [lengthM+1]; assert (storageM !=0); strcpy(storageM, s.storageM); }
Does a destructor return any value? Does it receive any parameter?
No a destructor does not return any value, and it does not receive any parameter.
What is the general format for a destructor?
MyString::~MyString(){
delete[] storageM;
}
What is object aggregation?
Object aggregation is when a class can contain other classes as their data member.
Example:
Person(int d, int m, int y, char* na, int a): birthday(d, m, y), age(a)
{
strcpy(name, na);
}
What does the function prototype look for an assignment operator in C++?
It would look like
MyClass& operator=(const MyClass& rhs);
The actual implementation would look like:
MyClass& MyClass::operator=(const MyClass& rhs) {
// Implementation of the assignment operator
// …
return *this;
}