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 2 of 5.
Aarti said:
1 decade ago
Please give the example of composition.
Mohamed said:
1 decade ago
Composition is not 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.
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
/*
.
.
*/
}
Dev said:
1 decade ago
But in inheritance also we use the object of one class inside another class. Its so confusing.
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];
};
Shoeb qureshi said:
1 decade ago
Whats the main difference between composition and inheritance?
Vinesh said:
1 decade ago
Inheritance is having properties (member variables and member functions) of a class into another class and Composition is having object of some class into another class.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers