C++对C的改进(1)

本文地址:http://www.cnblogs.com/archimedes/p/cpp-change1.html,转载请注明源地址

新的初始化方法

C提供的初始化方法

int x = 1024;

C++提供两种初始化方法:

复制初始化(copy-initialization)

如:int x = 1024;

直接初始化(direct-initialization):

如:int x(1024);

注意:

①初始化不是简单地赋值,初始化指声明变量或对象并且赋初值;赋值指用新值覆盖变量或对象当前值。

②直接初始化语法更灵活且效率更高

③初始化内置类型变量两种初始化几乎没有差别对于类类型的初始化,有时只能采用直接初始化(以后讨论)

④两种初始化的方法可以混用(见下例)

合使用初始化的例子:

#include <iostream>
using namespace std;
int main()
{
    double salary = 9999.99, wage(salary + 0.01);
    cout<<salary<<" "<<wage<<endl;

    int interval,month = 8, day = 7, year(2008);
    cout<<year<<":"<<month<<":"<<day<<":"<<endl; 
    system("PAUSE");
    return 0;
}

新的I/O

C语言的输入输出:

C++语言的输入输出:

C++语言的输入输出:先睹为快

#include <iostream>
using namespace std;
int main()
{
    int x; 
    double y;
    /*以下语句等价于printf("请输入两个整数(用逗号隔开):");*/
      cout << "请输入两个整数(用空格隔开):"; 

    /*以下语句等价于scanf("%d %f", &x, &y);*/
    cin >> x >> y; 

    /*以下语句等价于printf("x = %d, y = %f
", x, y);*/
    cout << "x = " << x << ", y = " << y << endl;         
    system("PAUSE");
    return 0;
}

C++的输入输出:cin,cout的用法

基本用法:

  cout<<表达式1<<表达式2<<表达式n;

  cin>>变量1>>变量2>>变量n;

例如:
cout<<"x + y ="<< x + y << "." << endl;
cin >> x >> y;

[注意]

①不能用一个<<输出多个数据项

cout<<a,b,c<<endl;           /*错误*/
cout<<a<<b<<c<<endl;   /*正确*/

②cin/cout可以分成多行来写

cin>>a>>b
   >>c;
cout<<a<<b
    <<c;

cout的用法举例:

#include <iostream>
using namespace std;
int main()
{
     cout << "This is a C++ program! " << endl;
     cout << "This is"
          << " a C++"
          << "program!"
          << endl;
     system("PAUSE");
     return 0;
}

cin的用法举例:

int main()
{    
    char  c1, c2;
    int a;  
    float b;
    cin >> c1 >> c2 >> a >> b;
    cout << "c1 = " << c1 << endl
         << "c2 = " << c2 << endl
         << "a = " <<a << endl
         << "b = " << b << endl;
    system("PAUSE");
    return 0;
 }

cout与输出控制字符:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int x = 0;
    cout << "请输入一个八进制整数(以开始):";    
   cin >> oct >> x; cout << "x的十六进制表示为:" << hex << x << endl; cout << "x的十进制表示为:" << dec << x << endl; cout << "x的八进制表示为:" << oct << x << endl; system("PAUSE"); return 0; }

输出控制符:

 注意:若用控制符,程序需包含头文件#include<iomanip>

有关类型的区别

有关类型的区别:bool类型

 

逻辑类型

C

没提供

非0

0

C++

bool

true

false

注意:

1)bool类型的取值只有两种true,false

2)输出时默认输出0或者1

3)用boolalpha可以改变默认的输出方式,noboolalpha可以恢复默认的输出方式

#include <iostream>
using namespace std;
int main()
{
    bool bval1 = 1 < 2;
    bool bval2 = true;
    bool bval3 = false;
    bool bval4 = 4;
    bool bval5 = 0;
    cout << "bval1=" << bval1 << endl;
    cout << "boolalpha bval1=" << boolalpha << bval1 << endl;
    cout << "noboolalpha bval1=" << noboolalpha << bval1 << endl;
    cout << "bval2=" << bval2 << endl;
    cout << "bval3=" << bval3 << endl;
    cout << "bval4=" << bval4 << endl;
    cout << "bval5=" << bval5 << endl;
    system("PAUSE");
    return 0;
}

有关类型的区别:string类

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "student";
    string address = "Hebei... ...";
    cout << name << address <<endl;
    return 0;
}

