C++

2017-10-09  本文已影响0人  __小赤佬__
Difference.png
try {
   // protected code
} catch( ExceptionName e1 ) {
   // catch block
} catch( ExceptionName e2 ) {
   // catch block
} catch( ExceptionName eN ) {
   // catch block
}
double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}
try {
   // protected code
} catch(...) {
  // code to handle any exception
}
#include <iostream>
using namespace std;

class Box {
   public:
      Box() { 
         cout << "Constructor called!" <<endl; 
      }
      ~Box() { 
         cout << "Destructor called!" <<endl; 
      }
};
int main() {
   Box* myBoxArray = new Box[4];
   delete [] myBoxArray; // Delete array

   return 0;
}
namespace namespace_name {
   // code declarations
}
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
}

// second name space
namespace second_space {
   void func() {
      cout << "Inside second_space" << endl;
   }
}

using namespace first_space;
int main () {
   // This calls function from first name space.
   func();
   
   return 0;
}
template <typename T> // function template
inline T const& Max (T const& a, T const& b) { 
   return a < b ? b:a; 
}
template <class T> // class template
class Stack { 
   private: 
      vector<T> elems;    // elements 

   public: 
      void push(T const&);  // push element 
      void pop();               // pop element 
      T top() const;            // return top element 
      
      bool empty() const {      // return true if empty.
         return elems.empty(); 
      } 
}; 
#define PI 3.14159
#define MIN(a,b) (((a)<(b)) ? a : b)

// Conditional Compilation
#ifdef DEBUG
  cerr <<"Variable x = " << x << endl;
#endif

// The # operator causes a replacement-text token to be converted to a string surrounded by quotes.
#include <iostream>
using namespace std;
#define MKSTR( x ) #x

int main () {
  cout << MKSTR(HELLO C++) << endl;
  return 0;
}

// The ## operator is used to concatenate two tokens. Here is an example −
#include <iostream>
using namespace std;
#define concat(a, b) a ## b

int main() {
  int xy = 100;
  
  cout << concat(x, y);
  return 0;
}
Line( int len );             // simple constructor
Line( const Line &obj);  // copy constructor
~Line();                     // destructor
Line line2 = line1; // This also calls copy constructor
class Box {
   double width;
   
   public:
      double length;
      friend void printWidth( Box box );
      void setWidth( double wid );
};

// Note: printWidth() is not a member function of any class.
void printWidth( Box box ) {
   /* Because printWidth() is a friend of Box, it can
   directly access any member of this class */
   cout << "Width of box : " << box.width <<endl;
}

friend class ClassTwo;

Reference:

  1. C++
上一篇 下一篇

猜你喜欢

热点阅读