1. 程式人生 > >C++基礎1

C++基礎1

1、行內函數

目的:消除函式呼叫時的系統開銷,提高執行速度,以空間換取時間。

#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

inline double circle(double r)
{
    return 3.1416*r*r;
}
int main(int argc, char** argv) 
{
    for(int i=1;i<=100;i++)
    cout<<"r="<<i<<"  area= "<<circle(i)<<endl;
    return 0;
}

2、(1)函式原型中,所有取預設值的引數都必須出現在不取預設值的引數的右邊

       (2)函式過載,型別不同。在同一作用域,只要函式引數的型別不同,或者引數的個數不同,兩個或兩個以上的函式可以使用相同的函式名。

3.作用域識別符號“::”

#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int avr;

int main(int argc, char** argv) 
{
    int avr;
    avr=10;//區域性變數 
    ::avr=20;//全域性變數 
    cout<<"local avr= "<<avr<<endl;
    cout<<"global avr= "<<::avr<<endl;
    
    return 0;
}

 4、new和delete運算子

程式程式碼區 全程資料區

程式執行時,計算機的記憶體被分為四個區:程式程式碼區、全程資料區、棧和堆。堆可由使用者分配和釋放。C語言使用malloc()和free()等來進行動態管理。C++運用new和delete來進行動態分配和釋放。

 

#include <iostream>
using namespace std;

int main(int argc, char** argv) 
{
    int *p;
    p=new int;
    *p=10;
    cout<<*p;
    delete p; 
    
    return 0;
}

5、引用

在C++中,變數的引用就是變數的別名。