一.項目一
1.頭文件.h
//A.h
#pragma once
//防止頭文件被重復包含(重復包含會被重復編譯,也就是該類會被重復定義)
#ifndef HEAD_H
//等價于( #if !defined(HEAD_H) )
//defined是一個預處理操作符,相當于一個表達式
#define HEAD_H
class A
{
public:static void f(A a);static void show();//構造函數//只能在初始化列表初始化常數據成員A(int x, int conx) :x(x), conx(conx) { x2++; }
private:int x;static int x2;const int conx;
};
//必須有結束指令
#endif
//Clock.h
#pragma once
#include<string>
using namespace std;
class Clock
{
public:Clock();void setTime(int newH, int newM, int newS, string newN);void showTime() const;//友元類friend class Point;
private:int hour, minute, second;string name;//類的靜態常量如果有整數類型或枚舉類型,可以在定義中指定常量值(不分配內存,只是簡單替換)static const int b = 10;
};
//內聯函數不用分配內存(定義在頭文件中)
inline int add(int a, int b) {return a + b;
}
//Point.h
#pragma once
#include"Clock.h"
class Point
{
public:Point(int x = 0, int y = 0) :x(x), y(y) {}int getX() const{ return x; }int getY() const{ return y; }//友元函數friend float dist(Point& p1, Point& p2);//作為Clock的友元類,可以訪問其私有成員void show_Clock(Clock& c);
private:int x, y;
};
2.源文件.cpp
//A.cpp
#include "A.h"
#include<iostream>
using namespace std;//外部變量只能放在.cpp下,不能在.h下(定義型聲明)(文件作用域)
extern int j = 20;//外部函數(不聲明,文件作用域)
void showing(){cout << "Hello Extern Function!--NOT--DEFINE" << endl;
}
//外部函數(聲明為全局)
extern void showed() {cout << "Hello Extern Function!--DEFINE" << endl;
}
//靜態函數(不能被其他編譯單元引用)(靜態生存期)
void static shows() {cout << "Hello Static Function!" << endl;
}//必須在類外使用類名進行定義式聲明(初始化)
int A::x2 = 0;void A::f(A a) {// cout << x; //不能在靜態成員函數中直接訪問非靜態成員,必須與特定成員相對cout << "a.x:" << a.x << endl;cout << "x2:" << x2 << endl; // 可以直接訪問靜態成員}void A::show() {cout << "x2:" << x2 << endl;
}
//Clock.cpp
#include "Clock.h"
#include<iostream>
#include<string>
using namespace std;
//文件作用域(沒有聲明,但是在main函數中引用性聲明,值也相同)
int i = 10;
Clock::Clock() :hour(0), minute(0), second(0) {cout << "調用構造函數" << endl;
}void Clock::setTime(int newH, int newM, int newS, string newN) {name = newN;hour = newH;minute = newM;second = newS;
}
void Clock::showTime() const {cout << hour << ":" << minute << ":" << second << endl;cout << name << endl;
}
//Point.cpp
#include "Point.h"
#include<iostream>
#include<cmath>
using namespace std;
//友元函數實現
float dist(Point& p1, Point& p2) {double x = p1.x - p2.x;double y = p1.y - p2.y;//static_cast是一種類型轉換運算符,用于在編譯時進行類型轉換return static_cast<float>(sqrt(x * x + y * y));
}void Point::show_Clock(Clock& c) {cout << "This is showed by Point" << this->x << " " << this->y << endl;cout << c.name << endl;
}
3.主函數.cpp
//main.cpp
#include"Clock.h"
#include"A.h"
#include"Point.h"
#include<iostream>
#include<string>
using namespace std;
// 定義未指定初值的基本類型靜態生存期變量,會被以0值初始化
// 聲明具有靜態生存期的對象
Clock globalClock;int main() {//生存期cout << "生存期" << endl;cout << "First:" << endl;globalClock.showTime();globalClock.setTime(8, 30, 30, "theclock");Clock myClock(globalClock);cout << "Second:" << endl;myClock.showTime();//靜態成員 A a1(100,100);A a2(99,100);//調用靜態成員函數cout << "調用靜態成員函數" << endl;A::f(a1);A::f(a2);A::show();//友元函數cout << "友元函數" << endl;Point myp1(1, 1), myp2(4, 5);cout << "The distance is:" << endl;cout << dist(myp1, myp2);//友元類//友元關系:不可傳遞,單向,不被繼承//友元函數不僅可以是普通的函數,也可以是另外一個類的成員函數cout << "通過友元類輸出私有類成員:" << endl;myp1.show_Clock(myClock);//調用內聯函數cout << "4 + 5 = " << add(4, 5) << endl;//外部變量extern定義(引用性聲明)extern int i;cout << "外部變量 i = " << i << endl;//在源文件中聲明后還要再聲明,此時值一致extern int j;cout << "外部變量 j = " << j << endl;//外部函數(使用要包含文件+函數聲明)void showing();//extern void showing();//也可showing();void showed();showed();//不能訪問其他編譯單元靜態函數//void shows();//shows();return 0;
}
4.輸出
調用構造函數
生存期
First:
0:0:0Second:
8:30:30
theclock
調用靜態成員函數
a.x:100
x2:2
a.x:99
x2:2
x2:2
友元函數
The distance is:
5通過友元類輸出私有類成員:
This is showed by Point1 1
theclock
4 + 5 = 9
外部變量 i = 10
外部變量 j = 20
Hello Extern Function!--NOT--DEFINE
Hello Extern Function!--DEFINE
二.個人銀行賬戶管理系統
(主要添加靜態數據成員static,常成員const,條件預編譯,并且實現頭文件源文件文件分離)
1.頭文件.h
//account.h
#pragma once
//條件預編譯指令
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount
{
private:int id; //賬號double balance; //余額double rate; //存款的年利率int lastDate; //上次變更余額的時期double accumulation; //余額按日累加之和static double total; //所有賬戶總余額//記錄一筆賬, date為日期, amount為金額, desc為說明void record(int date, double amount);//獲得到指定日期為止的存款金額按日累積值double accumulate(int date) const {return accumulation + balance * (date - lastDate);}
public:SavingsAccount(int date, int id, double rate); //構造函數int getId() const { return id; }double getBalance() const { return balance; }double getRate() const { return rate; }static double getTotal() { return total; }void deposit(int date, double amount); //存入現金void withdraw(int date, double amount); //取出現金//結算利息,每年1月 1日調用一次該函數void settle(int date);//顯示賬戶信息void show() const;//友元函數friend void show_items(SavingsAccount& s1);//友元類friend class AnotherAccount;
};
#endif
//AnotherAccount.h
#pragma once
#include"account.h"
class AnotherAccount
{
//成員函數可以訪問SavingsAccount的保護和私有成員
private:SavingsAccount S1;SavingsAccount S2;
public:AnotherAccount(SavingsAccount& s1,SavingsAccount& s2):S1(s1),S2(s2) {}void showing() const;
};
2.源文件.cpp
//account.cpp
#include "account.h"
#include<iostream>
using namespace std;// 一定要在類外初始化靜態本地變量,不然會報錯
double SavingsAccount::total = 0;// 友元函數實現
void show_items(SavingsAccount& s1) {cout << "id:" << s1.id << " balance:" << s1.balance << " lastDate:" << s1.lastDate << endl;
}//SavingsAccount類相關成員函數的實現
SavingsAccount::SavingsAccount(int date, int id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount::record(int date, double amount) {accumulation = accumulate(date);lastDate = date;amount = floor(amount * 100 + 0.5) / 100; //保留小數點后兩位(四舍五入)balance += amount;total += amount;cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount::deposit(int date, double amount) {record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {if (amount > getBalance())cout << "Error: not enough money" << endl;elserecord(date, -amount);
}
void SavingsAccount::settle(int date) {double interest = accumulate(date) * rate / 365; //計算年息if (interest != 0)record(date, interest);accumulation = 0;
}
void SavingsAccount::show() const{cout << "#" << id << "\tBalance: " << balance;
}
//AnotherAccount.cpp
#include "AnotherAccount.h"
#include<iostream>
using namespace std;
//訪問SavingsAccount的私有成員
void AnotherAccount::showing() const{if (S1.balance > S2.balance) {cout << "The bigger is:" << S1.id << " balance:" << S1.balance << endl;}else if (S1.balance < S2.balance) {cout<< "The bigger is:" << S2.id << " balance:" << S2.balance << endl;}else {cout << "No one bigger than the other" << endl;}
}
3.主函數.cpp
//5-11.cpp
#include<iostream>
#include"account.h"
#include"AnotherAccount.h"
using namespace std;int main() {//建立幾個賬戶cout << "建立賬戶:" << endl;SavingsAccount sa0(1, 1234567, 0.02);SavingsAccount sa1(5, 7654321, 0.025);//幾筆賬目cout << "記賬:" << endl;sa0.deposit(5, 670000);sa1.deposit(6, 700000);sa0.deposit(60, 5500);sa1.withdraw(70, 4000);//開戶后第100天到了銀行的計息日,結算所有賬戶的年息cout << "結息:" << endl;sa0.settle(100);sa1.settle(100);//輸出各個賬戶信息cout << "輸出各個賬戶信息:" << endl;sa0.show();cout << endl;sa1.show();cout << endl;cout << "利用靜態本地變量const唯一初始化性輸出total:" << endl;cout << "Total:" << SavingsAccount::getTotal() << endl;//利用友元函數輸出賬戶信息cout << "利用友元函數輸出賬戶信息:" << endl;show_items(sa0);show_items(sa1);//友元類cout << "友元類訪問私有成員然后輸出:" << endl;AnotherAccount s1(sa0,sa1);s1.showing();return 0;
}
4.輸出
建立賬戶:
1 #1234567 is created
5 #7654321 is created
記賬:
5 #1234567 670000 670000
6 #7654321 700000 700000
60 #1234567 5500 675500
70 #7654321 -4000 696000
結息:
100 #1234567 3499.73 679000
100 #7654321 4498.63 700499
輸出各個賬戶信息:
#1234567 Balance: 679000
#7654321 Balance: 700499
利用靜態本地變量const唯一初始化性輸出total:
Total:1.3795e+06
利用友元函數輸出賬戶信息:
id:1234567 balance:679000 lastDate:100
id:7654321 balance:700499 lastDate:100
友元類訪問私有成員然后輸出:
The bigger is:7654321 balance:700499
三.其他
1.限定作用域的enum枚舉類
//限定作用域的enum枚舉類
#include<iostream>
#include<string>
using namespace std;
enum color { red, yellow, green };
//enum color1 { red, yellow, green }; //錯誤,枚舉類型不能重復定義
enum class color2 { red, yellow, green }; // 正確 限定作用域的枚舉元素被隱藏
color c = red;
//color2 c1 = red; // color類型的值不能用來初始化color2類型實體
color c2 = color::red; // 顯式的訪問枚舉元素
color2 c3 = color2::red; // 使用color2枚舉元素
struct SColor {enum color { red, yellow, green };
};
SColor::color c = SColor::red; // 正確使用未限定作用域的枚舉元素
2.常成員函數舉例
//常成員函數舉例
#include<iostream>
using namespace std;class R {
public:R(int r1, int r2) :r1(r1), r2(r2) {}void print();void print() const;
private:int r1, r2;
};void R::print() {cout << r1 << "---" << r2 << endl;
}void R::print() const {cout << r1 << "+++" << r2 << endl;
}int main() {R a(5, 4);a.print(); //調用普通函數成員const R b(20, 25);b.print(); //調用常函數成員return 0;
}
//輸出
5---4
20+++25