C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 13)
13.
Which of the following concepts provides facility of using object of one class inside another class?
Discussion:
48 comments Page 4 of 5.
Arjun said:
1 decade ago
Composition is has a type relationship car has wheels.
class wheel
{
int id;
};
class car
{
wheel w[4];
};
class wheel
{
int id;
};
class car
{
wheel w[4];
};
Dev said:
1 decade ago
But in inheritance also we use the object of one class inside another class. Its so confusing.
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;
public:
X() { i = 0; }
void set1(int I) { i = I; }
}
class Y
{
int j;
public:
X x; // Composition of other class object
Y() { i = 0; }
void set2(int J) { j = J; }
};
int main()
{
Y y;
y.set2(47);
y.x.set1(10); // Access the embedded object
/*
.
.
*/
}
class X
{
int i;
public:
X() { i = 0; }
void set1(int I) { i = I; }
}
class Y
{
int j;
public:
X x; // Composition of other class object
Y() { i = 0; }
void set2(int J) { j = J; }
};
int main()
{
Y y;
y.set2(47);
y.x.set1(10); // Access the embedded object
/*
.
.
*/
}
Shshikant said:
1 decade ago
What is the definition of Composition?
Sahithi said:
1 decade ago
What is the difference between composition and inheritance?
Royal said:
1 decade ago
In the name "COMPOSITION" itself indicating that nature of ingredients.
So, here ingredients are objects & nature is is class.
So, here ingredients are objects & nature is is class.
Mohamed said:
1 decade ago
Composition is not inheritance.
Aarti said:
1 decade ago
Please give the example of composition.
Phaneendhra said:
1 decade ago
Inheritance will acquire one class properties, but here the question is not about the class, it's about an object inside a class, so the answer is composition.
Shubha said:
1 decade ago
Inheritance means deriving features of base class to sub-class.. where as composition means deriving a complex object from a simpler object..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers