C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.13)

13. 

Which of the following concepts provides facility of using object of one class inside another class?

[A]. Encapsulation
[B]. Abstraction
[C]. Composition
[D]. Inheritance

Answer: Option C

Explanation:

No answer description available for this question.

Dipanjan Saha said: (May 7, 2012)  
What is composition in c++?

Gayathiri said: (May 16, 2012)  
Can anyone explain whats meant by composition in c++?

Suman.K said: (May 20, 2012)  
The process of building complex objects from simple ones is called composition.

Neha Katariya said: (Jun 7, 2012)  
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

Abhinav said: (Jul 11, 2012)  
But this is what the whole concept of Object Oriented Programming. But Question said a feature of OOPS is Composition, I have learned only four of them, and composition is not one of them.

Rahul said: (Jul 30, 2012)  
We don't have composition so please can anyone tell what do you mean by composition in c++.

Kapil Patidar said: (Sep 12, 2012)  
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: (Sep 29, 2012)  
What the main difference of composition and inheritence?

Shubha said: (Oct 12, 2012)  
Inheritance means deriving features of base class to sub-class.. where as composition means deriving a complex object from a simpler object..

Phaneendhra said: (Oct 14, 2012)  
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.

Aarti said: (Oct 17, 2012)  
Please give the example of composition.

Mohamed said: (Nov 26, 2012)  
Composition is not inheritance.

Royal said: (Dec 30, 2012)  
In the name "COMPOSITION" itself indicating that nature of ingredients.

So, here ingredients are objects & nature is is class.

Sahithi said: (Mar 11, 2013)  
What is the difference between composition and inheritance?

Shshikant said: (Mar 19, 2013)  
What is the definition of Composition?

Ravi said: (Mar 25, 2013)  
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
/*
.
.
*/
}

Dev said: (Jun 16, 2013)  
But in inheritance also we use the object of one class inside another class. Its so confusing.

Arjun said: (Jul 8, 2013)  
Composition is has a type relationship car has wheels.

class wheel
{
int id;
};
class car
{
wheel w[4];
};

Shoeb Qureshi said: (Jul 23, 2013)  
Whats the main difference between composition and inheritance?

Vinesh said: (Aug 26, 2013)  
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.

Ronzzz said: (Oct 12, 2013)  
Still not clear. When class is made up of objects, member functions are not objects then ?

Siva said: (Dec 16, 2013)  
Still now I can't understand the concept of inheritance? please give me the example for inheritance.

Suvaran said: (Jan 13, 2014)  
What is mean by composition please give answer & one example?

Divya said: (Jan 21, 2014)  
What do you mean by composition in c++?

Shivani said: (Apr 5, 2014)  
Is composition similar to inheritance?

Hardeep said: (Aug 10, 2014)  
Can anyone explain me relationship between inheritance and composition?

Ashwini said: (Oct 11, 2014)  
Why you answering as composition instead of inheritance?

Payal said: (Oct 13, 2014)  
What is Composition?

Sarang Patrange said: (Nov 9, 2014)  
When object is made from other small object it is called composition?

Sarang Patarnge said: (Nov 9, 2014)  
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.

Suchita said: (Dec 18, 2014)  
What is the difference between Containment and Composition?

Rupali Kate said: (Jan 7, 2015)  
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.

Rahul said: (Jan 9, 2015)  
Containment and composition are same.

Karthik said: (May 2, 2015)  
Inheritance is the answer. Its the class inside another existing class.

Answer C is wrong.

Sneha Bele said: (May 6, 2015)  
Can anyone give clear explanation of difference between Inheritance and Composition with examples of both?

Abhi said: (Aug 22, 2015)  
This answer should be abstraction.

Lovish said: (Mar 30, 2016)  
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
{
public:
int x;
}

class b:public a
{
x=10;
}

// But in composition,

class a
{
public:
int x;
}

class b
{
a obj;
obj.x=10;
}

We can't access x directly in other class.

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

Sachin Kunte said: (Jul 30, 2016)  
Composition means containment. Where one object may contain attributes of other object.

Amol said: (Aug 10, 2016)  
Inheritance can be know by IS A relationship.

Composition is known by HAS A relationship (its basically a code reuse).

Gelu Menumorut said: (Aug 11, 2016)  
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.

Akshay said: (Dec 9, 2016)  
The car has an engine is a composition here we are talking about some part of the car.

The car is a 4wheeler is an inheritance.

Kavi said: (Jun 21, 2017)  
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();
}

Similarly
class Employee{
Address a=new Address();
}

Khushbu said: (Sep 8, 2017)  
I think right will be inheritance as it inherits attributes and behavior of a class to another class.

Sunil said: (Jun 1, 2018)  
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).

Piyudh said: (Sep 11, 2019)  
I agree, thanks @Sunil.

Bhuneshwar Mahto said: (Feb 14, 2020)  
I don't know about composition please tell me, anyone, in detail about composition.

Hamdan Ali said: (Apr 20, 2020)  
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.

Sparsh Gupta said: (Apr 24, 2020)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.