zoukankan      html  css  js  c++  java
  • C++第一天学习

    代码1

    #include<iostream>

    int main()
    {
    int a;
    std::cout << "hello c++" << std::endl;
    std::cin >> a;
    return 0;
    }

    代码2

    #include<iostream>
    int main(void)
    {
    int a;
    std::cin>>a;
    std::cout<<a<<std::endl;
    return 0;
    }

    代码

    #include<iostream>
    namespace ns
    {
    //int a;
    namespace ns1
    {
    namespace ns2
    {
    int aa;
    }
    }
    };
    namespace ns
    {
    int a;
    int b;
    }
    namespace std
    {
    int c;
    }

    using namespace ns;
    using std::cout;
    using std::endl;
    int a;
    int main(void)
    {
    ns::a=10;
    ::a=30;
    std::c=20;
    cout<<::a<<endl<<std::c<<endl;
    return 0;
    }

    代码4

    #include<iostream>
    namespace ns
    {
    int a;
    namespace ns1
    {
    namespace ns2
    {
    int aa;
    }
    }
    };
    namespace ns
    {
    int b;
    //int a;//error
    }
    namespace std
    {
    int c;
    }
    using namespace ns;
    using std::cout;
    using std::endl;
    int a;
    int main()
    {
    ns::a = 10;
    std::cout << ns::a << std::endl;
    ns::a = 20;
    cout << ns::a << endl;
    ::a = 30;
    cout << ::a<< endl;
    ns::ns1::ns2::aa = 100;
    namespace NS=ns::ns1::ns2;
    cout << NS::aa << endl;
    return 0;
    }

    代码5

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;

    struct student
    {
    int num;
    char name[20];
    int age;
    };

    int main(void)
    {
    student stu;//可以省去struct
    stu.num=007;
    strcpy(stu.name,"chengjisihan");//可以用string类来写直接可以赋值
    stu.age=21;
    printf("%03d",stu.num);
    cout<<endl<<stu.name<<endl<<stu.age<<endl;
    return 0;
    }

    代码6

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;

    struct student
    {
    int num;
    string name;
    int age;
    void show(void)//c++函数可以写在结构体中
    {
    cout<<num<<endl<<name<<endl<<age<<endl;
    }
    };

    int main(void)
    {
    student stu;
    cin>>stu.num>>stu.name>>stu.age;
    stu.show();
    return 0;
    }

    代码7

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

    union myunion
    {
    int a;
    char c;
    };

    int main()
    {
    myunion u;
    u.a = 0x12345678;
    printf("u.c = %x ",u.c);//78
    return 0;
    }

    #include<stdio.h>
    //#include<stdbool.h>
    enum color{red,green,blue};

    int main()
    {
    // bool ok;
    // ok = true;
    // printf("ok = %d ",ok);
    enum color c;
    c = 10;

    printf("c = %d ",c);
    }

    #include<iostream>
    using namespace std;

    int main()
    {
    bool ok;
    ok = true;
    // cout << ok << endl;
    ok = 100000000000;
    cout << ok << endl;//1

    ok = "hello";
    cout << ok << endl;//1

    ok = NULL;
    cout << ok << endl;//0
    cout << sizeof(ok) << endl;//1
    return 0;
    }

    #include<iostream>
    using namespace std;

    int add(int a,int b)
    {
    return a+b;
    }
    double add(double d1,double d2)
    {

    return d1+d2;
    }
    void add(int b)
    {

    cout << b << endl;
    }

    int main()
    {
    int a = 10,b = 20;
    double d1 = 10.0,d2 = 20.0;
    cout << add(a,b) << endl;
    cout << add(d1,d2) << endl;
    add(a);
    return 0;
    }

    #include<iostream>
    using namespace std;
    int add(int a,int b=100);
    int add(int a,int b)
    {
    return a+b;
    }

    int add(int a,int b ,int c=30)
    {
    return a+b+c;
    }

    int main()
    {
    cout << add(10) << endl;
    cout << add(10,30,40) << endl;
    return 0;
    }

    #include<iostream>
    using namespace std;

    int main()
    {
    int *p;
    p = new int(100);
    // *p = 10;
    cout << *p << endl;
    delete p;
    p = NULL;
    int *p1 = new int[5]{1,2,3,4,5};
    for(int i= 0; i < 5; i++)
    {
    cout << p1[i] << endl;
    }
    delete []p1;
    p1 = NULL;
    return 0;
    }

    C++/QT

    c++ 4天左右

    qt 3周左右

    --------------

    c++

    1.C++C差异

    2.封装

    3.继承

    4.多态

    ---------------

    1.C++简介

    C++

    c with classes

    肯 汤普逊 ken Thompson Unix之父 1943-

    丹里斯  里奇  C语言之父  Unix之父 1941-2011

    本贾尼 斯特劳斯特卢普 丹麦人  C++之父 1950-

    C++重要历史事件

    1979- c with classes

    1983- 正式定名为C++

    1985- 第一款产品级的C++编译器

    1987- GNU C++

    1992-   MS C++ ->Visual C++

    1998- ISO标准    c++98

    2003- 修订 c++03

    2011- ISO C++做了革命性的改变 c++11

    2014- 扩展

    CC++共同点:

    编译型语言  --  解释型语言

    强类型语言

    最大的不同点:

    C++增加了面向对象机制

    2.第一个C++程序

    1.头文件

    C <stdio.h>  -> <cstdio>

    C++: <iostream>

    .h/.hpp

    2.输入/输出方式

    C: printf() scanf()

    C++: cout -> console output

    cin  -> console input

    endl -> end line

    << 流插入运算符(输出运算符)

    >> 流提取运算符(输入运算符)

    3.std名字空间

    std是标准C++库中所有的函数、类型、变量、对象的名字空间

    4.扩展名

    C: .c

    C++: .cpp  .C .cc .cxx  

    5.编译方式

    C: gcc/cc

    C++: g++/c++

    gcc hello.cpp -lstdc++

    练习:

    输入两个整数

    求和

    并输出

    3.名字空间/命名空间 namesapace

    1.为什么要用名字空间?why

    用来避免名字冲突

    可以用来给名字逻辑分类

    2.什么是名字空间?what

    本质:就是一块由程序员分配指定的内存空间

    名字空间应用要有一个名字 如:std

    3.怎么使用名字空间?how

    1.定义名字空间

    namesapace 名字空间的名字

    //名字空间的成员

    ;

    注:名字空间成员可以是变量、类型、函数、对象...

    2.名字空间用法:

    1.通过作用域运算符 ::

    名字空间名::要访问的成员

    std::cout

    2.名字空间指令

    using namesapace 名字空间名;

    using namesapace std;

    cout cin

    3.名字空间声明

    using 名字空间名::名字空间成员;

    using std::cout;

    using std::cin;

    using std::endl;

    3.无名名字空间

    不属于任何名字空间的标识符,将被编译器放到无名名字空间中。

    使用无名名字空间成员:

    ::标识符

    4.名字空间嵌套

    namesapace ns

    {

    namesapace ns1

    {

    namesapace ns2

    {

    int aa;

    }

    }

    }

    名字空间别名:

    ns::ns1::ns2::aa = 100;

    namespace NS=ns::ns1::ns2;

    4.结构、联合、枚举

    1.结构体

    结构体关键字struct可以省略

    结构体中可以包含函数,称为成员函数

    结构体中的数据成员,称为成员变量

    (在C语言中验证)

    2.联合体(共用体)

    联合体的关键字union,也可以省略。

    3.枚举

    枚举的关键字enum可以省略

    C++中枚举是一种完全独立的类型,不能把整数直接赋值给枚举变量。

    5.字符串

    C:字符数组、字符指针

    char *c;

    char c[];

    C++: 表示字符串的类型string

    < > >= <= += = != ==

    6.布尔类型

    C++bool类型是基本类型之一,用来表示逻辑值

    bool类型只有两种值,1表示真 true0表示假 false

    bool类型占一个字节

    bool类型可以接受任意类型的表达式

    只要非0 即为真

    7.函数重载

    在相同的作用域中,函数名相同,参数表不同,这样的函数构成重载关系。

    函数重载原理:

    通过C++编译器换名实现的。

    8.缺省参数/默认参数

    一般情况下,实参个数应与形参个数相同,但C++允许实参数与形数个数不同。

    1.在声明函数时,所有指定默认值的参数必须出现在不指定默认值的参数的右边。

    默认参数必须靠右。

    2.在同时使用缺省参数和函数重载时,要避免出现二义性

    3.如果函数的声明和定义 分开写了,那么缺省要写在声明里,定义时不写。

    9.动态内存分配

    C: malloc()/free() 函数

    C++: new/delete 运算符

    new 分配内存

    delete 释放内存

    运算符new分配空间的基本用法:

    指针变量名=new 类型;

    new在分配空间的同时,可进行初始化

    指针变量名=new 类型(初值)

    delete 指针变量名;

    new 还可以用于为数组动态分配内存空间

    指针变量名 = new 类型[数组元素个数]

    (new 类型[数组元素个数]{初始值},但是需要c++11标准支持 )

    char *p = new char[3];

    释放数组内存:

    delete []指针变量名;

    delete []p;

    作业:

    写一个C++风格的结构体

    实现功能:

    从键盘输入年月日,输出 这是这一年中的第几天

    注:要考虑闰年。

  • 相关阅读:
    vs2010创建文件夹
    strlen源码,远没有想象中的那么简单、、、、
    排序
    字符数组,字符指针,sizeof,strlen总结
    QT中的QInputDialog的小例子
    QT实现启动画面
    QT中Dialog的使用

    QT中的文件浏览
    Python日期操作
  • 原文地址:https://www.cnblogs.com/liudehao/p/5674745.html
Copyright © 2011-2022 走看看