Extra

Problem delete array of derived class

        #include<iostream>
        using namespace std;
        class Base{
            public:
            Base(){cout<<"Base constructs\n";}
            virtual ~Base(){cout<<"Base destructs\n";}
        };
        class Derived: public Base{
            public:
            int *pt;
            Derived(){
                pt=new int[100];
                cout<<"Derived constructs\n";
            }
            ~Derived(){
                delete[] pt;
                cout<<"Derived destructs\n";
            }
        };
        int main(){
            while(1){
                //Base b = Base(); //works
                //Derived d= Derived();
                Base *b = new Derived[3];
                delete[] (Derived*)b;//if there is no 'virtual', delete only base object
            }
            return 0;
        }

Input argument to executable file[extra]

    #include <iostream>
    using namespace std;
    int main(int argc, char** argv) {
        for(int i=0; i<argc;i++){
            cout<<argv[i]<<endl;
        }
        return 0;
    }

output

    >>main.exe -l -a
    main.exe
    -l
    -a

Template [extra]

Tempalte allow us to create functions and containers for an unspecified datatype. Example: to crate a singly linked list that able to push and pop a particualr datatype.

results matching ""

    No results matching ""