C + + (c + + type enhancement)

Source: Internet
Author: User

C + + type enhanced type check more stringent

Assigns a pointer of a const type to a pointer of a non-const type. C language can pass through, but in C + + is not a part of the past

const  int  A = 10 ; a = 100 ; //const modified cannot modify the  int  *p = &a;*p = 100 ;p rintf ( "a =%d  \n  , a); //a = 100,c inside the type is not strict, the pointer can modify the const modified variable, is actually a is a const int * Type, that is, forcibly assigned to INT *, the const is omitted.  const  int  A with int const  A, Look at the const modifier who removed the type. int  *const  p; The int is removed, p cannot be changed, and the content of P points can be changed. const  int  *const  p; It points to itself and the content cannot be changed. 
#include<iostream>usingnamespace std;int main() {    constint100;//const修饰的必须要初始化    int *p = &a;//不可以报错 const int *无法转化成int *;    constint *p = &a;//才是正确的    //C++不需要强转    char *p = malloc(100);//C++会报错    char *q = (char*)malloc(100);//void *    return0;}
Boolean type (BOOL)

The logic of C language is represented by 0 and non-true. There are specific types in C + +.

int main(){    booltrue;    if(flag)        printf("hello world!\n");    else        printf("hello C++!\n");    return0;}
A true enumeration (enum)

The enumeration essence in C is an integral type, and an enumeration variable can be assigned with any integral type. In C + +, enumeration variables can only be initialized with the elements that are enumerated.

BOOL uses enumeration to implement the process:

#include<iostream>enum BOOL{    FALSE,TURE};int main(){    constint100;    BOOL flag = TRUE;    booltrue;    if(flag)        cout<<a;        printf("sizeof(flag) = %d sizeof(BOOL) = %d sizeof(bool) = %d\n",sizeof(flag),sizeof(BOOL),sizeof(bool));}//sizeof(flag) = 4 sizeof(BOOL) = 4 sizeof(bool) = 1//enum枚举本质是整型

It is not the actual data type but an object that will be learned by the string type.

enum SEASOM{    Spr,Sum,Autu,Win};int main(){    SEASON s;    s = Win;//在C++中比较严格,除了枚举外的无法赋值。c里面就可以赋其他值。    return0;}

In fact, enumerations can be equivalent to macros.

#define Spr 0#define Sun 1....enum{    1,Sum,Autu,Win};

In C + +, with the possible use of enumerations, Const replaces the use of macros.

The value of an expression can be assigned

C:

int main(){    int a,b=10;    100;//100赋值给b,b = 100这个表达式赋值给a。    100;//错误,表达式是不可被赋值的    1000;//错误    return0;}

C++

int main(){    int5;    100;    printf("a = %d b = %d\n",a,b);    1000;    printf("a = %d b = %d\n",a,b);    return0;}//a = 100,b = 5;//a = 1000,b = 5//a = b是一个表达式,b的值赋值给a,最后把100的值赋值给a,b的值没发生变化。

The limitation and promotion of C language embodied in C + +

CIN cout Class Object

scanf printf functions are functionally identical

int main(){    char name[30];    scanf("%s",name);//不安全的输入,输入很多会挂机    fgets(name,30,stdin);//最多只能输入29个字符,比较安全。    cin>>name;//>>流输入运算符    cout<<"name = "<<name<<endl;//"name = "字符串流入cout对象,name流入cout对象,endl(换行)流入cout对象中,从左至右的顺序。    string name1;    cin>>name1;    cout<<name1;    //很安全,输多少都不会挂机    cin>>a>>b;//有顺序,从键盘输入的第一个数流入a,第二个数流入b    //<=>    cin>>a;    cin>>b;    return0;}

C + + (c + + type enhancement)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.