1. 程式人生 > >C++ 第三天

C++ 第三天

一 、 c++ 給c程式設計師的建議
1.1 儘量的少使用巨集
多使用 列舉 const inline
1.2 變數隨時用隨時定義
保證這些變數可以及時的初始化
1.3 動態記憶體分配時
使用new delete 少使用malloc free
1.4 少使用c風格的字串 多使用string
1.5 少使用強制型別轉化
如果需要進行型別轉換 就使用轉換運算子
1.6 使用面向物件的思想去進行程式設計

二、面向物件程式設計
2.1什麼是物件
一切皆物件
任何物件 都是一個型別
同類型的物件 可接收相同的訊息
物件就是現實中的物體 在計算機中的變數

2.2 類 ----------分類

2.3 分類如何實現
抽取一組物件的共同特徵:
car
顏色
價格
牌子

抽取行為:
void run();
void start();
void stop();

2.4 計算機中如何表達一個型別?
使用結構體描述一個型別
特徵:成員變數
行為:成員函式
Student
特徵:
string name;
int age;
string sno;
行為:
void play(string name);
void learn(string name);

#include <iostream>
using namespace std;
struct Student{
/*特徵*/
string name;
int age;
string sno;
bool gender;
double height;
double weight;
/*行為*/
void play(string name){
cout < < "play" << name << endl;
}
void learn(string name){
cout << "learn" << name << endl;
}
void show(){
cout << "my name is " << name
<< "age is" << age << endl;
}
};

int main(){
Student sa={"xxx", 30 };
sa.play("qwer");
sa.learn("c++");
sa.show();
}

2.5設計一個時間 使用struct描述
特徵:
int hour;
int min;
int sec;
行為:
/*顯示時間*/
void show();
/*呼叫一次代表走一秒*/
void dida();
/*不斷的走秒*/
void run();

#include <iostream>
#include <iomanip>
using namespace std;
struct MyTime{
int hour;
int min;
int sec;
void show(){
cout << setfill('0') << setw(2)
<< hour << ':' << setw(2)
<< min << ':' << setw(2)
<< sec << '\r' << flush;
}

void dida(){
sleep(1);
if(60 == ++ sec){
sec=0;
if(60 == ++min){
min=0;
if(24 == ++hour){
hour=0;
}
}
}
}

void run(){
while(1){
show();
dida();
}
}
};

int main(){
MyTime mytime={11,37,27};
mytime.run();
}

2.6 class 來描述一個型別
struct -----> class
初始化問題 -----> 建構函式
許可權問題 -----> class中的資料預設是私有的 struct預設是公開的
私有的
private: 只能在類內訪問成員
public: 既能類內 也能在類外
protected: 類內和子類中可以訪問
#include <iostream>
using namespace std;
class MyTime{
private:
int hour;
int min;
int sec;
public:
/*為訪問私有資料 公開訪問介面*/
void setTime(int h=0,int m=0,int s=0){
hour= h;
min = m;
sec=s;
}

void dida(){
sleep(1);
if(60 == ++ sec){
sec=0;
if(60 == ++min){
min=0;
if(24 == ++hour){
hour=0;
}
}
}
}

void run(){
while(1){
show();
dida();
}
}
}
int main(){
MyTime mytime;
/*使用公開的介面 給成員變數賦值*/
mytime.setTime(11,17,37);
mytime,run();
}

2.7使用類描述一個日期 Date
特徵:
int year;
int month;
int day;
行為:
設定日期;
顯示日期;

#include <iostream>
using namespace std;
class Date{
public :
void setDate(int y=2014,int m=12,int d=12){
year=y;
month=m;
day=d;
}
void show(){
cout << setfill('0') << setw(4)
<< year << '-' << setw(2)
<< month << '-' << setw(2)
<< day << endl;
}
private:
int year;
int mon;
int day;
};

int main(){
Date date;
date.setDate(2012,12,12);
}

三、建構函式
1.1 特點
構建一個物件時 自動呼叫一次
沒有返回值型別
函式名 和 類名相同
目的是為了初始化物件
#include <iostream>
using namespace std;
class Date{
private:
int year;
int month;
int day;
public:
Date(){
cout << "Date()" << endl;
year=2014;
month=1;
day=1;
}
void show(){
cout << setfill('0') << setw(4)
<< year << '-' << setw(2)
<< month << '-' << setw(2)
<< day << endl;
}
};

int main(){
/*型別的例項化*/
Date date;
Date *pd = new Date();
Date *pd1 = new Date;
/*編譯器會翻譯成函式宣告*/
Date date2();
date.show();
}

1.2 一個物件建立的過程
根據型別大小 分配記憶體
初始化成員變數
基本型別的成員 什麼都不做
類型別的成員 就構造這個成員
呼叫這個了型別對應的建構函式

1.3 建構函式的應用
建構函式 可以有引數 建構函式之間可以形成過載關係。(系統會預設提供一個無參建構函式 但一旦提供建構函式則系統的會自動回收掉)
可以是引用引數的預設值 減少建構函式的個數。但不要引入衝突。

當型別中有const 型別的成員 和引用 型別成員時。這些成員要求在建構函式呼叫之前就初始化。初始化引數列表時唯一的選擇
初始化引數列表 只有建構函式有,建構函式引數表之後 實現體之前。
#include <iostream>
using namespace std;
class A(){
const int x; //const 型別的成員
int y;
int& z; //引用型別成員
public:
/* A():x(100),y(123){
}*/
A(int& z, int x=100,int y=123):z(z),x(x),y(y){//初始化引數列表可以區分誰是引數,誰是成員變數x(x) 第一個為成員變數,第二個為引數
}
void show(){
cout << x << '/' << y << '/' << z << endl;
}
};

int main(){
int p=1;
A a(p);
a.show();
}

四、在實際開發中標頭檔案和實現檔案要分離
4.1先寫標頭檔案
#ifndef 巨集
#define 巨集
變數的宣告
函式的宣告
型別的定義
#endif
4.2 實現檔案
實現全域性函式
實現型別
去掉型別名 和 成員變數 許可權
在每個函式名前加上 類名::
實現函式
4.3 寫測試檔案
使用型別定義物件

寫一個帶參巨集,完成兩個整數取最大值的邏輯
GETMAX(100,200)