0

what is new,i dont understand

16th Oct 2016, 9:05 AM
suhail
5 Respostas
+ 2
new is used to create new memory example: int x=5; int y=5; int z ; // let take any condition for sum if (x>0) { z=x+y; cout<<z;} then it will give output as 10 now the value of x and y are fixed which are stored into fixed memory now if u want to input some value by using user input (cin) then where should will it save in order used it in sum input from user cannot saved and use in further calculation s so u have to save it into memory for that u have to use "new" example : int a; cin >>a; cout<<a; // if u type 6 then it will give as 6 out put and if u want to use 6 in further calculation then int *p = new int ; *p = a+5; cout <<*p; u will get output as 11 in that we save a and then assign its address to p. if u get it then reply and not then also... I will try in other way... Good luck
16th Oct 2016, 9:51 AM
Jay Patil
Jay Patil - avatar
+ 1
thank u jay patil but i think u have one more try...😊
16th Oct 2016, 10:00 AM
suhail
+ 1
new is to create an object dynamically. Such an object will persist in memory till you use delete on that object (don't forget to do it, or else you will clog up the ram with each execution of your program, this is called a memory leak). Example: Creation of a dynamic array which is then filled by user input. Since it is created dynamically, its length can be set at run time, allowing us to set it via user input, whereas the size of a classic array is set at compile time. https://code.sololearn.com/c2gdSAv1hD5u #include <iostream> using namespace std; int main(void) { int i, len; int *arr; cout << "Creation of a dynamic array filled via user input" << endl; cout << "Number of elements?" << endl; cin >> len; arr = new int[len]; for (i = 0; i < len; i++) { cout << "Element number " << i+1 << "?" << endl; cin >> arr[i]; } cout << "You entered: "; for (i = 0; i < len; i++) { cout << arr[i] << " "; } delete arr; return 0; } @Jay Patil: "input from user cannot be saved and used in further calculations" -> That's false.
16th Oct 2016, 10:38 AM
Zen
Zen - avatar
+ 1
So if you have a `class Foo {}` and you want to use a `Foo` in your code, you can write `Foo f;` somewhere in your code, and you'll get a new `Foo` to play with. However, you can also do `Foo* f = new Foo;` and the difference can be a bit tricky at first. The reason on why there are two systems to make new objects is differences in "storage duration". Let's look at an example: void makeFoos(){ Foo f; Foo* g = new Foo; } Say you call this within your main function. You can't access `f` or `g` in your main function, but if you were to add `return f`, you'd actually get a copy of `f` while the original one is destroyed, and if you `return g`, you get the actual `g`. That is to say: - f has 'automatic storage duration', it 'goes out of scope' once you are outside the pair of {curly braces} it was declared in, and it will be destroyed. - g has 'dynamic storage duration' and is not bound to any scope. It will live on until you manually call `delete` on it. What does that mean in practice? If you plan on using an object for very long and in all sorts of different contexts, use `new`. If you just need an object inside one function, use normal variables. I suppose if you are just starting out, avoid `new` (since you need to call `delete` too and everybody forgets that) and use normal variables until you run into a problem. Then remember my words and it'll all make sense :P Another thing: objects that were created using `new` are allocated on the "heap", ordinary variables on the "stack". I don't think you need to worry about that much, other than that the heap is a lot bigger than the stack. If your object needs massive amounts of storage, use `new` aswell!
16th Oct 2016, 1:10 PM
Schindlabua
Schindlabua - avatar
0
if u want to understand plz see my program in which all info.that I explained earlier. search as :- calculator 1234 In that I use double in place of int in order to make calculations of numbers contenting decimal points(6.567) 👍👍
16th Oct 2016, 12:36 PM
Jay Patil
Jay Patil - avatar