string对象的定义和初始化:

初始化string对象的方式

string s1;

默认构造函数,s1为空串

string s2(s1);

将s2初始化为s1的一个副本

string s3("value");

用字符串字面值初始化s3

String s4(n,'c')

将s4初始化为字符'c'的n个副本

#include <iostream>
#include <string>
using namespace std;
string s0;
int main( )
{
    string s1;
    //string s2 = "hello world!";
    string s2("hello world!");
    //string s3 = s2;
    string s3(s2);
    string s4(5, 'r');
    cout << "s0=" <<s0 <<endl;
    cout << "s1=" <<s1 <<endl;
    cout << "s2=" <<s2 <<endl;
    cout << "s3=" <<s3 <<endl;
    cout << "s4=" <<s4 <<endl;
    system("PAUSE");
    return 0;
}

string对象的读写:用cin、cout读写string对象

注意:

cin忽略开头所有空格、TAB、回车符

不接收含空格的字符串

string的I/O:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s;
    cin >> s;      //hello world!
    cout << s <<endl;  //hello
    system("PAUSE");
    return 0;
}

string读入未知数目的对象:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string word;
    while(cin >> word)
        cout << word << endl;
    
    system("PAUSE");
    return 0;
}

string对象的读写:用getline读取整行文本(含空格)。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line;
    while(getline(cin, line))                  
        cout << line << endl;
     system("PAUSE");
     return 0;
}

注意:

getline的第一个参数通常为cin, 第二个参数为string对象

从录入数据的下一行读取,可读取  任何字符

getline()以回车作为结束符 (不接受换行符)

getline()不忽略前导回车,若第一 个就是换行符,则置为空串

string对象的操作,设有:string s, s1;

string的操作

s.empty()

若s为空串,则返回true,否则返回false

s.size()

返回s中字符的个数

s[n]

返回s中位置为n的字符,位置从0开始

s1+s2

将两个串连接成新串,返回新生成的串

s1 = s2

把s1得内容替换为s2的副本

v1 == v2

判定时候相等,相等返回true,否则返回false

!= < <= > >=

保持这些操作惯有的含义,如:s != s2;

注意:

1、size()的返回类型并非int而是string::size_type类型的值,建议不要把size()的返回值赋值给int变量

string s2 = "hello";
string::size_type count = s2.size();
2、两个string对象+时,+操作符左右操作数必须至少有一个是string
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "hello";
    string s2 = "world";
    string s3 = s1  + ",";
    string s4 = "hello" + "world ";  //error
    string s5 = "hello" + s2 + "world" ;
    system("PAUSE");
    return 0;
}
3、string对象下标操作时,任何无符号整型值均可用作下标,但下标的实际类型为string::size_type

4、string下标操作可用作左值

int main()
{  
    string str = "student";
    cout << str << endl;
    for(string::size_type ix = 0; ix!=str.size(); ++ix)
             str[ix] = 'x';
    cout << str << endl;
    
    system(" PAUSE ");
    return 0;
}

有关类型的区别:枚举

C++对枚举的改进:

①在C++中定义枚举变量可以不用enum

enum  weekday
{sun, mon, tue, wed, thu, fri, sat};
weekday  w;            //省略了enum

②无名枚举:不给出枚举类型名和变量,将枚举元素当符号常量用

enum
{min = 0, max = 100};
int x = min, arr[max];
... ...

有关类型的区别:union

C++对联合的扩展:

①无名联合:没有联合体类型名和变量名的联合体

#include <iostream>
using namespace std;
int main()
{
    union {
        char c;
        int i;
        double d;
    };
    c = 'a';
    cout << c << endl;
    return 0;
}

②定义联合变量无需给出union

#include <iostream>
using namespace std;
union test
{  
    char c;
    int i;
    double d;
};
int main()
{
    test m = {'a'};
    cout << m.c << endl;
    return 0;
}

有关类型的区别:struct

C++对结构体的扩展

①定义结构体变量可以不用struct

struct  point
{
    double x;
    int a;
};
point  p;

②成员可以包含函数定义

struct  point{
     double x,y;    //数据成员
     void  setvalue(double a,double b) //成员函数
     { 
         x = a; 
         y = b; 
     }
};

 

原文地址:https://www.cnblogs.com/wuyudong/p/cpp-change1.html