Regarding Memory allocation | Sololearn: Learn to code for FREE!


Regarding Memory allocation

I've a question regarding memory allocation, Pls see the code given below- #include<iostream> using namespace std; Class Fruit { int cost; } int main() { Fruit Apple,Mango;/*If I created object's of the class,So does the variable(int cost) in this class allocated different memory? Like I created 2 object's of class Fruit.Apple and mango,So if I initialise the cost of Apple=2 and Mango=4 ,So do they get different memory? }


1/8/2022 9:33:16 PM

Manav Roy

7 Answers

New Answer


Yes, and Yes! Every class instance gets its own copy of the class' member variables, except for `static` members, but that doesn't apply here. Pointers and/or references can be used to make multiple identifiers point to the same object.


Martin Taylor Ohh Nice, You have alot programing experience.


Martin Taylor I am learning C++ through YouTube and I've completed C++ till memory allocation(From an indian youtuber playlist) Well,Thanks for your answer bro😀




Martin Taylor Well, You are amazing,Thanks for answering my almost all questions 😀 BTW,Since how many years you being into the programing world?And are u a student or working person?