zoukankan      html  css  js  c++  java
  • 【C++】指针的引用及面向对象

    指针的引用

    #include <iostream>
    using namespace std;
    
    struct Teacher
    {
        char name[64];
        int age;
    };
    
    int getTeacher(Teacher **p) {
        Teacher *tmp = NULL;
        if (p == NULL) {
            return -1;
        }
        tmp = (Teacher *)malloc(sizeof(Teacher));
        if (tmp == NULL) {
            return -2;
        }
        tmp->age = 33;
        *p = tmp;
    }
    
    int getTeacher2(Teacher* &myp) {
        myp = (Teacher *)malloc(sizeof(Teacher));
        if (myp == NULL) {
            return -1;
        }
        myp->age = 36;
    }
    
    void FreeTeacher(Teacher *pT1) {
        if (pT1 == NULL) {
            return;
        }
        free(pT1);
    }
    
    void main() {
        Teacher *pT1 = NULL;
        getTeacher(&pT1);
        cout << "age:" << pT1->age << endl;
        FreeTeacher(pT1);
    
        getTeacher2(pT1);
        cout << "age:" << pT1->age << endl;
        FreeTeacher(pT1);
        cout << "hello..." << endl;
        system("pause");
    }

    C++中可以声明const引用

    const Type& name = var

    const引用让变量拥有只读属性

    const引用总结

    1.const& int e 相当于const int * const e

    2.普通引用相当于int *const e1

    3.当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值分配空间,并将引用名作为这段空间的别名

    4.使用字面量对const引用初始化后,将生成一个只读变量

    inline void printA() {
        int a = 10;
        cout << "a" << a << endl;
    }

    编辑器将内联函数直接插入函数调用的地方,一般是常用的小函数

    内联函数中不能写循环语句

    结论:
    1内联函数在编译时直接将函数体插入函数调用的地方

    2inline只是一种请求,编译器不一定允许这种请求

    3)内联函数省去了普通函数调用时压栈,跳转和返回的开销 

    #include <iostream>
    using namespace std;
    
    inline int myfunc(int a, int b) {
        return a < b ? a : b;
    }
    
    #define MYFUNC(a,b)((a)<(b)?(a):(b))
    
    void main() {
        int a = 1;
        int b = 3;
        //int c = myfunc(++a, b); 输出结果:2;3;2
        int c = MYFUNC(++a, b); //宏替换并展开 ((++a) < (b) ? (++a) : (b))  输出结果:3;3;3
        cout << "a:" << a << ";b:" << b << ";c:" << c << endl;
        system("pause");
    }

    默认参数

    #include <iostream>
    using namespace std;
    
    void myPrint(int x = 3) {
        cout << "x:" << x << endl;
    }
    
    void myPrint2(int x = 3, int y = 4) {
        cout << "x:" << endl;
    }
    
    void main() {
        //myPrint();
        //myPrint(4);
        myPrint2(4);
        system("pause");
    }

    函数默认参数的规则

    只有参数列表后面部分的参数才可以提供默认参数值

    一旦在一个函数调用中开始使用默认参数值,那么这个参数后的所有参数都必须使用默认参数值

    函数占位参数

    #include <iostream>
    using namespace std;
    
    void func(int a, int b, int) {
        cout << "a:" << a << "b:" << b << endl;
    }
    
    void main() {
        //func(1, 2); 两个参数不适用
        func(1, 2, 3);

    当函数默认参数遇上函数重载会发生什么

    int func(int a, int b, int c = 0)
    {
        return a * b * c;
    }
    
    int func(int a, int b)
    {
        return a + b;
    }

    存在二义性,编译不通过

    //函数指针 基础的语法
    //1声明一个函数类型
    typedef void (myTypeFunc)(int a,int b) ;  //int
    //myTypeFunc *myfuncp = NULL; //定义一个函数指针 这个指针指向函数的入口地址
    
    //声明一个函数指针类型 
    typedef void (*myPTypeFunc)(int a,int b) ;  //声明了一个指针的数据类型 
    //myPTypeFunc fp = NULL;  //通过  函数指针类型 定义了 一个函数指针 ,
    
    //定义一个函数指针 变量
    void (*myVarPFunc)(int a, int b);
    //
    
    myPTypeFunc fp; //定义了一个 函数指针 变量

    封装

    #include <iostream>
    using namespace std;
    class MyCircle {
    public:
        double m_r;
        double m_s;
    public:
        double getR() {
            return m_r;
        }
        void setR(double r) {
            m_r = r;
        }
        double getS() {
            m_s = 3.14*m_r*m_r;
            return m_s;
        }
    };
    
    void printCircle01(MyCircle *pC) {
        cout << "r:" << pC->getR() << endl;
        cout << "s:" << pC->getS() << endl;
    }
    
    void printCircle02(MyCircle &myc) {
        cout << myc.getS() << endl;
    }
    
    void printCircle03(MyCircle myc) {
    
    }
    
    void main() {
        MyCircle c1, c2;
        c1.setR(10);
        cout << "c1 s:" << c1.getS() << endl;
    
        c1.setR(11);
        printCircle01(&c1);
    
        c2.setR(20);
        printCircle01(&c2);
    
        printCircle02(c2);
    
        system("pause");
    }

     lclass成员变量默认是私有的

    struct成员变量默认是共有的

    #pragma once  //只包含一次
    
    相当于
    
    #ifndef __MYTEACHER_H_  //ctrl +shift + u 变大写
    #define __MYTEACHER_H_
    ...
    #endif

    这样.h文件就只会写一次到类文件中去

    练习1:判断两个立方体是否相同

    Cube.h

    #pragma once
    class Cube
    {
    public:
        void setA(int a);
        void setB(int b);
        void setC(int c);
        int getA();
        int getB();
        int getC();
        void setABC(int a, int b, int c);
    private:
        int m_a;
        int m_b;
        int m_c;
        int m_s;
        int m_v;
    public:
        int judgeCube(Cube &v2);
    };

    Cube.cpp

    #include "Cube.h"
    
    void Cube::setA(int a) {
        m_a = a;
    }
    
    void Cube::setB(int b) {
        m_b = b;
    }
    void Cube::setC(int c) {
        m_c = c;
    }
    int Cube::getA() {
        return m_a;
    }
    int Cube::getB() {
        return m_b;
    }
    int Cube::getC() {
        return m_c;
    }
    
    void Cube::setABC(int a, int b, int c) {
        m_a = a; m_b = b; m_c = c;
    }
    
    int Cube::judgeCube(Cube &v2) {
        if (m_a == v2.getA() && m_b == v2.getB() && m_c == v2.getC()) {
            return 1;
        }
        else {
            return 0;
        }
    }

    main.cpp

    #include <iostream>
    using namespace std;
    #include "Cube.h"
    
    void main() {
        Cube v1, v2;
        v1.setABC(1, 2, 3);
        v2.setABC(2, 3, 4);
        cout << v1.judgeCube(v2)<< endl;
        system("pause");
    }

    练习2:判断点是否在圆内

    Point.h

    #pragma once
    class Point
    {
    public:
        int getX();
        int getY();
        void setPoint(int _x, int _y);
    private:
        int x;
        int y;
    };

    Point.cpp

    #include "Point.h"
    
    void Point::setPoint( int _x, int _y) {
        x = _x; y = _y;
    }
    int Point::getX() {
        return x;
    }
    int Point::getY() {
        return y;
    }

    Circle.h

    #pragma once
    #include "Point.h"
    class Circle
    {
    public:
        void setCircle(int _r, int _x, int _y);
        int judge(Point &p);
    private:
        int r;
        int x;
        int y;
    };

    Circle.cpp

    #include "Circle.h"
    #include "Point.h"
    
    void Circle::setCircle(int _r, int _x, int _y) {
        r = _r; x = _x; y = _y;
    }
    int Circle::judge(Point &p) {
        int dd =(x - p.getX())*(x - p.getX()) + (y - p.getY())*(y - p.getY());
        if (dd <= r*r) {
            return 0;
        }
        else {
            return 1;
        }
    }

    main.cpp

    #include <iostream>
    using namespace std;
    #include "Circle.h"
    #include "Point.h"
    
    void main() {
        Circle c;
        Point p;
        c.setCircle(2, 3, 3);
        p.setPoint(4, 4);
        if (c.judge(p) == 0) {
            cout << "点在圆内" << endl;
        }
        else {
            cout << "点在圆外" << endl;
        }
        system("pause");
    }
  • 相关阅读:
    Tomcat:基础安装和使用教程
    java部署
    tomcat 配置访问路径 server.xml配置去掉项目名称 .
    linuxACL控制
    Your PHP installation appears to be missing the MySQL
    ssh报错
    502 Bad Gateway
    单点登录SSO
    tomcat详细介绍
    详解redis5.x版本
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/7196820.html
Copyright © 2011-2022 走看看