In: Computer Science
Describe the difference between making a class a
member of another class (object composition) and making a class a
friend of another class.
Explain why memberwise assignment can cause problems
with a class that contains a pointer member.
Explain why the parameter of a copy constructor must
be a reference.
1)
Describe the difference between making a class a member of another
class (object composition) and making a class a friend of another
class.
Ans:-
Object composition is the way to combine multiple small class
objects into large complex class objects.
For example, Student class composed of a Name, Age, Sex, Address
etc. each of these would be classes or types.
class Name{
string firstname, lastname;
}
class Address{
string street, postcode;
}
class Student{
Name name;
Address address;
int age;
int sex;
}
we use composition for writing data structures like LinkedList,
Tree, Graph, STL etc.
the composite object becomes the property of that class which
cannot be accessed easily if that class wants to.
that object is owned by that class where all the properties of the
owned object is not accessible by that class which owns it. the
composite object has abstraction and encapsulation which same
applies to the class that owns it.
the composite object cannot access any members of the class that
owns it.
Friend classes are completely different from composition. friend
class has access to all the members of another class even private
members. friend classes breaks the rule of abstraction and
encapsulation but allows the ownership to another class or giving
access. object composition defines the ownership and friend classes
breaks all ownerships.
Following example allow Professor to access all information of
student even private.
class Professor;
class Student{
int rollno;
string name;
friend class Professor;
}
class Professor{
void run(){
cout<<name;
}
}
2)
Explain why memberwise assignment can cause problems with a class
that contains a pointer member.
Ans:-
if the member is a pointer then some memory must will have
allocated to it dynamically or will have assigned address of
another object. In an assignment, no new object is created, the
value of an existing object is simply changed.
if memory is allocated to pointer member then assignment will not
copy the values of the members of pointer, instead it will just
assign the addres to it.
For example:
class Test{
int x, y;
}
class ABC{
ABC(Test* t){
mt = new Test;
mt = t; // problem occurs here
}
private:
Test* mt;
}
once memory is allocated to mt, the memberwise assignment cannot
copy values from object t to mt, instead it is just assigning
address contain in t to mt which will be invalid when constructor
returns. so now mt contains an invalid memory location address.
also the dynamically allocated memory is wasted because its never
been used.
Instead do the following,
class Test{
int x, y;
}
class ABC{
ABC(Test* t){
mt = new Test;
mt->x = t->x;
mt->y = t->y;
}
~ABC(){ delete mt; }
private:
Test* mt;
}
3)
Explain why the parameter of a copy constructor must be a
reference.
Ans:-
The question should be "Explain why the parameter of a copy
constructor must be a constant reference."
references are the inbuilt pointers managed by the compiler
internally to overcome pointer spaghetti everywhere in the program
by applying some rules on it such as they must not be null or must
not be re-reference to.
As you can see in Que.No 2, it is so overhead to write pointers and
passing addresses to constructors.
instead compilers inbult support copy constructors, assignment
operators where reference must be passed because it guarantee that
is is not null, it is referencing to some other object unlike
pointer which can be null and can point to invalid location.
copy constructor parameters must be constant because the passed
object to constructor will remain unchanged, even if someone try to
modify it, compiler will generate an error. so other objects are
secure from modification.
following are two examples why parameters of copy constructor must be a reference.
1) using pointer
class ABC{
ABC(const ABC* a){ // dont know where a pointes to(valid/invalid
object address location)
x = a->x; // if a points to invalid, then
this raises segmentation fault
y = a->y;
}
private:
int x, y;
}
2) using reference
class ABC{
ABC(const ABC& a){ // guarantees a refernce to another
object
x = a.x; // no problem, references cannot
references to null
y = a.y;
}
private:
int x, y;
}