+ 2
Nesting?
I am having a little difficulty wrapping my head around what nesting is could someone explain or give me a simple example of what nesting is and also what is this good for? is it just a fancy word for encapsulating code or is this two different meanings?
5 ответов
+ 8
Nesting in a very simple way is-
"Something inside Something".
In C++,
When we use a loop inside a loop,
we says that inner loop is nested inside the outer loop.
Confused?
See this example-
for(int i=0;i<10;i++) //loop 1
{
for(int j=0;j<10;j++) //loop 2
{
cout<<"Hello";
}
}
You can clearly see that loop 2 is inside loop 1, so it is nested loop.
While, Encapsulation is wrapping of all the data into a single unit.
In OOP, we say that Class is a user defined datatype that achieves encapsulation because-
"We wrap the data members (variables) and member functions (functions) into a single unit called class".
+ 8
So many,
like you may have heard about programs for making patterns like a pyramid of "*", "#", or pascal triangle,
All they are very hard to do without nesting, because they must need two or more loops for their constructions and those loops must be outer loops and an inner loops.
There are many more examples you will see when you lesrn more and morw about C++ 😉
+ 5
Mark the answer best you are satisfied with.
It's a good habit for all us to do 😊
And You're welcome
+ 4
Thanks, I now understand it perfectly. What are the advantages of nesting?
+ 4
I see, I greatly appreciate you're help Sachin.
Thank you.