C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 13)
Which of the following concepts provides facility of using object of one class inside another class?
Answer: Option
No answer description is available. Let's discuss.
48 comments Page 1 of 5.

Hamdan Ali said:   4 years ago
It's a part of Association, where two classes communicate each other but don't have a relationship and inside Association the form of Aggregation where two classes objects can communicate each other but one class denotes as owner and one class denotes its part for example Department has lecturers if department object destroys lecturers will exist another example Address can relate to one and more entity if people those are living on that address leave the address so the address will exist and inside aggregation, the form is composition, whether one entity lifecycle depends on another entity, means like if House and Room relationship if the house will destroy room will also destroy this called composition.

Lovish said:   8 years ago
In inheritance, we can use data members and member function the class which is inherited but in composition you can initialise the object of other class and with the help of that object you can access its members.

For example:

class a
int x;

class b:public a

// But in composition,

class a
int x;

class b
a obj;

We can't access x directly in other class.

We have to create obj of that class but in inheritance we can do.

Neha katariya said:   1 decade ago
In real-life, complex objects are often built from smaller, simpler objects. For example, a car is built using a metal frame, an engine, some tires, a transmission, a steering wheel, and a large number of other parts. A personal computer is built from a CPU, a motherboard, some memory, etc.. Even you are built from smaller parts: you have a head, a body, some legs, arms, and so on. This process of building complex objects from simpler ones is called composition

Ravi said:   1 decade ago
Composition takes the relationship between two classes one step further by ensuring that the containing object is responsible for the lifetime of the object it holds.

class X
int i;
X() { i = 0; }
void set1(int I) { i = I; }
class Y
int j;
X x; // Composition of other class object
Y() { i = 0; }
void set2(int J) { j = J; }

int main()
Y y;
y.x.set1(10); // Access the embedded object

Gelu Menumorut said:   7 years ago
The question is very inaccurate, it should have been:.

"Of the defining object of one class as a member of another class?".

Compare this with "Of using object of one class inside another class?".

Actually "using" means to have access to an instance by any mean (even as a parameter) and call it's member functions. None of the options match this trivial functionality, but "None" was provided as an option.

Sarang patarnge said:   9 years ago
Inheritance is relation in which, one class shares behaviors of one or more classes. Also in inheritance, subclass represents general behavior of its super class.

In composition allows physical grouping of logically related structures while inheritance allows common groups to be easily reused among different abstractions.

Eg. Room has wall, System unit has motherboard.

Sunil said:   6 years ago
Composition is a HAS-A relationship. One class can hold the object of another class and also manages its life-cycle. So we can class A has a B class object.

Inheritance is IS-A relationship. Derived class is derived from the base class. So we can say that derived class is a base class (since it holds all the properties which is in base class).

Kavi said:   6 years ago
We establish composition (HAS-A) relation between classes if one object cannot exist without another object.

For ex-Student/Employee/anyone cant exist without Address.
Say Student HAS-A address
& Employee HAS-A address
class Address{}
class Student{
Address a=new Address();

class Employee{
Address a=new Address();

Kapil Patidar said:   1 decade ago
Here we are talking about object, not class, so don't relate this with inheritance. So 'composition' is related to the object and 'inheritance' is related to classes so the answer here will be COMPOSITION. Thank you.

Rupali Kate said:   9 years ago
Inheritance and composition are two different concepts:

Inheritance deals with classes and the composition deals with objects.

Composition means deriving a complex object from a simpler object.

Post your comments here:

Your comments will be displayed after verification.