c++学习笔记 02

#include <iostream>

using namespace std;


/*******************引用的学习****************************************************/
/*
 * 1.用地址进行传入,这样就可以进行处理了
 * 2.与引用有很大的区别的
*/
void swap_c(int *r1,int *r2)
{
    int temp = *r1;
    *r1=*r2;
    *r2=temp;
}


/*
 *引用的用法
 * 1.作为参数。
 * 也可以理解为别名
*/
void swap_cpp(int &r1,int &r2)
{
    int temp = r1;
    r1=r2;
    r2=temp;
}

/*
 * 引用的用法
 * 2.作为返回值
 * 返回一个引用,不会产生新的地址(空间)
*/
int& Count1()
{
    static int n = 0;
    n++;
    return n;
}


/*
 * 普通的返回值·
 * 返回一个临时变量,对变量进行了一个拷贝
*/
int Count2()
{
    static int n = 0;
    n++;
    return n;
}


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

/*
 * 引用的学习测试代码
 * 少创建一个拷贝,减少内存
 *
*/
void test1(){
//    int a = Count1();
//    cout<<a<<endl;
//    a=10;
//    int b = Count1();
//    cout<<a<<" "<<b<<endl;

    //int *a=Count2() 编译不会通过,传回的是一个临时变量
     int a = 10;
     int &b= a;
     int *c=&a;

}

/***********************************************************************/


/***********************************************************************/

/*
 * 权限学习测试代码
*/
void test2(){

    //    //权限的只能平级,或者缩小
    //    const int a  =10;
    //    const int b = a;

    //    int c = 20;
    //    int &d = c;
    //    const int &e = c;

    //    //赋值不属于权限
    //    const int aa;
    //    int n  = aa;
    //    cout << "Hello World!" << endl;
    //    int a = 10;
    //    int b = 20;
    //    cout<<a<<b<<endl;
    //    swap_cpp(a,b);
    //    cout<<a<<b<<endl;
    //

}

struct A
{
    int a[10000];
};

int Add(int x1,int x2)
{
    return  x1+x2;
}


/**
 * @brief swap
 * @param x1
 * @param x2
 * 频繁调用swap需要建立栈帧,会有消耗
 * 如何解决:c语言实验宏函数
 * 2、c++使用内联函数:在调用它的地方展开
 * 内联函数没有地址
 * 内联替代宏
 * 宏不能调试
 * 对宏没有;类型检查
 */
void swap(int &x1,int &x2)
{
    int temp =x1;
    x1=x2;
    x2=temp;
}

/**
 * @brief tes02 c++ auto,新函数、
 * auto 自动推导类型
 *
 */
void test02(){
    double a=1;
    auto b = a;
    cout<<typeid(b).name()<<endl;
}

void fun(int n)
{
    cout<<"int"<<endl;
}

void fun(int *p)
{
    cout<<"intpoint"<<endl;
}


/**
 * @brief test03 c++11指针
 */
void test03()
{
    //C
    int *p=NULL;

    //C++11推荐
    int *p1= nullptr;
    //nullptr更容易代表空指针

    fun(0);
    fun(p);
    fun(p1);

}

/***********************************************************************/



/********************************类的学习**************************************************/

/**
 * @brief The Person class
 * 类内可以定义:1.成员变量 2.成员方法
 */
class Person{ //class 类名

    char _name[10];


};

/**
 * @brief The Student class
 * c++中都可以用来定义类struct,class
 * class默认private
 * 
 */
class Student{
private:
    char _name[10]="asdfas";
    int _age=10;
    int _sex=1;

public:
    int getage()
    {
        return _age;
    }

    void showall()
    {
        cout<<_name<<" "<<_age<<" "<<_sex<<endl;
    }
};

/**
 * @brief test04 面向对象
 * 过程重点在过程,对象强调用于对象之间的关系
 *之间的交互关系
 * 访问限定符 public private protext
 * 默认private
 */
void test04()
{
    Student student ;
    student.showall();

}

/**
 * @brief test05类的作用域
 */
void test05()
{
    Student student ;
    student.showall();

}

/**************************************************************************************/


int main()
{


   test04();

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值