Question

In: Computer Science

Describe the difference between making a class a member of another class (object composition) and making...

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.


Solutions

Expert Solution

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;
}


Related Solutions

1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
Difference between composition and properties of concrete and asphalt mixes
Difference between composition and properties of concrete and asphalt mixes
1. when you create a class by making it inherit from another class, the new class...
1. when you create a class by making it inherit from another class, the new class automatically, contains the data fields and _____ of the original class a. fonts b. methods c. class names d. arrays 2. if a programming language does not support ________, the language is not considered object oriented a. syntax b. applets c. loops d. polymorphism 3. when you create a class and do not provide a ______, java automatically supplies you with a default one...
Describe the 8 steps in decision making process. Is there a difference between wrong decisions and...
Describe the 8 steps in decision making process. Is there a difference between wrong decisions and bad decisions? How do you think managers can improve decision making skills?
Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing)...
Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class to model books and the author that wrote it. Note the relationship that a book has an author. At this point, you should put all of your classes and other code in the same file. Don't worry about separating it out into separate modules. Instructions Write...
Describe the difference between equal-frequency and equal-interval (or -size) class intervals.
Describe the difference between equal-frequency and equal-interval (or -size) class intervals.
(3) Briefly (in just a few words), describe the difference between an instance and a class.
(3) Briefly (in just a few words), describe the difference between an instance and a class.
Use composition relationship. What is composition? You will find out what composition in object oriented programming....
Use composition relationship. What is composition? You will find out what composition in object oriented programming. Based on the bedroom package below please create your own classroom package. After completing entire package, you will write about your package specification in Readme.txt and also explain how you apply composition to your project. package Bedroom; public class Bedroom { private String name; private Wall wall1; private Wall wall2; private Wall wall3; private Wall wall4; private Ceiling ceiling; private Bed bed; private Lamp...
Use composition relationship. What is composition? You will find out what composition in object oriented programming....
Use composition relationship. What is composition? You will find out what composition in object oriented programming. Based on the bedroom package below please create your own classroom package. After completing entire package, you will write about your package specification in Readme.txt and also explain how you apply composition to your project. package Bedroom; public class Bedroom { private String name; private Wall wall1; private Wall wall2; private Wall wall3; private Wall wall4; private Ceiling ceiling; private Bed bed; private Lamp...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT