We can utilize polymorphic classes using a unique pointer:
#include <iostream> #include <memory> class MyBaseClass { public: /* w ww . j a va 2 s . c o m*/ virtual void printmessage() { std::cout << "Hello from a base class."; } }; class MyderivedClass: public MyBaseClass { public: void printmessage() { std::cout << "Hello from a derived class."; } }; int main() { std::unique_ptr<MyBaseClass> p = std::make_unique<MyderivedClass>(); p->printmessage(); }
No need to explicitly delete the allocated memory, the smart pointer does it for us.
for calculating the time in seconds it takes to fall a given distance in feet:
time = sqrt(2 * distance / g)
g is the gravitational constant equal to 32.2 ft/sec2.
#include <iostream> #include <cmath> using namespace std; int main()//from w w w . j av a 2 s .c om { int height; double time; height = 800; time = sqrt(2 * height / 32.2); cout << "It will take " << time << " seconds to fall " << height << " feet.\n"; return 0; }