C++?++C? 这是一个问题, 国庆第四天, 闲来无事, 一直想看下 C++, 白天有空看了一下, 晚上来整梳理一番.
- Hello,C++
- #include < iostream > //引入头文件using namespace std;//使用名称空间, int main(){ //输出语句 std是C++封装了的类 ::表示调用类里面的静态函数或属性 cout输出函数 //std::cout<<"hello ,C++\n" ; cout<<"hello ,C++\n" ;}</iostream>
- 基本数据类型
- #include < iostream > using namespace std; //使用名称空间,int main() { bool islike=false; //bool类型值可以是true也可以是false,其实对应值是1和0 short s=10; int i=10; long l=10; float f=10.1; double d=10.22; long double ld=300; char c='c'; wchar_t w='ha'; cout<<"is like="<<islike<<"\n" s="<<s<<" i="<<i<<" l="<<l<<" f="<<f<<" d="<<d<<" ld="<<ld<<" c="<<c<<" w="<<w<<" pre=""><p>我们可以看到比C新增的数据类型bool,我感觉这只是在C的基础上简单的封装了下,因为我们打印true和false分别是1和0.</p><h2 id="符号"> - &符号</h2><p>在C中我们知道&有取地址符的使用方法,和与的位运算,在C++中多了一种引用的用法:其实这种引用就是把变量重新取了个小名,他们指向的地址是相同的.</p><pre class="brush:java;"># include<iostream>using namespace std;int main() { //&取地址符 int i=10; int* p=&i; cout<<"*p="<<*p<<"\n"; cout<<"p="<<p<<"\n"; int="" x="1;" y="2;" z="<<z<<" a="<<a<<" ref="<<ref<<" pref="10;" pre="" img="" data-cke-saved-src=/uploadfile/2017/0220/20170220043344394.png" src=/uploadfile/2017/0220/20170220043344394.png" alt="这里写图片描述" title="" h2="" id="类" -="" class="" using="" namespace="" const="" flag="<<flag; }<=" string="" str="<<str; }<=" sum="<<sum; }<=" i="0;i<" char="" ::iterator="" startiterator="ivec.begin();" student="" void="" my="" name="student" is="<<name<<" age="<<age<<" private:="" stu.name="huyuxin" struct="" --="" stu3="new" new="" stu4="new"></p<<"\n";></iostream></pre><p<<"\n"; int="" x="1;" y="2;" z="<<z<<" a="<<a<<" ref="<<ref<<" pref="10;" pre="" img="" data-cke-saved-src=/uploadfile/2017/0220/20170220043344394.png" src=/uploadfile/2017/0220/20170220043344394.png" alt="这里写图片描述" title="" h2="" id="类" -="" class="" using="" namespace="" const="" flag="<<flag; }<=" string="" str="<<str; }<=" sum="<<sum; }<=" i="0;i<" char="" ::iterator="" startiterator="ivec.begin();" student="" void="" my="" name="student" is="<<name<<" age="<<age<<" private:="" stu.name="huyuxin" struct="" --="" stu3="new" new="" stu4="new"><p>我们可以发现和java中类的定义有一定的差别,但是也有相通之处,我们可以在上面看到四种对象的实例方法,在C++中也有<strong>new</strong>这个关键字,但是和java不同的是java返回的是类对象,而C++返回的是这个对象内存空间的首地址 –>指针接收</p><h2 id="static">- static</h2><p>对于静态函数和静态变量的声明在java中区别不大,需要注意的是静态函数的调用格式是<strong>类名::静态函数名</strong>(好像在C++没有方法的叫法,都是叫函数的,可能只是习惯问题)</p><pre class="brush:java;"># include<iostream>#include<vector>using namespace std;//类的定义class Student { public://访问修饰符 const static int MAX=100; //静态函数 static void say(string content){ cout<<content<<"\n"; int="" just="" like="" this="" pre=""><h2 id="继承"> - 继承</h2><p>既然是面向对象编程那自然是少不了继承的概念,在C++中继承不需要extends关键字</p><pre class="brush:java;"># include<iostream>#include<vector>using namespace std;//类的定义class Supclass { public: int a; Supclass() { a=10; b=20; c=30; } protected: int b; private: int c;};class Sonclass:public Supclass { public: void show_a() { cout<<"a="<<a<<"\n void="" b="<<b<<" c="<<c<<" int="" sonclass="" pre=""><p>需要注意的是,在C++中有三种继承方式:public继承,protected继承,private继承 <img data-cke-saved-src=/uploadfile/2017/0220/20170220043344721.png" src=/uploadfile/2017/0220/20170220043344721.png" alt="这里写图片描述" title=""> 在父类中修饰为private的方法和变量在子类是不可见得.</p><h2 id="多继承"> - 多继承</h2><p>这是一个有趣的东西,为啥是有趣呢?因为可以多继承,一个儿子就可以有多个爸爸了, 在java中是只能单继承的但是可以多实现(implement)</p><pre class="brush:java;"># include<iostream>#include<vector>using namespace std;//类的定义class SupclassA{ public: int a; SupclassA() { a=10; }};class SupclassB{ public: int b; SupclassB() { b=100; }};class Sonclass:public SupclassA,public SupclassB{ public: void show(){ cout<<"show in Sonclass :A.a="<<p></p> </vector></iostream></pre></a<<"\n></vector></iostream></pre></content<<"\n";></vector></iostream></pre></p<<"\n";></islike<<"\n"></iostream>
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/03-10/18399093.html