随笔分类 -  [010_C++基础]

摘要:今天丢人丢大发了,往项目组邮件列表里发了一封关于函数定义的邮件,讨论了关于如下形式的函数定义:#include<stdio.h>void function(arg1, arg2)int arg1;int arg2;{ printf("arg1=%d, arg2=%d", arg1, arg2);}int main(){ function(); function(1); function(1,2); return 0;}上面的代码输出为:arg1=134513424, arg2=134513755arg1=1, arg2=134513755arg1=1, arg. 阅读全文
posted @ 2012-10-09 09:38 邵贤军 阅读(1375) 评论(2) 推荐(0) 编辑
摘要:private 自己可以访问protected 自己和派生类可以访问public 谁都能访问上面是三者的访问权限,这对C++的封装性起到很大作用,但是我们还有一个神器:friend。friend是个什么东西呢?它可以使得任何函数都可以访问类的private和protected成员。对于类来说,它破坏了类的封装性以及安全性。不过,friend在实际编程中很少使用,也尽量少用。此外,一些小知识:1. struct在C++也是可以继承的,且默认继承权限是public的2. class声明时,成员权... 阅读全文
posted @ 2012-10-09 01:03 邵贤军 阅读(427) 评论(0) 推荐(0) 编辑
摘要:1 #include<iostream> 2 using namespace std; 3 4 class Human{ 5 public: 6 Human(){ 7 cout<<"constrct"<<endl; 8 } 9 ~Human(){10 cout<<"destruct"<<endl;11 }12 private:13 int age;14 };15 16 int main(){17 Human human;18 19 Human *humanPtr;20 humanPtr = ne 阅读全文
posted @ 2012-10-09 00:47 邵贤军 阅读(494) 评论(1) 推荐(0) 编辑
摘要:1 #include<iostream> 2 using namespace std; 3 4 int main(){ 5 char ch1 = 'A'; 6 cout<<"ch1 = "<<ch1<<endl; 7 8 char ch2 = '中'; 9 cout<<"ch2 = "<<ch2<<endl;10 11 wchar_t ch3 = '中';12 cout<<"ch3 = "&l 阅读全文
posted @ 2012-10-09 00:14 邵贤军 阅读(766) 评论(1) 推荐(0) 编辑
摘要:1 int function(int a, int b = 0)2 {3 cout<<"a + b = "<<(a+b)<<endl;4 return 0 ;5 }上面的 int b = 0就是默认参数了,如果不传入b值的话,函数默认将使用b=0.注意:默认参数必须出现在结尾,或连续结尾。即如下的声明是非法的:1 int function(int a=0, int b)2 {3 cout<<"a + b = "<<(a+b)<<endl;4 a = 10;5 b = 20;6 ret 阅读全文
posted @ 2012-10-08 23:23 邵贤军 阅读(450) 评论(1) 推荐(0) 编辑
摘要:函数参数的传递学问也不小,不过呢,核心就是两个概念:值传递和引用传递,也很简单。值传递:传递参数的复制体,函数体内对参数进行修改,原参数值不会发生变化。引用传递:传递函数的地址,函数体内修改参数会引起原参数值的变化。值传递 1 using namespace std; 2 3 int function(int a, int b){ 4 cout<<"a + b = "<<(a+b)<<endl; 5 return 0 ; 6 } 7 8 int main(){ 9 int a = 1;10 int b = 2;11 function(a, 阅读全文
posted @ 2012-10-08 23:13 邵贤军 阅读(505) 评论(1) 推荐(0) 编辑
摘要:函数的声明方法1: 1 #include <iostream> 2 using namespace std; 3 4 int function(); 5 6 int main(){ 7 function(); 8 return 0; 9 }10 11 int function(){12 cout<<"Hello, Function."<<endl;13 return 0 ;14 }函数的声明方法2: 1 #include <iostream> 2 using namespace std; 3 4 int function(){ 阅读全文
posted @ 2012-10-08 23:05 邵贤军 阅读(392) 评论(1) 推荐(0) 编辑
摘要:1 void comment(){2 //这是单行注释3 /**4 *5 *这是多行注释6 *7 **/8 }太简单了吧!哈哈!不要小看了注释,注释也有学问。比如文件注释,类注释,函数注释,变量注释技巧等等。这里不做一一解释,多编程,自然就有了解了。 阅读全文
posted @ 2012-10-08 22:59 邵贤军 阅读(394) 评论(3) 推荐(0) 编辑
摘要:1 #include <iostream> 2 using namespace std; 3 4 namespace A{ 5 int a = 1; 6 } 7 8 namespace B{ 9 int b = 2;10 }11 12 namespace C{13 int c = 3;14 }15 16 int main(){17 18 int a = 10;19 int b = 20;20 int c = 30;21 22 using namespace A;23 using namespace B;24 usin... 阅读全文
posted @ 2012-10-08 22:55 邵贤军 阅读(550) 评论(3) 推荐(0) 编辑
摘要:C++是在C的基础上发展的,它是兼容C的。但只是兼容,它有自己完整的一套语言规则,当然你有自己的特点,其一便是<iostream>。C++引入了名字空间namespace,它避免了变量和函数名字重复冲突,这是一个很好的机制,而<iostream>便是符合这一套实现的代码。而<iostream.h>则是沿用了C风格的头文件,这个文件填入后,可以直接使用诸如cout、cin之类的函数。总结: ■ <iostream> C++语言用,如果要使用cout,需要加入using namespace std,或使用std::cout ■ <iostrea 阅读全文
posted @ 2012-10-08 22:38 邵贤军 阅读(745) 评论(2) 推荐(0) 编辑