C++基础_02

* inline 内联函数
>1.内联函数必须和函数体写在一起,否则编译器直接忽略内联请求
inline int getMax(int a,int b);//Error
inline int getMax(int a,int b){
    return a> b ? a : b; 
}

>2.内联函数没有普通函数调用时的额外开销(出栈,跳转,返回...)
>3.内联函数是一种特殊的函数,具有普通函数的特征(参数检查,返回类型等...)
>4.内联函数是对编译器的一种请求,因此编译器可能会拒绝内联请求.
>5.内联函数由编译器处理,直接将编译后的函数体插入到函数调用的地方,宏定义则是由预处理器处理,只是简单的进行文本片段,并没有编译过程。
>6.不同的编译器可能进行编译优化,即使一些函数没有inline声明,但仍有可能会被编译器内联优化。
>7.C++中内联编译的限制:
    不能存在任何形式的循环语句       
    不能存在过多的条件判断语句
    函数体不能过于庞大
    不能对函数进行取址操作
    函数内联声明必须在调用语句之前
>8.编译器对于内联函数的限制并不是绝对的,内联函数相对于普通函数的优势只是省去了函数调用时压栈,跳转和返回的开销。因此,当函数体的执行开销远大于压栈,跳转和返回所用的开销时,那么内联将无意义。
* inline 和 #define 的区别
#include <iostream>
using namespace std;
inline int getMax(int a,int b){
    return a < b ? a : b;
}
#define GetMAX(a,b)  ((a) < (b) ? (a) : (b))
int main(int argc, char *argv[])
{
    int a = 1;
    int b = 3;
    //int c = getMax(++a,b);//inline A = 2 B = 3 C = 2
    int c = GetMAX(++a,b);// #define A = 3 B = 3 C = 3 ==> (++a) < (b) ? (++a) : (b)
    cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
    return 0;
}
* 函数的默认参数
>若填写参数,则使用参数的值(x = 4),若不填写参数,则使用参数的默认值(x = 3)
void funA(int x = 3){...}
funA();//OK
funA(4)//OK

>默认参数必须出现在参数列表的右侧
void funA(int a,int b ,int x = 3,int y = 4);//OK
void funB(int a = 3,int b);//Error
void funC(int a,int b = 4,int c);//Error
* 函数的占位参数
>占位参数只有参数类型声明,并没有变量声明
void funA(int a,int b ,int);

>一般情况下,函数体内部无法使用占位参数
>调用funA();
funA(1,2);//Error
funA(1,2,3);//OK
* 默认参数和占位参数(方便程序以后扩展用)
void funA(int a,int b,int =  10);
funA(1,2);//OK
funA(1,2,3);//OK
* 函数重载(函数名相同,参数的[个数,类型,或位置]不同)[函数重载与函数的返回值无关]
//函数重载的例子
void funA(){...}
int funA(int a){...}
void funA(const char * a){...}
int funA(int a,int b){....}
int funA(char * a,int b){...}
void funA(int a,int b,int c){...}
void funA()(int a,char * b,int c){...}
* 函数重载和默认参数
>带有默认参数的函数重载(允许函数这样定义,但函数调用时需要注意)
void funA(int a,int b = 10){...}
void funA(int a){...}

>调用
    funA(10);//Error->存在二义性,编译失败
    funA(10,20);//OK
* 函数指针和函数重载
>函数指针并不支持重载,且和函数的参数列表以及返回值一一对应
#include <iostream>

using namespace std;

void funA(int a){
    cout<<"A = "<<a<<endl;
}

void funA(char * a){
    cout<<"A = "<<a<<endl;
}

void funA(int a,int b){
    cout<<"A = "<<a<<" B = "<<b<<endl;
}

void funA(char * a,char * b){
    cout<<"A = "<<a<<" B = "<<b<<endl;
}

//声明一个函数类型
typedef void (FunctionNumA)(char * ,char *);
typedef void (FunctionNumB)(char *);
typedef void (FunctionNumC)(int ,int );
typedef void (FunctionNumD)(int);

//声明一个函数指针类型
typedef void (*FunctionStrA)(int );
typedef void (*FunctionStrB)(int ,int );

//定义一个函数指针变量
//void (*pstr)(int);


int main(int argc, char *argv[])
{

    //定义一个函数指针(指向函数的入口地址)
    FunctionNumA * ft = NULL;
    ft = funA;//==> funA(char * a,char * b);
    ft("Hello","World");

    FunctionNumB * fp = NULL;
    fp = funA;//==> funA(char * a);
    fp("Hello World!");

    FunctionStrB fs = NULL;
    fs = funA;//funA(int a,int b);
    fs(3,6);

    return 0;
}
* 一个简单的C++类的组成
======>Teacher.h
#ifndef TEACHER_H
#define TEACHER_H

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include <iostream>
using namespace std;
class Teacher //定义一个Teacher类
{
public:
    Teacher();//构造函数
    ~Teacher();//析构函数
    Teacher(const Teacher & t);//拷贝构造函数
    Teacher & operator = (const Test & t);//赋值函数

    //成员函数

    //get and set
    void setAge(int age);
    int getAge();
    void setName(char * name);
    char * getName();

    //显示老师的信息
    void show();

    //判断是否是同一个老师
    bool isTeacher(Teacher * &t);

private:

    //成员变量

    int age;
    char * name;
};

#endif // TEACHER_H

======>Teacher.cpp
#include "teacher.h"

/*
Teacher类的实现
*/

Teacher::Teacher()
{
    this->name = new char[64];
    memset(this->name,0,sizeof(this->name));
}


//get and set
int Teacher::getAge(){
    return this->age;
}

void Teacher::setAge(int age){
    this->age = age;
}

char * Teacher::getName(){
    return this->name;
}

void Teacher::setName(char * name){
    strcpy(this->name,name);
}

void Teacher::show(){
    cout<<"======================"<<endl;
    cout<<"= Name:"<<this->getName()<<endl;
    cout<<"= Age:"<<this->getAge()<<endl;
    cout<<"======================"<<endl;
}

bool Teacher::isTeacher(Teacher * &t){
    return this == t;
}


=====>main.cpp
#include <iostream>
#include "teacher.h"

using namespace std;

int main(int argc, char *argv[])
{

    //实例化一个对象
    Teacher * t = new Teacher;

    t->setAge(18);
    t->setName("lcmm");

    t->show();

    Teacher * f = new Teacher;

    f->setAge(19);
    f->setName("yurisa");
    f->show();

    cout<<"t-t = "<<t->isTeacher(t)<<endl;
    cout<<"t-f = "<<t->isTeacher(f)<<endl;


    return 0;
}
* C++中 struct 和 class 的区别
在用struct定义类时,所有成员的默认属性为public
在用class定义类时,所有成员的默认属性为private
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值