[15 May] Predict the output

[1] 

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    char c = 'a';  
    // pass at compile time, may fail at run time
    int* q = (int*)&c; 
    int* p = static_cast<int*>(&c);
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Base {
};
class Derived : public Base {
};
int main()
{
    Derived d1;
    Base* b1 = (Base*)(&d1); // allowed
    Base* b2 = static_cast<Base*>(&d1);  
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Base {
    public:
    void func()
    {
        cout<<"Base::func called"<<endl;
    }
};
class Derived : public Base {    
};
int main()
{
    Derived d1;
    Base* b1 = (Base*)(&d1); // allowed
    Base* b2 = static_cast<Base*>(&d1); 
    b2->func();
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
#include <iostream>
#include<array>
using namespace std;

class Base {
public:
    virtual void show(int i = 0) {
        cout << "Base::show() i = " << i << endl; 
    }
};

class Derived : public Base {
public:
    void show(int i = 20) {
        cout << "Derived::show() i = " << i << endl;
    }
};

int main() {
    Base *p = new Derived();
    p->show();
    delete p;
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
#include <iostream>
#include<cstring>
using namespace std;

class Base {
public:
    Base() {
        cout << "Base()" << endl;
        clear();
    }
    void clear() {
        memset(this, 0, sizeof(*this));
    }
    virtual void show() {
        cout << "Base::show()" << endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        cout << "Derived()" << endl;
    }
    void show() {
        cout << "Derived::show()" << endl;
    }
};

int main() {
    Base *pb1 = new Base();
    pb1->show();
    delete pb1;
    return 0;
}
-----------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Design Pattern and Architecture in C++

Google Test (gtest) Notes

Stack & Queue interview Questions