C 中使用類(過載,友元函式,轉換函式等)

2021-06-08 12:52:30 字數 3837 閱讀 6198

12點半了。好久沒更新c++博文了。把乙個章節看完了。接下來說下c++裡的操作符過載和以後的內容。時間晚了,可能打字沒打好。望大家見諒。

c++中有個operator操作符概念。如果想過載+運算子,那麼需要寫成operator+()。一般兩個數相加是這麼呼叫的:

[cpp]view plain

copy

print?

a = b+c; == a = b.operator+(c);  

當呼叫操作符,會有乙個隱式的呼叫。把自己的物件作為操作符的物件。然後顯示呼叫引數c。

重點介紹友元函式。因為過載了運算子後可能出現類似:

[cpp]view plain

copy

print?

a = 1.5 + c;  

此時按照上面的說法。1.5不是乙個物件。無法完成操作,這時候就需要友元函式了,其實友元函式就是類的非成員函式,不會隱式的將自身物件也作為引數。

然後貼三段**,注釋寫的還算詳細。大家可以複製後去執行下看下結果。**是從c++primer plus裡抄過來的。

mytime0.h

[cpp]view plain

copy

print?

#ifndef mytime0_h_

#define mytime0_h_

#include 

class

time  

//內聯函式,呼叫該類的成員函式,這裡也就是上面那個函式。

//  友元函式。對於那些非成員過載操作符函式來說,操作符左面的運算元對應函式的第乙個引數。類似 c = a+b 類似於 c = operator+(a,b)

friend

std::ostream & operator<<(std::ostream & os, 

const

time & t);  

};  

#endif

mytime0.cpp

[cpp]view plain

copy

print?

#include 

#include "mytime0.h"

time::time()  

time::time(int

h, int

m)  

void

time::addmin(

intm)  

void

time::addhr(

inth)  

void

time::reset(

inth, 

intm)  

time time::sum(const

time & t)

const

// 過載加號運算子的版本

time time::operator+(const

time & t)

const

time time::operator-(const

time & t)

const

time time::operator*(double

n)const

std::ostream & operator<<(std::ostream & os, const

time & t)  

void

time::show()

const

#include 

#include "mytime0.h"

intmain()    

然後接下來說下類的強制轉換和自動轉換。

類的自動轉換就是,如果你定義了乙個只接受乙個引數的建構函式,那麼在呼叫建構函式時,如果碰到匹配的引數,直接可以將乙個基本型別轉換為相應的類型別。其中有幾種隱式轉換的情況。

1.將某個物件初始化為某個值時。

2.將某個值賦值某個物件。

3.將某值傳遞給接收某物件引數的函式時。

4.返回值被宣告為某個物件的函式返回乙個某值時。

5.以上情況中,如果另乙個值可以轉化為某值時。

使用explicit關鍵字可以強制使用顯式建構函式,可以避免把某值轉化為某物件。

如果想進行相反的轉換,那麼就需要使用c++操作符函式-------轉換函式。轉換函式是使用者自定義的強制型別轉換。使用轉換函式得注意以下三點:

1.轉換函式必須是類方法。

2.轉換函式不能指定返回型別。

3.轉換函式不能有引數。

同樣的,也上三段**。

stonewt.h

[cpp]view plain

copy

print?

#ifndef stonewt_h_

#define stonewt_h_

class

stonewt  

;  int

stone;  

double

pds_left;  

double

pounds;  

public

:  stonewt(double

lbs);  

stonewt(int

stn, 

double

lbs);  

stonewt();  

~stonewt();  

void

show_lbs()

const

;  void

show_stn()

const

;  // 轉換函式,將物件轉換為內建型別

operator int

()const

;  operator double

()const

;  };  

#endif

stonewt.cpp

[cpp]view plain

copy

print?

#include 

using

std::cout;  

#include "stonewt.h"

stonewt::stonewt(double

lbs)  

stonewt::stonewt(int

stn, 

double

lbs)  

stonewt::stonewt()  

stonewt::~stonewt()  

void

stonewt::show_stn()

const

void

stonewt::show_lbs()

const

// 轉換方法 不帶引數,不帶返回值型別

stonewt::operator int

() const

stonewt::operator double

() const  

stone.cpp

[cpp]view plain

copy

print?

#include 

using

std::cout;  

#include "stonewt.h"

void

display(

const

stonewt st, 

intn);  

intmain()  

void

display(

const

stonewt st, 

intn)  

}  好了,寫完就要1點了。準備休息,週末繼續更新。

C 中使用類(過載,友元函式,轉換函式等)

12點半了。好久沒更新c 博文了。把乙個章節看完了。接下來說下c 裡的操作符過載和以後的內容。時間晚了,可能打字沒打好。望大家見諒。c 中有個operator操作符概念。如果想過載 運算子,那麼需要寫成operator 一般兩個數相加是這麼呼叫的 cpp a b c a b.operator c 當...

C 友元函式 友元類 友元成員函式

眾所周知,c 控制對類物件私有部分的訪問。通常,公有類方法提供唯一的訪問途徑,但是有時候這種限制太嚴格,以至於不適合特定的程式設計問題。在這種情況下,c 提供了另外一種形式的訪問許可權 友元,友元有3種 友元函式 友元類 友元成員函式。通過讓函式成為類的友元 即 友元函式 可以賦予該函式與類的成員函...

c 內聯函式 友元函式 友元類 運算子過載

短小 頻繁,且沒有迴圈體 的功能 1.內聯函式 特點 1.在函式名前加inline關鍵字 2.以空間換取時間,編譯階段整個函式體替換呼叫部分 2.巨集定義 1.對變數巨集定義 define 巨集名 值 2.帶參函式的巨集定義 define 函式名 形參表 不需要型別 函式體 1.整體思維 2.不進行...