美文网首页
C++ 命名空间 引用

C++ 命名空间 引用

作者: 闪客飞飞 | 来源:发表于2021-03-12 20:57 被阅读0次

    include <stdlib.h>

    include <stdio.h>

    include <iostream>

    //
    using namespace std;//标准的命名空间(包含很多标准的定义)
    //// << : 运算符重载
    //
    ////命名空间 类似于java中的包(归类)
    //
    ////自定义命名空间
    //namespace NSP_A{
    // int a = 9;
    // namespace NSP_A_SUN{
    // int a = 132;
    // }
    // struct Teacher
    // {
    // int age;
    // char name[20];
    // };
    //}
    //
    //namespace NSP_B{
    // int a = 12;
    // struct Teacher
    // {
    // int age;
    // char name[20];
    // };
    //}
    //void main(){
    // //printf("this is C plusplus");
    // cout << "this is c++" << endl;
    // //使用命名空间 :: 访问符
    // cout << NSP_A::a << endl;
    // cout << NSP_A::NSP_A_SUN::a << endl;
    // cout << NSP_B::a << endl;
    // using NSP_A::Teacher;
    // Teacher t;
    // t.age = 10;
    // using NSP_B::Teacher;
    // Teacher t;
    // t.age = 10;
    // system("pause");
    //}

    //const double PI=3.14;
    ////圆
    //class MyCircle{
    // //属性 (公用权限访问修饰符)
    //private:
    // double r;
    // double s;
    // double length;
    //public:
    // void setR(double r){
    // this->r = r;
    // }
    // //获取面积
    // double getS(){
    // return PIrr;
    // }
    //};
    //void main(){
    // MyCircle c1e;
    // c1e.setR(4);
    // cout << "圆的面积:" << c1e.getS() << endl;
    //
    // MyCircle c1;
    // c1.setR(4);
    //
    // cout << "圆的面积:" << c1.getS() << endl;
    // system("pause");
    //}
    //结构体
    //struct MyTeacher
    //{
    //public:
    // char name[20];
    // int age;
    //public:
    // void say(){
    // cout << this->age << "岁" << endl;
    // }
    //};
    //void main(){
    // MyTeacher t;
    // t.age = 12;
    // t.say();
    // system("pause");
    //}
    //void main(){
    // bool isSingle = false;
    // cout << isSingle << endl;
    // if (isSingle > 0){
    // cout << "单身" << endl;
    // }else{
    // cout << "有对象" << endl;
    // }
    // system("pause");
    //}

    //引用
    //void main(){
    // //变量名 -门牌号(内存空间的别名 )
    // int a = 10;
    // //引用
    // int &b = a;
    // b = 20;
    // cout << a << endl;
    // system("pause");
    //}

    //指针值交换
    void swap_1(int *a, int *b){
    int c = 0;
    c = *a;
    *a = *b;
    *b = c;
    }

    //引用值交换
    void swap_2(int &a, int &b){
    int c = 0;
    c = a;
    a = b;
    b = c;
    }

    void main(){
    int x = 10;
    int y = 20;

    printf("%d,%d\n", x, y);
    //swap_1(&x, &y);
    //a成了x的别名
    swap_2(x, y);
    printf("%d,%d\n", x, y);
    
    system("pause");
    

    }

    //指针的引用替代二级指针
    //struct Teacher
    //{
    // char* name;
    // int age;
    //};
    //void getTeacher(Teacher **tea){
    // Teacher t = (Teacher)malloc(sizeof(Teacher));
    // t->age = 13;
    // tea = t;
    //}
    ////指针的引用 替代二级指针 Teacher
    &tea=(Teacher p)
    //void getTeacher(Teacher
    &tea){
    // tea = (Teacher
    )malloc(sizeof(Teacher));
    // tea->age = 14;
    //}
    //void main(){
    // Teacher *tea = NULL;
    // getTeacher(&tea);
    // system("pause");
    //}
    //指针常量 常量指针
    //void main(){
    // //指针常量 指针的常量,不改变地址的指针,但是可以修改他的值
    // int a = 13, b = 16;
    // int *const p1 = &a;
    // //p1 = &b;
    // p1 = 4;
    // //常量指针,指向常量的指针,内容不能修改
    // const int p2 = &a;
    // p2 = &b;
    // //
    p2 = 9;
    // system("pause");
    //}
    //1.单纯给变量取别名没有任何意义,作为函数参数传递,能保证参数传递过程中不产生副本
    //2.引用可以直接操作变量,指针要通过取值(
    p),间接操作变量,指针的可读性差
    //3.指针需要判空引用不需要,引用不可能为空。引用不可能为空
    //常引用类似于java 中的final
    //常引用类似于java中final
    //void myprintf(const int &a){
    // cout << a << endl;
    //}
    //void main(){
    // //const int a;
    // //引用必须要有值,不能为空
    // //int &a = NULL;
    // //常引用
    // int a = 10, b = 9;
    // const int &c = a;
    // //字面量
    // const int &d = 70;
    // //c = b;
    // myprintf(c);
    // system("pause");
    //}

    //引用的大小
    /*
    struct Teacher{
    char name[20];
    int age;
    };

    void main(){
    Teacher t;

    Teacher &t1 = t;
    Teacher *p = &t;

    cout << sizeof(t1) << endl;
    cout << sizeof(p) << endl;
    system("pause");
    }
    */

    /*
    struct Teacher{
    char name[20];
    int age;
    };

    void myprint(Teacher *t){
    cout << t->name << "," << t->age << endl;
    }

    void myprint2(Teacher &t){
    cout << t.name << "," << t.age << endl;
    t.age = 21;
    }

    void main(){
    Teacher t;

    Teacher *p = NULL;
    //报错,防止不报错,进行非空判断
    myprint(p);

    //引用不能为空,没法传进去
    Teacher &t2 = NULL;
    myprint2(t2);

    system("pause");
    }
    */

    //函数默认参数
    /*
    void myprint(int x, int y = 9, int z = 8){
    cout << x << endl;
    }
    //重载
    void myprint(int x,bool ret){
    cout << x << endl;
    }

    void main(){
    myprint(20);

    system("pause");
    }
    */

    //可变参数
    //int...
    //void func(int i, ...){
    // ////可变参数指针
    // //va_list args_p;
    // //// 开始读取可变参数 i 是最后一个固定的参数
    // //va_start(args_p, i);
    // //int a = va_arg(args_p, int);
    // //int b = va_arg(args_p, int);
    // //char c = va_arg(args_p, char);
    // //int d = va_arg(args_p, int);
    // //int w = va_arg(args_p, int);
    //
    // //cout << a << endl;
    // //cout << b << endl;
    // //cout << c << endl;
    // //cout << d << endl;
    // //cout << w << endl;
    // //va_end(args_p);
    //
    // //可变参数指针
    // va_list args_p;
    // //开始读取可变参数,i是最后一个固定参数
    // va_start(args_p, i);
    // int a = va_arg(args_p, int);
    // char b = va_arg(args_p, char);
    // int c = va_arg(args_p, int);
    // cout << a << endl;
    // cout << b << endl;
    // cout << c << endl;
    // //结束
    // va_end(args_p);
    //}
    //void main(){
    // //func(2, 4,'b',6, 6);
    // func(9, 20, 'b', 30);
    // system("pause");
    //}

    //循环读取
    /*
    void func(int i,...)
    {
    //可变参数指针
    va_list args_p;
    //开始读取可变参数,i是最后一个固定参数
    va_start(args_p,i);
    int value;
    while (1){
    value = va_arg(args_p,int);
    if (value <= 0){
    break;
    }
    cout << value << endl;
    }

    //结束
    va_end(args_p);
    }

    void main(){
    func(9, 20, 40, 30);

    system("pause");
    }
    */
    //类 C++ 类的普遍写法

    //#include"MyTeacher.h"
    //void main(){
    // MyTeacher t;
    // t.setAge(23);
    // t.setName("chengyuelxl");
    // t.print();
    // system("pause");
    //}\

    ////构造函数 析构函数 拷贝构造函数
    //class MyClass{
    //private:
    // char* name;
    // int age;
    //public:
    // //无参构造函数 写了就会覆盖默认的构造函数
    // MyClass(){
    // cout << "无参构造函数s" << endl;
    // }
    // MyClass(char* name,int age){
    // cout << "有参构造函数" << endl;
    // this->name = name;
    // this->age = age;
    // }
    // //析构函数
    //};
    //void main(){
    // MyClass c;
    // MyClass cw("chengyue",16);
    // system("pause");
    //}

    //构造函数 析构函数 拷贝构造函数
    //class MyClass{
    //private:
    // char* name;
    // int age;
    //public:
    // MyClass(){
    // this->name =(char)malloc(100);
    // strcpy(name, "wer");
    // age = 26;
    // }
    // //析构函数
    // //当前对象被系统释放时,析构函数被执行
    // //作用:善后处理
    // ~MyClass(){
    // cout << "析构函数s" << endl;
    // free(this->name);
    // }
    //};
    //void func(){
    // MyClass m1;
    //}
    //void main(){
    // func();
    //
    // system("pause");
    //}
    //拷贝构造函数 浅拷贝
    //class MyClass{
    //private:
    // char
    name;
    // int age;
    //public:
    // MyClass(char* name, int age){
    // cout << "有参构造函数" << endl;
    // this->name = name;
    // this->age = age;
    // }
    // //拷贝构造函数 (值拷贝)
    // //默认的拷贝构造函数就是值拷贝
    // MyClass(const MyClass &obj){
    // this->name = obj.name;
    // this->age = obj.age;
    // cout << "拷贝构造函数" << endl;
    // }
    // void myprint(){
    // cout << name << "," << age << endl;
    // }
    //};
    //void main(){
    // MyClass my("chengyue", 24);
    // MyClass m2 = my;
    // m2.myprint();
    // system("pause");
    //}

    //深拷贝
    /*
    class Teacher{
    private:
    char name;
    int age;
    public:
    Teacher(char name, int age){
    int len = strlen(name);
    this->name = (char
    )malloc(len+1);
    strcpy(this->name, name);
    this->age = age;
    cout << "有参构造函数" << endl;
    }
    ~Teacher(){
    cout << "析构" << endl;
    //释放内存
    free(this->name);
    }
    //深拷贝
    Teacher(const Teacher &obj){
    //复制name属性
    int len = strlen(obj.name);
    this->name = (char
    )malloc(len+1);
    strcpy(this->name,obj.name);
    this->age = obj.age;
    }
    void myprint(){
    cout << name << "," << age << endl;
    }
    };

    void func(){
    Teacher t1("rose", 20);

    Teacher t2 = t1;
    t2.myprint();
    }

    void main(){
    func();

    system("pause");
    }
    */
    //拷贝构造函数被调用的场景
    //1.声明时赋值
    //Teacher t2 = t1;
    //t2.myprint();
    //2.作为参数传入,实参给形参赋值
    //func1(t1);
    //3.作为函数返回值返回,给变量初始化赋值
    //Teacher t3 = func1(t1);

    //这里不会被调用
    //Teacher t1 ;
    //Teacher t2;
    //t1 = t2;

    //构造函数中属性对象的初始化列表 构造函数后:
    //class Teacher
    //{
    //private:
    // char* name;
    //
    //public:
    // Teacher(char* name){
    // this->name = name;
    // cout << "Teacher 有参构造函数" << endl;
    // };
    // ~Teacher(){
    // cout << "Teacher 析构函数" << endl;
    // }
    // char* getName(){
    // return this->name;
    // }
    //};
    //class Student
    //{
    //private:
    // int id;
    // //属性对象
    // //Teacher t1=Teacher("miss cang");
    // Teacher t1;
    // Teacher t2;
    // Teacher t3;
    //public:
    // Student(int id, char *t1_name, char *t2_name, char *t3_name)
    // :t1(t1_name), t2(t2_name), t3(t3_name){
    // this->id = id;
    // cout << "Student 有参构造函数" << endl;
    // };
    // void setId(int id){
    // this->id = id;
    // }
    // void myprint(){
    // cout << t1.getName() << t2.getName() << t3.getName() << endl;
    // }
    // ~Student(){
    // cout << "Student 析构函数" << endl;
    // }
    //};
    //void func(){
    // Student s1(13, "chang", "chan23424g", "chwerwrwange");
    // //s1.setId(19);
    // s1.myprint();
    //}
    //void main(){
    // func();
    // system("pause");
    //}
    // c++ new (delete) 内存分配
    // c malloc free

    //class Teacher
    //{
    //private:
    // char* name;
    //public:
    // Teacher(char* name){
    // this->name = name;
    // cout << "Teacher 有参构造函数" << endl;
    // };
    // ~Teacher(){
    // cout << "Teacher 析构函数" << endl;
    // }
    // char* getName(){
    // return this->name;
    // }
    // void setName(char* name){
    // this->name = name;
    // }
    //};
    //void func(){
    //C++ h會調用构造函數和析构函数
    /Teacher t2 = new Teacher("cjhemg");
    cout << t2->getName << endl;
    delete(t2);
    /
    //C 不會調用构造函数和析构函数
    //Teacher t3 = (Teacher)malloc(sizeof(Teacher));
    //t3->setName("werwer");
    //free(t3);
    //}
    //void main(){
    // func();
    //数组类型
    /
    int* p1 = (int)malloc(sizeof(int) * 10);
    p1[0] = 9;
    free(p1);
    /

    //int *p2 = new int[10];
    //p2[0] = 19;
    ////释放数组[] 
    //delete[] p2;
    

    // system("pause");
    //}
    //静态的属性和函数赋值
    //class Teacher
    //{
    //public:
    // char* name;
    // static int total;
    //public:
    // Teacher(char* name,int total){
    // this->name = name;
    // this->total = total;
    // cout << "Teacher 有参构造函数" << endl;
    // };
    // ~Teacher(){
    // cout << "Teacher 析构函数" << endl;
    // }
    // char* getName(){
    // return this->name;
    // }
    // void setName(char* name){
    // this->name = name;
    // }
    // //计数的静态函数
    // static void count(){
    // total++;
    // cout << total << endl;
    // }
    //};
    ////全局的地方对静态属性初始化赋值
    //int Teacher::total = 9;
    //void main(){
    // cout << Teacher::total << endl;
    // Teacher::total++;
    // cout << Teacher::total << endl;
    // Teacher::count();
    // system("pause");
    //}
    //类的大小
    //class A{
    //public :
    // int i;
    // int j;
    // int K;
    // static int m;
    //};
    //class B{
    //public:
    // int i;
    // int j;
    // int K;
    // char * nba;
    // /void myorintf(){
    // cout << "打印" << endl;
    // };
    /
    //};
    //void main(){
    // cout << sizeof(A) << endl;
    // cout << sizeof(B) << endl;
    // //C C++ 内存分区: 栈 堆 全局 (静态成员,全局) 常量区(字符串),程序代码区
    // jvm stack , 堆, (通过对象的引用来操作堆内存中的对象),
    // native method stack(本地方法栈 jni 中native方法),
    // 方法区 (程序代码),程序计数器
    // system("pause");
    //}

    // this 指针 函数是共享的 必须要有标识当前对象是谁的办法
    // this 当前对象的指针
    //class Teacher
    //{
    //private:
    // char* name;
    // int age;
    //public:
    // Teacher(char* name, int age){
    // this->name = name;
    // this->age = age;
    // cout << "Teacher 有参构造函数" << endl;
    // };
    // ~Teacher(){
    // cout << "Teacher 析构函数" << endl;
    // }
    // //常函数 修饰的是 this
    // //既不能修改指针的值 也不能修改指针指向的内容
    // void print() const{
    // printf("%#x\n", this);
    // //this->name = "werw";
    // //改变this 指针的值 也不能改
    // cout << this->name << "," << this->age << endl;
    // }
    //};
    //void main(){
    //
    // Teacher t1("jack", 14);
    // printf("%#x\n", &t1);
    // const Teacher t2("tom", 14);//常量对象只能调用常量函数,
    // //常函数 方式数据成员被非法访问 不能修改
    //
    // t1.print();
    // t2.print();
    //
    // system("pause");
    //}
    //友元函数
    //class A{
    //private:
    // int i;
    //public:
    // A(int i){
    // this->i = i;
    // }
    // void myprint(){
    // cout << i << endl;
    // }
    // //友元函数
    // friend void modify_i(A* p,int a);
    //};
    ////友元函数的实现,在友元函数中可以访问私有的属性
    //void modify_i(A* p, int a){
    // p->i=a;
    //}
    //void main(){
    // A *a = new A(10);
    // a->myprint();//打印输出
    // modify_i(a, 20);
    // a->myprint();
    // system("pause");
    //}
    //友元类
    //class A{
    // //友元类 B可以访问A类中所有的私有成员
    // friend class B;
    //private:
    // int i;
    //public:
    // A(int i){
    // this->i = i;
    // }
    // void myprint(){
    // cout << i << endl;
    // }
    //};
    //class B{
    //public:
    // //友元类可以访问A类中的所有的成员 class.setAccessable(true)
    // void accessAny(){
    // a.i=30;
    // }
    //private:
    // A a;
    //};

    //运算符重载
    //class Point{
    //public:
    // int x;
    // int y;
    //public:
    // Point(int x,int y){
    // this->x = x;
    // this->y = y;
    // }
    // void myprint(){
    // cout << x << "," << y << endl;
    // }
    // // 重载+ 号
    // Point operator+( Point &p2){
    // return Point(this->x + p2.x, this->y + p2.y);
    // };
    // // 重载- 号
    // Point operator-( Point &p2){
    // return Point(this->x - p2.x, this->y - p2.y);
    // };
    //};
    //// 重载+ 号
    //Point operator+(Point &p1, Point &p2){
    // return Point(p1.x + p2.x, p1.y + p2.y);
    //};
    //
    //// 重载- 号
    //Point operator-(Point &p1, Point &p2){
    // return Point(p1.x - p2.x, p1.y - p2.y);
    //};

    //运算符的重载还是 函数的调用
    //void main(){
    // Point p1(12,23);
    // Point p2(124,23);
    // Point p3 = p1 + p2;
    // p3.myprint();
    // Point p4 = p1 - p2;
    // p4.myprint();
    // system("pause");
    //}

    //当属性私有时,通过友元函数完成函数的重载
    class Point{
    friend Point operator+(Point &p1, Point &p2);
    friend Point operator-(Point &p1, Point &p2);
    private:
    int x;
    int y;
    public:
    Point(int x, int y){
    this->x = x;
    this->y = y;
    }
    void myprint(){
    cout << x << "," << y << endl;
    }
    };
    // 重载+ 号
    Point operator+(Point &p1, Point &p2){
    return Point(p1.x + p2.x, p1.y + p2.y);
    };
    // 重载- 号
    Point operator-(Point &p1, Point &p2){
    return Point(p1.x - p2.x, p1.y - p2.y);
    };
    void main(){
    Point p1(12, 23);
    Point p2(124, 23);
    Point p3 = p1 + p2;
    Point p4 = p1 - p2;
    p3.myprint();
    p4.myprint();
    system("pause");
    }

    相关文章

      网友评论

          本文标题:C++ 命名空间 引用

          本文链接:https://www.haomeiwen.com/subject/hcxtcltx.html