Submitted Questions

  • What are the different way of creating object in c++ ?

    NikunjSingh

    • Feb 23rd, 2012

    1. using normal object creation: Shape obj; 2. copy ctor: Shape obj2=obj;// or Shape obj3(obj); 3. using new operator;

    Code
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5. class Shape{
    6. public:
    7.         Shape(){cout<<"Shape..."<<endl;}
    8.         Shape(const Shape& obj)
    9.         {
    10.                 cout<<"Copy called"<<endl;
    11.         }
    12.         void* operator new(size_t size)
    13.         {
    14.                 cout<<"new...."<<endl;
    15.                 void* storage=malloc(size);
    16.                 if(NULL == storage) {
    17.                         throw "allocation fail : no free memory";
    18.                 }
    19.                 return storage;
    20.         }
    21.  
    22.         void operator delete (void*){
    23.  
    24.         }
    25. };
    26.  
    27. int main()
    28. {
    29.         Shape obj;
    30.         Shape obj2=obj;
    31.         Shape obj3(obj);
    32.         Shape* ptrShape=new Shape();
    33.         Shape* ptrShapeOver= new Shape();
    34.         return 0;
    35. }

    Isaac

    • Feb 21st, 2012

    1. Create on stack. i.e. ClassA varA;
    2. Create on heap. i.e. ClassA *ptrA = new ClassA;

  • C++ without Header files

    How we can run our c++ program without header files?

    Eric Nantel

    • Apr 23rd, 2015

    Put everything in the only cpp containing main()

    harshit

    • Apr 14th, 2015

    We can run c++ program without using header files by making the definition of all the functions and keywords in the programs