【C/C++學(xué)院】(4)c++開篇/類和對象/命名空間/類型增強(qiáng)/三目運(yùn)算符/const專題/引用專題/函數(shù)增強(qiáng)
來源:程序員人生 發(fā)布時(shí)間:2015-02-06 08:31:23 閱讀次數(shù):2927次
1.類和對象
成員函數(shù),成員變量,抽象封裝的能力。
求圓的面積;
#include <iostream>
using namespace std;
class circle{
private:
double m_r;//成員變量
public:
void setR(double r)//成員函數(shù)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getS()
{
return 3.14*m_r*m_r;
}
};
void main()
{
circle c1;
c1.setR(4);
cout << "r:" << c1.getR() << "s:" << c1.getS() << endl;
c1.setR(5);
cout << "r:" << c1.getR() << "s:" << c1.getS() << endl;
system("pause");
}
2.命令空間
命名空間;c++對c的擴(kuò)大。解決標(biāo)識符沖突。
std::out :: 域作用符。
#include <iostream>
namespace NameSpaceA{
int a = 0;
}
namespace NameSpaceB{
int a = 1;
namespace NameSpaceC{
struct Teacher{
char name[10];
int age;
};
}
}
void main()
{
using namespace NameSpaceA;
printf("NameSpaceA:a=%d
", a);
printf("NameSpaceB:a=%d
", NameSpaceB::a);
using NameSpaceB::NameSpaceC::Teacher;
Teacher t1 = { "aaa", 3 };
printf("t1.name = %s
", t1.name);
printf("t1.age = %d
", t1.age);
system("pause");
}
3.語法增強(qiáng)
3.1register關(guān)鍵字增強(qiáng)
int main()
{
register int a = 0;
printf("&a = %x
", &a);
system("pause");
return 0;
}
//register關(guān)鍵字 要求編譯器讓變量a直接放在寄存器里面,速度快
//在c語言中 register修飾的變量 不能取地址,但是在c++里面做了內(nèi)容
register關(guān)鍵字的變化
register關(guān)鍵字要求“編譯器”將局部變量存儲于寄存器中
C語言中沒法獲得register變量地址
在C++中仍然支持register關(guān)鍵字
C++編譯器有自己的優(yōu)化方式,不使用register也可能做優(yōu)化
C++中可以獲得register變量的地址
3.2struct類型增強(qiáng)
struct類型的加強(qiáng):
C語言的struct定義了1組變量的集合,C編譯器其實(shí)不認(rèn)為這是1種新的類型
C++中的struct是1個(gè)新類型的定義聲明
|
struct Student
{
char name[100];
int age;
};
int main(int argc, char *argv[])
{
Student s1 = {"wang", 1};//struct Student s1={};
Student s2 = {"wang2", 2};
return 0;
}
|
4.3目運(yùn)算符
#include <iostream>
using namespace std;
//在c++里面的3目運(yùn)算符 返回是1個(gè)變量
//讓表達(dá)式做左值
//1 左值 能被放在 = 做值 稱為左值
//2 當(dāng)左值的條件, 這段內(nèi)存空間可以被你寫
int main()
{
int a = 10;
int b = 20;
int c = 31;
//返回1個(gè)最小數(shù) 并且給最小數(shù)賦值成30
//3目運(yùn)算符是1個(gè)表達(dá)式 ,表達(dá)式不可能做左值
//讓表達(dá)式做左值
(a < b ? a : b) = 30;//相當(dāng)于c中的*((a < b ? &a : &b)) = 30;
//在c中編譯不過,報(bào)錯(cuò)
printf("a = %d, b = %d
", a, b);
system("pause");
return 0;
}
5.const專題
const 定義的變量, 在c++的編譯器中, 做了1個(gè)符號表, key <--->value | a<-->10;
修改的時(shí)候, 只是修改了重新分配的空間, 對原來的const數(shù)據(jù)沒有造成修改。
而在c中,卻可以將const常量進(jìn)行修改。
6.援用專題
援用在c++內(nèi)部是1個(gè)常量指針。
type &name <---> type * const name;
援用的本質(zhì)是c++編譯器幫我們做了1個(gè)取地址的操作。
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
}
void swap2(int *a, int *b)
{
int c = 0;
c = *a;
*a = *b;
*b = c;
}
//援用和左值進(jìn)行綁定的時(shí)候
void main()
{
int a1 = 10; int b1 = 20;
swap(a1, b1);
printf("a1:%d, b1:%d", a1, b1);
system("pause");
}
7.函數(shù)增強(qiáng)
7.1內(nèi)聯(lián)函數(shù)
內(nèi)聯(lián)函數(shù)的函數(shù)體需要和實(shí)現(xiàn)寫在1起,不能單獨(dú)聲明。
代替帶參數(shù)的宏,避免宏的副作用。
#include "iostream"
using namespace std;
#define MYFUNC(a, b) ((a) < (b) ? (a) : (b))
//inline要求關(guān)鍵字, 內(nèi)聯(lián)編譯
//內(nèi)聯(lián)函數(shù)的函數(shù)體,需要和實(shí)現(xiàn)寫在1塊
inline int myfunc(int a, int b)
{
return a < b ? a : b;
}
int main()
{
int a = 1;
int b = 3;
//int c = myfunc(++a, b);
int c = MYFUNC(++a, b);//===> ((++a) < (b) ? (++a) : (b));
printf("a = %d
", a); //3
printf("b = %d
", b);//3
printf("c = %d
", c); //3
system("pause");
return 0;
}
7.2函數(shù)重載
函數(shù)重載與函數(shù)指針的結(jié)合;可以添加斷電,選擇逐語句履行,視察運(yùn)行效果。
#include "iostream"
using namespace std;
int func(int x) // int(int a)
{
return x;
}
int func(int a, int b)
{
return a + b;
}
int func(const char* s)
{
return strlen(s);
}
//定義了1個(gè) 指針 類型 (指向函數(shù)的指針類型)
typedef int(*PFUNC)(int a); // int(int a)
void main()
{
PFUNC p = func;
int c = p(1);
printf("c = %d
", c);
system("pause");
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