摘要: 说实话,学习C++以来,第一次听说"Metaprogramming"这个名词。 Predict the output of following C++ program. 1 #include 2 using namespace std; 3 4 template struct funStruct 5 { 6 enum { val = 2*funStruct::val }; 7 }; 8 9 template struct funStruct10 {11 enum { val = 1 };12 };13 14 int main()15 {16 cout ::v... 阅读全文
posted @ 2013-11-26 22:30 虔诚的学习者 阅读(323) 评论(0) 推荐(0) 编辑
摘要: Default parameters for templates in C++: Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char. 1 #include 2 using namespace std; 3 4 template class A 5 { 6 public: 7 T x; 8 ... 阅读全文
posted @ 2013-11-26 22:22 虔诚的学习者 阅读(206) 评论(0) 推荐(0) 编辑
摘要: Function templates and static variables: Each instantiation of function template has its own copy of local static variables. For example, in the following program there are two instances: void fun(int ) and void fun(double ). So two copies of static variable i exist. 1 #include 2 using names... 阅读全文
posted @ 2013-11-26 22:17 虔诚的学习者 阅读(322) 评论(0) 推荐(0) 编辑
摘要: We have discussed assignment operator overloading for dynamically allocated resources here . This is a an extension of the previous post. In the previous post, we discussed that when we don’t write our own assignment operator, compiler created assignment operator does shallow copy and that cause p.. 阅读全文
posted @ 2013-11-26 22:02 虔诚的学习者 阅读(368) 评论(0) 推荐(0) 编辑
摘要: In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play smart role in such situations. For example consider the following class 1 ... 阅读全文
posted @ 2013-11-26 21:47 虔诚的学习者 阅读(216) 评论(0) 推荐(0) 编辑
摘要: Predict the output of following C++ program. 1 #include 2 3 using namespace std; 4 5 class Complex 6 { 7 private: 8 double real; 9 double imag;10 11 public:12 // Default constructor13 Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) 14 {15 }16 17 ... 阅读全文
posted @ 2013-11-26 21:42 虔诚的学习者 阅读(285) 评论(0) 推荐(0) 编辑
摘要: The answer is same as Copy Constructor. If a class doesn’t contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment oper... 阅读全文
posted @ 2013-11-26 21:36 虔诚的学习者 阅读(328) 评论(0) 推荐(0) 编辑
摘要: Difficulty Level: Rookie Consider the following C++ program. 1 #include 2 #include 3 4 using namespace std; 5 6 class Test 7 { 8 public: 9 Test() 10 {11 }12 Test(const Test &t)13 {14 cout<<"Copy constructor called "<<endl;15 }16 Test& operator = (const Test &... 阅读全文
posted @ 2013-11-26 21:24 虔诚的学习者 阅读(309) 评论(0) 推荐(0) 编辑
摘要: In C++, RTTI (Run-time type information) is available only for the classes which have at least one virtual function. For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’) to type `class D*’ (source type is not polymorphic) ” becau... 阅读全文
posted @ 2013-11-26 21:13 虔诚的学习者 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 这个不懂,等看会了再写。。。 阅读全文
posted @ 2013-11-26 21:10 虔诚的学习者 阅读(90) 评论(0) 推荐(0) 编辑
摘要: Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. Source: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP34-CPP.... 阅读全文
posted @ 2013-11-26 21:07 虔诚的学习者 阅读(347) 评论(0) 推荐(0) 编辑
摘要: Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static typed (the purpose of RTTI is different) language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In... 阅读全文
posted @ 2013-11-26 21:01 虔诚的学习者 阅读(466) 评论(0) 推荐(0) 编辑
摘要: We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. 1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual int fun(int i) 8 { 9 }10 };1... 阅读全文
posted @ 2013-11-26 20:44 虔诚的学习者 阅读(146) 评论(0) 推荐(0) 编辑
摘要: In C++, a static member function of a class cannot be virtual. For example, below program gives compilation error. 1 #include 2 using namespace std; 3 4 class Test 5 { 6 public: 7 // Error: Virtual member functions cannot be static 8 virtual static void fun() 9 { 10 ... 阅读全文
posted @ 2013-11-26 20:32 虔诚的学习者 阅读(353) 评论(0) 推荐(0) 编辑
摘要: In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class. In other words, it is not necessary to use the keyword virtual in the derived class while declaring redefined versions of the virtual base class functio.. 阅读全文
posted @ 2013-11-26 20:25 虔诚的学习者 阅读(266) 评论(0) 推荐(0) 编辑
摘要: Predict the output of following C++ program. 1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void fun ( int x = 0 ) 8 { 9 cout fun();28 return 0;29 } Output: Derived::fun(), x = 0 If we take a closer look at the output, we observe that fun()... 阅读全文
posted @ 2013-11-26 20:19 虔诚的学习者 阅读(190) 评论(0) 推荐(0) 编辑
摘要: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor. 1 #include 2 usin... 阅读全文
posted @ 2013-11-26 20:12 虔诚的学习者 阅读(368) 评论(0) 推荐(0) 编辑
摘要: 派生类可以从基类中继承: (1)基类中定义的每个数据成员(尽管这些数据成员在派生类中不一定可以被访问); (2)基类中的每个普通成员函数(尽管这些成员函数在派生类中不一定可以被访问); (3)The same initial data layout as the base class; (4)基类中的static成员函数; 派生类无法从基类中继承: (1)基类的构造函数和析构函数; (2)基类的友元; Please write comments if you find anything incorrect, or you want to share more inf... 阅读全文
posted @ 2013-11-26 19:46 虔诚的学习者 阅读(227) 评论(0) 推荐(0) 编辑
摘要: In C++, compiler creates a default constructor if we don’t define our own constructor (See this). Compiler created default constructor has empty body, i.e., it doesn’t assign default values to data members (In java, default constructors assign default values). Compiler also creates a copy constru... 阅读全文
posted @ 2013-11-26 19:37 虔诚的学习者 阅读(212) 评论(0) 推荐(0) 编辑
摘要: Predict the output of following program?1 #include 2 using namespace std;3 4 int main() 5 {6 7 cout << int() << endl;8 return 0;9 } A constructor without any arguments or with default values for every argument, is treated as default constructor. It will be called by the compiler when in 阅读全文
posted @ 2013-11-26 19:09 虔诚的学习者 阅读(376) 评论(0) 推荐(0) 编辑