fbpx

C++ Job interview question: Inheritance and object orientation source code example to create class object on heap vs stack

(Last Updated On: May 18, 2010)

C++ Job interview question: Inheritance and object orientation source code example to create class object on heap vs stack

This may be a stupid example but does show the difference between stack and heap object creation using class object:

#include
using namespace std;

class MyClass {
public:
MyClass(int x) {}
MyClass() {}
};

int main() {

MyClass *myclass = new MyClass[10]; // creates class object on heap
int i;

int j[] = {1,2,3,4,5,6,7,8,9,10};

MyClass mc2[10] = {1,2,3,4,5,6,7,8,9,10}; //creates class object on stack

MyClass* mc[10];
for (int i=0; i < 10; i++) mc[i] = new MyClass(i); //it will call user-defined constructor here. return 0; }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
Don't miss out!
Get my TRADE LIKE A BOSS 2 PDF Books

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top