上一页 1 2 3 4 5 6 7 8 9 ··· 11 下一页
摘要: In C, we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::). 1 #include 2 using namespace std; 3 4 int x; // Global x 5 6 int main() 7 { 8 int x = 10; // Local x 9 cout<<"Value of global x is "< 阅读全文
posted @ 2013-11-27 12:18 虔诚的学习者 阅读(278) 评论(0) 推荐(0) 编辑
摘要: Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing arguments to a function using a declaration that doesn’t list argument types: If we save the below program in a .c file and compile it, it works without any error. But, if we save the same in a .cpp fil... 阅读全文
posted @ 2013-11-27 12:11 虔诚的学习者 阅读(314) 评论(0) 推荐(0) 编辑
摘要: 说实话,学习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) 编辑
上一页 1 2 3 4 5 6 7 8 9 ··· 11 下一页