C++笔记


inline函数(内联函数)和define(预定义)的作用相似,但却有不同之处:

比如,预定义一个求平方的方法:(编译环境是Xcode)

#include <iostream>
using namespace std;
#define SQUARE(x) x*x
int main (int argc, const char * argv[])
{
    //内联函数,在成员函数里不含有循环体,则默认为内联函数
    int theSquare (int a);
    // 从下面可以看出预定义与内联函数作用的区别
    cout<<SQUARE(1+2)<<endl;
    cout<<theSquare(1+2)<<endl;
    return 0;
}
inline int theSquare (int a)
{
    return a*a;
}

输出结果:

[Switching to process 1394 thread 0x0]
5
9
Program ended with exit code: 0

与预定义不同的是,内联函数在运算时先把参数计算出来再代入算法之中。


在C++中可以用class和struct声明一个类:

用struct声明

struct function
{
    int firstNumber;
    int secondNumber;
    
    int sum(int a,int b)
    {
        return a+b;
    }
};

    cout<<f1.sum(5, 8)<<endl<<f1.firstNumber<<endl;
输出结果:
13
0
用class声明

class function
{
    public:
    int firstNumber;
    int secondNumber;
    public:
    int sum(int a,int b)
    {
        return a+b;
    }
};

输出结果与上面一样,那么两者有什么区别呢?

用struct声明的类中如果不写public、private的话,系统默认里面的变量和成员函数是公用的

而用class声明的类中默认为私有变量和私有成员函数


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值