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.
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.
Madhavikalyanapu said:
1 decade ago
What the main difference of composition and inheritence?
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..
Aarti said:
1 decade ago
Please give the example of composition.
Mohamed said:
1 decade ago
Composition is not inheritance.
Dipanjan Saha said:
1 decade ago
What is composition in c++?
Sahithi said:
1 decade ago
What is the difference between composition and inheritance?
Shshikant said:
1 decade ago
What is the definition of 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;
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
/*
.
.
*/
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers