• 从零开始的c++


    严格来说,也不算吧!(我学了c和java,有些还是比较容易理解的,面向对象这些部分还是很容易理解的)

    第一个c++的面向对象的基本引入:
    #include "iostream"
    using namespace std;//标准的命名空间,定义了很多的标准
    void main1(){
    cout<<"hello...."<<endl;//标准的输出,黑屏幕
    //<<左移操作符 在c++中 功能的增强
    //end1 打印到屏幕 并且换行
    system("pause");
    }
    //计算圆的面积 面向过程
    void main2(){
    double r=0;
    double s=0;
    cout<<"请输入圆的半径:";
    cin>>r;//cin代表标准的输入,代表键盘
    cout<<"r的值为:"<<r<<endl;
    s=3.14*r*r;
    cout<<"圆的面积是:"<<s<<endl;
    system("pause");
    }


    //struct circle{
    // double m_s;//圆的面积
    // double m_r;//圆的半径
    //};
    //面向对象的方法
    class MyCircle{
    public://成员变量
    double m_s;//圆的面积
    double m_r;//圆的半径


    public://成员函数
    void setR(double r){
    m_r=r;
    }
    double getR(){
    return m_r;
    }
    double getS(){
    return m_r*3.14*m_r;
    }


    };
    void main3(){
    MyCircle c1,c2,c3;//类的实例化 创建对象
    double r;
    cout<<"请输入圆的半径:";
    cin>>r;//给r赋值
    c1.setR(r);
    cout<<"得到的圆的面积为:"<<c1.getS()<<endl;
    system("pause");
    }

    第二个namespace的基本的代码理解:
    #include "iostream"
    using namespace std;//iostream 没有标准的std 所以需要我们自己来进行手动添加


    namespace as{
    int a=0;
    };


    namespace as1{
    int a=11;
    namespace as2{
    struct  test
    {
    int age;
    char name[20];
    };
    };
    };


    void main4(){
    //std::cout<<"namespace test"<<std::endl;
    //cout<<as::a<<endl;//测试直接使用a
    using as1::as2::test;
    test a;
    a.age=1;
    //cout<<as1::as2::test::age<<endl;//这是一个错误的结果
    //(25): error C2597: 对非静态成员“as1::as2::test::age”的非法引用
    cout<<a.age<<endl;
    system("pause");
    }
    第三个const的c++的一些特性
    #include"iostream"
    using namespace std;


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


    void test1(const student *st){//指针指向的内存空间不能够被修改
    //st->age=20;//error C3490: 由于正在通过常量对象访问“age”,因此无法对其进行修改
    st=NULL;//这样是可以跑起来的
    }


    //还有就是const student *st和student const *st这两个是一样的
    void test2( student *const st){//指针变量本身不能够被修改
    st->age=10;//这种又是可以跑起来的
    //st=NULL;//error C3892: “st”: 不能给常量赋值
    }


    void test3(const student *const st){
    //st->age=10;//这样两个都是错误的
    //st=NULL;//error C3892: “st”: 不能给常量赋值
    }


    void main(){
    const int a=10;
    int *p=(int *)&a;
    *p=20;
    cout<<"a的值为:"<<a<<endl;//值还是没有变化a还是为10
    system("pause");
    }
  • 相关阅读:
    常见字体图标库——font-awesome
    windows server 2012显示桌面图标
    SE 2014年4月14日
    SE 2014年4月13日
    PPP协议总结
    SE 2014年4月12日
    django运行时报错
    linux-python在vim下的自动补全功能
    python发邮件
    背景透明兼容
  • 原文地址:https://www.cnblogs.com/csnd/p/16675668.html
Copyright © 2020-2023  润新知