CS304 Assignment No 3. August 2020
CS304 Assignment No 3.
Solution File:
Object studentB is also pointing to memory allocated object studentA for Student class data member name of char * type, now there are two problems with this sort of copying,
Suppose we delete first object studentA for some reason then its destructor will also free memory allocated by it hence memory area containing name “AHMAD” will also be freed and will be given to some other application by operating system, but studentB member name is still pointing to that area so issue of “Dangling Pointer” will arose. [Pointer pointing to incorrect memory location]. If we will try to print the name of object studentB our program will terminate abnormally as it was pointing memory of some other applications. Secondly if for some reason we change name of studentA the value of object studentB will also be changed as it pointing to same memory location.
We resolve these two issues using deep copy.
Benefits of separating interface and implementation:
Consider the example of following complex no. class, this complex no. class two forms of implementations one is new and one is old implementation you can observe that if you have separated interface and implementation then we can easily change implementation without changing interface.
Explicit Default constructor:
If user has given constructor for any class without any arguments or with all arguments with default parameters then it is also default constructor according to definition but it is explicit default constructor.
Now if a base class has only non-default constructor (implicit or explicit), then when we will create object of any class derived from this base class compiler will not be able to call base class constructor as base class has no default constructor ( constructor that can be called without giving any parameters) so compiler will generate error. We can avoid this error by calling base class non-default constructor in derived class constructor initializer list.
Posting Komentar untuk "CS304 Assignment No 3. August 2020"