随笔分类 -  C++(Geeks For Geeks)

摘要: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 虔诚的学习者 阅读(276) 评论(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 虔诚的学习者 阅读(378) 评论(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 虔诚的学习者 阅读(229) 评论(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 虔诚的学习者 阅读(392) 评论(0) 推荐(0)
摘要:Predict the output of the below code snippet. 1 #include 2 using namespace std; 3 4 int i; 5 6 class A 7 { 8 public: 9 ~A()10 {11 i=10;12 }13 };14 15 int foo()16 {17 i=3;18 A ob;19 return i;20 }21 22 int main()23 {24 cout 2 using namespace std; 3 4 int i; ... 阅读全文
posted @ 2013-11-26 11:06 虔诚的学习者 阅读(235) 评论(0) 推荐(0)
摘要:Predict the output of following programs. 1 #include 2 using namespace std; 3 4 class Test 5 { 6 private: 7 ~Test() 8 { 9 }10 };11 int main()12 {13 } The above program compiles and runs fine. It is not compiler error to create private destructors. What do you say about below program... 阅读全文
posted @ 2013-11-26 10:48 虔诚的学习者 阅读(429) 评论(0) 推荐(0)
摘要:A constructor without any arguments or with default value for every argument, is said to be default constructor. What is the significance of default constructor? Will the code be generated for every default constructor? Will there be any code inserted by compiler to the user implemented default... 阅读全文
posted @ 2013-11-26 10:42 虔诚的学习者 阅读(403) 评论(0) 推荐(0)
摘要:Initializer List is used to initialize data members of a class. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon. Following is an example that uses initializer list to initialize x and y of Point class. 1 #include 2 using namespace ... 阅读全文
posted @ 2013-11-26 10:37 虔诚的学习者 阅读(475) 评论(0) 推荐(0)
摘要:In C++, class variables are initialized in the same order as they appear in the class declaration. Consider the below code. 1 #include 2 3 using namespace std; 4 5 class Test 6 { 7 private: 8 int y; 9 int x; 10 public:11 Test() : x(10), y(x + 10) 12 {13 }14 void ... 阅读全文
posted @ 2013-11-26 10:24 虔诚的学习者 阅读(165) 评论(0) 推荐(0)
摘要:Reference:http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 转载请注明:http://www.cnblogs.com/iloveyouforever/ 2013-11-26 10:16:00 阅读全文
posted @ 2013-11-26 10:16 虔诚的学习者 阅读(231) 评论(0) 推荐(0)
摘要:In C++, compiler by default creates default constructor for every class. But, if we define our own constructor, compiler doesn’t create the default constructor. For example, program 1 compiles without any error, but compilation of program 2 fails with error “no matching function for call to `my... 阅读全文
posted @ 2013-11-26 10:11 虔诚的学习者 阅读(193) 评论(0) 推荐(0)
摘要:In C++, delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. 1 #include 2 #include 3 int... 阅读全文
posted @ 2013-11-26 10:01 虔诚的学习者 阅读(252) 评论(0) 推荐(0)
摘要:Following are the differences between malloc() and operator new. (1)new calls constructors, while malloc() does not. In fact primitive data types (char, int, float.. etc) can also be initialized with new. For example, below program prints 10. 1 #include 2 3 using namespace std; 4 5 int main... 阅读全文
posted @ 2013-11-26 09:56 虔诚的学习者 阅读(179) 评论(0) 推荐(0)
摘要:Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered. (1)delete operator works only for objects allocated using operator new (See http://geeksforgeeks.org/?p=8539). If the object is created using new, then we can do delete this,... 阅读全文
posted @ 2013-11-26 09:43 虔诚的学习者 阅读(402) 评论(0) 推荐(0)
摘要:In C++, this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X* (see code 1 below), if the member function is declared volatile, th... 阅读全文
posted @ 2013-11-26 09:37 虔诚的学习者 阅读(267) 评论(0) 推荐(0)
摘要:The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. 'this' pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available 阅读全文
posted @ 2013-11-26 09:29 虔诚的学习者 阅读(360) 评论(0) 推荐(0)
摘要:Predict the output of following C++ program: 1 #include 2 using namespace std; 3 4 class A 5 { 6 public: 7 A() { cout 2 using namespace std; 3 4 class A 5 { 6 int x; 7 public: 8 A() 9 { 10 cout 2 using namespace std; 3 4 class A 5 { 6 int x; 7 public: 8 A(... 阅读全文
posted @ 2013-11-26 09:05 虔诚的学习者 阅读(343) 评论(0) 推荐(0)
摘要:在C++中,类的静态成员函数有以下几点需要注意: (1)静态成员函数没有this指针 例如,下面的程序有编译错误"`this’ is unavailable for static member functions"。 1 #include 2 class Test 3 { 4 static Test * fun() 5 { 6 return this; // compiler error 7 } 8 }; 9 10 int main()11 {12 getchar();13 return 0;14 } (2)静态成... 阅读全文
posted @ 2013-11-26 08:50 虔诚的学习者 阅读(530) 评论(0) 推荐(0)
摘要:In C++ and Java, functions can not be overloaded if they differ only in the return type. For example, the following program C++ and Java programs fail in compilation. (1)C++ Program 1 #include 2 int foo() 3 { 4 return 10; 5 } 6 7 char foo() { // compiler error; new declaration of foo()... 阅读全文
posted @ 2013-11-25 22:43 虔诚的学习者 阅读(316) 评论(0) 推荐(0)
摘要:Predict the output of following C++ program. 1 #include 2 using namespace std; 3 4 class Test 5 { 6 protected: 7 int x; 8 public: 9 Test (int i):x(i) 10 { 11 }12 13 void fun() const14 {15 cout 3 using namespace std; 4 5 void fun(const int i) 6 { 7 c... 阅读全文
posted @ 2013-11-25 22:34 虔诚的学习者 阅读(301) 评论(0) 推荐(0)