Skip to main content

Posts

Part1 : STL Algorithms (Non-Modifying sequence) in c++

Non-Modifying sequence operations : 1.  Non-modifying sequence operations:   _of (CPP 11) std::array all_of_elem = { 3,5,7,11,13,17,19,23 }; 1. if ( std::all_of ( all_of_elem.begin(),   all_of_elem.end() ,  [](int i) {return i % 2; } )) std::cout << "All the elements are odd numbers.\n"; std::array any_of_elem = { 0,1,-1,3,-3,5,-5 }; 2. if ( std::any_of ( any_of_elem.begin() ,  any_of_elem.end() ,  [](int i) {return i )) std::cout << "There are negative elements in the range.\n"; std::array foo = { 1,2,4,8,16,32,64,128 }; 3. if ( std::none_of ( foo.begin() ,  foo.end() ,  [](int i) {return i )) std::cout << "There are no negative elements in the range.\n"; 2.  Non-modifying sequence operations:   find       std::string myints[] = { "Hello", "Hi", "Bye", "ByeBye" }; std::vector myvector(myints, myints + 4); std::vector ::iterator it;...

Sequence Containers: Vectors

Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Container properties Sequence Dynamic array Allocator-aware Example : #include #include int main () { std::vector< int > myvector; for ( int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:" ; for (std::vector< int >::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n' ; return 0; }

Clone Function in C++

class Dog {  public:    virtual Dog* clone() { return (new Dog(*this)); }  // co-variant return type }; class Yellowdog : public Dog {     virtual Yellowdog* clone() { return (new Yellowdog(*this)); } }; void foo(Dog* d) {      // d is a Yellowdog    //Dog* c = new Dog(*d); // c is a Dog    Dog* c = d->clone();    // c is a Yellowdog     //...     //  } int main() {    Yellowdog d;    foo(&d); }

Invoke Virtual Function in Constructor or Destructor

Case 1:  class Dog { public: Dog()  { cout << "Dog Born" << endl; } virtual void bark()  { cout << "Dog barking" << endl; } void SeeCat() { bark();  // first it check bark() in YellowDog else invokes in Dog class } }; class YellowDog : public Dog  { public: YellowDog() { cout << "Yellow Dog Born" << endl; } virtual void bark()  // virtual keyword is optional        { cout << "Yellow barking" << endl; } }; int main() { YellowDog d; d.SeeCat(); } Output:  Dog Born Yellow Dog Born Yellow Dog barking Case 2: why Virtual function in constructor  should be avoided class Dog { public: Dog()  { cout << "Dog Born" << endl; bark();  Since construtor of yellowdog is not yet constructed, so bark of Dog class gets called. } virtual void bark()  { cout << "Do...

Exceptions in Destructors

When dog objects in main() go out of scope, it invokes destructor. Since destructor throws an exception, due to multiple exception condition program crashes. class dog { public: string m_name; dog(string name)  {  m_name = name;  cout << name << " is born." << endl;  } ~dog()  {  cout << m_name << " is distroied.\n" << endl; throw 10; } }; int main() { try { dog dog1("Henry"); dog dog2("Bob"); } catch (int e) { cout << e << " is caught" << endl; } return 1; } Solution 1:   Destructor swallow the exception ~dog() {        try {          // Enclose all the exception prone code here       } catch (MYEXCEPTION e) {          // Catch exception       } catch (...) {       }    } Solution 2:   Move ...

Shared Pointer : Alternative to Virtual Destruct

When a base class points to a derived class object and when delete gets called on pointer then only base class destructor gets called. Solution 1:  TO overcome this issue, make base class destructor as virtual. With this change, both the derived class and base class destructor gets called. Solutions 2 : Using Shared_ptr class Dog { public:    ~Dog() {  cout << "Dog is destroyed"; } }; class Yellowdog : public Dog { public:    ~Yellowdog() {cout << "Yellow dog destroyed." < }; class DogFactory { public:     //static Dog* createYellowDog() { return (new Yellowdog()); }    static shared_ptr createYellowDog() {        return shared_ptr (new Yellowdog());     }     //Connot  useful through Unique_ptr in this case //static unique_ptr createYellowDog() {    //   return unique_ptr (new Yellowdog());    //} }; in...