随笔分类 - C++(Geeks For Geeks)
摘要:In C++, following function declarations cannot be overloaded. (1)Function declarations that differ only in the return type. For example, the following program fails in compilation. 1 #include 2 int foo() 3 { 4 return 10; 5 } 6 7 char foo() 8 { 9 return 'a'; 10 }11 12 int main()13 {1...
阅读全文
摘要:在C++中,基于以下如下我们通过以引用reference的形式传递变量。 (1)To modify local variables of the caller function A reference(or pointer) allows called function to modify a local variable of the caller function. For example, consider te following example program where fun() is able to modify local variable x of main()....
阅读全文
摘要:在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联。 但是,在使用引用的地方仍然会有一些例外。 (1)Reference to value at uninitialized pointer1 int *ptr;2 int &ref = *ptr; //Reference to value at some random memory location (2)Reference to a local variable is returned 1 int& fun()2 {3 int a = 10;4...
阅读全文
摘要:When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. 1 #include 2 using namespace std; 3 4 int main() 5 { 6 int x = 10; 7 8 // ref is a reference to x. 9 int& ref = x;...
阅读全文
摘要:Predict the output of the following program? 1 #include 2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 cout 2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 Empty a, b;11 12 if (&a == &b)13 {14 cout 2 using namespace std...
阅读全文
摘要:A class declaration can contain static object of self type,it can also have pointer to self type,but it can not have a non-static object of self type。 例如,下面的程序可运行。 1 // A class can have a static member of self type 2 #include 3 4 using namespace std; 5 6 class Test 7 { 8 static Test self; ...
阅读全文
摘要:在C++中,除了以下几点外,struct和class是相同的。 (1)class的成员的默认访问控制是private,而struct的成员的默认访问权限是public。 例如,program 1会编译失败,而program 2可正常运行。 1 // Program 1 2 #include 3 4 class Test 5 { 6 int x; // x is private 7 }; 8 9 int main()10 {11 Test t;12 t.x = 20; // compiler error because x is private13 getc...
阅读全文
浙公网安备 33010602011771号