クラスのメンバ関数のプロトタイプ宣言の戻り値の型と関数名の間に クラスの型にスコープ解決演算子::がくっついたものが挟み込まれたものがクラスのメンバ関数の定義に用いられます えっ?もう知ってるって?

マックス「快調そのもの


順調な感じじゃないか😊」



🌞   🌞   🌞   🌞   🌞   🌞   🌞



          よかったね😊


           それでは



クラスPointのメンバ関数operator+関数の定義

👇


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}


がどのような仕組みになっているかを


みていきましょう。


🌞   🌞   🌞   🌞   🌞   🌞   🌞


マックス「もう これは簡単だろ~う


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}



1番左端のPointは


なぜ

Pointになっているのかというと・・・」


int(イント)「きゃ~ さすがね マックス」


マックス「なぜ


1番左端のPointは


Pointになっているのかというと・・・




    なんでだ・・・



そもそも


Point Point::


となんで


Pointが2つならんでいるんだ?・・・



仕方がないようだな。



          『出でよ』



         『int(イント)ォォォォォ』

                         」


int(イント)int(イント)int(イント)おおおおおおお!」



int(イント)「もう! でてる」



マックス「どうだ int(イント)


なぜ


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}



1番左端は


Pointになっているんだ」



int(イント)「へ?


ええ?  


わたし~


簡単じゃなかったのぉ?」


ソーラー「この問題はとても簡単じゃないかな(´▽`*)


一番左端が


なぜPointになっているかだけでなく


なぜ


Point Point::operator+(Point p)



記述されているのかまでわかるよ。」


マックス「な、なんで?」


ソーラー「


だって


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}



     クラスPointのメンバ関数operator+関数の定義


なんだよ。」



マックス「


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}



クラスPointのメンバ関数operator+関数の定義であることと


Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}


Point Point::operator+(Point p)



なぜ


このように


Point Point::operator+(Point p)


と記述されるのか何か関係があるのか?」


ソーラー「


クラスPointのメンバ関数operator+関数の定義

👇

Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}

👆

が記述してありますが


今のプログラムのPointのクラス宣言を見れば


みなさん お分かりになられますように👉」

👇


#include <iostream>


using namespace std;


class Point{


public:

int x;

int y;


public:


void pointdisplay();


public:


Point operator+(Point p);


};


void Point::pointdisplay() {


cout << x << "\n";

cout << y << "\n";

}



Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}




int main() {



Point p1;


p1.x = 1;

p1.y = 1;

p1.pointdisplay();



Point p2;


p2.x = 2;

p2.y = 2;

p2.pointdisplay();


Point p3;


p3 = p1 + p2;

p3.pointdisplay();


return 0;

}


プログラムの実行結果


1

1

2

2

3



ソーラー「👉


クラスPointのメンバ関数operator+関数の


プロトタイプ宣言


Point operator+(Point p);


が実行されています。


このプロトタイプ宣言


Point operator+(Point p);


の形が



なぜ



クラスPointのメンバ関数operator+関数の定義

👇

Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}

👆



Point Point::operator+(Point p)


が記述されているかに


関わってきます」


マックス「う・・・んん なんかわかりそうな・・」


ソーラー「クラスPointのメンバ関数である関数operator+のプロトタイプ宣言


Point operator+(Point p);



クラスPointのメンバ関数であるoperator+関数の定義の


Point Point::operator+(Point p)



よ~く


見比べてみてよ」


マックス「もしかして


クラスPointのメンバ関数である関数operator+の定義の

👇


Point Point::operator+(Point p)


👆この点々の部分は


クラスPointのメンバ関数である関数operator+のプロトタイプ宣言


Point operator+(Point p);



Point operator+(Point p)


に対応しているんじゃないか?


Point Point::operator+(Point p)



1番左側のPointはいまいちなんなのかわからんが•••


Pointにスコープ解決演算子::がくっついた


Point::



operator+(Point p)がくっついている


Point::operator+(Point p)



クラスPointのメンバ関数operator+を表しているんじゃないかなんじゃないか


operator+のクラスPointのメンバ関数宣言


Point operator+(Point p);



Point



戻り値の型か?


Point operator+(Point p)



戻り値の型がPointの


クラスPointのメンバ関数operator+


ってことになるのか



クラスPointのメンバ関数である関数operator+の定義の

👇


Point Point::operator+(Point p)


👆この点々の部分は


クラスPointのメンバ関数である戻り値の型がPointの関数operator+のプロトタイプ宣言


Point operator+(Point p);



Point



operator+(Point p)


に対応しているってことにならないか?」




ソーラー「おおう


そうですね


Point💖💖💖💖💖 operator+(Point p);

戻り値の型が


Point💖💖💖💖💖


operator+関数のクラスPoint型のメンバ関数宣言(プロトタイプ宣言)をおこなっている・・・か・・・




クラスPointのメンバ関数operator+関数の定義で

👇

Point Point::operator+(Point p)

{

Pointdisplay a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}

👆



クラスPointのメンバ関数である関数operator+のプロトタイプ宣言


Point💖💖💖💖💖 operator+(Point p)😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊



Point💖💖💖💖💖 Point::operator+(Point p)😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊


の一部に


用いられているってことだよね



Pointにスコープ解決演算子::がくっついた


Point::



operator+(Point p)


にくっついていることにより


       💖クラスPointの💖


メンバ関数operator+関数の定義がおこなわれているということが


わかるんだね


Point::



この関数はクラスPointのメンバ関数であるということを


はっきり示すために


つけられているんだ」



🌞   🌞   🌞   🌞   🌞   🌞   🌞



      みんな


      そうなんで~す


      なぜ


クラスPointのメンバ関数operator+関数の定義


👇

Point Point::operator+(Point p)

{

Point a;


a.x = x + p.x;

a.y = y + p.y;


return a;

}

👆


内で


Point Point::operator+(Point p)



記述されているのか?


わかりにくいな と思ったかたも多いいんじゃないかな?


なぜ


Point Point::operator+(Pointdisplay p)


が記述されているのか


それは


            🦚単純に🦚


クラスのメンバ関数の定義を記述する形式だからなんです


そのことをわかりやすく説明するために


以前登場した


クラスCubeのクラス宣言と


クラスCubeのメンバ関数であるdisplay()


の定義をみてね。

👇

class Cube {


public:


int no; //変数noはクラスCubeのメンバ変数とよばれます

float tate; //変数tateはクラスCubeのメンバ変数とよばれます

float yoko; //変数yokoはクラスCubeのメンバ変数とよばれます

float takasa; //変数takasaはクラスCubeのメンバ変数とよばれます


void display(); //自作関数display()はクラスCubeのメンバ関数とよばれます


};


void Cube::display() {


cout << "立方体につけられたナンバーは" << no << "です" << "\n";

cout << "立方体の縦の長さは" << tate << "です" << "\n";

cout << "立方体の横の長さは" << yoko << "です" << "\n";

cout << "立方体の高さの長さは" << takasa << "です" << "\n";


}


//👆クラスCubeのメンバ関数となっている自作関数display()の定義をおこなっています



🌞   🌞   🌞   🌞   🌞   🌞   🌞




マックス「そういえば・・・」



🌞   🌞   🌞   🌞   🌞   🌞   🌞

           


どう?  思い出した?


クラスCube のクラス宣言と


そのメンバ関数であるdisplay()


の定義をよ~くみると・・・


🌞   🌞   🌞   🌞   🌞   🌞   🌞



マックス「もしやあ」



🌞   🌞   🌞   🌞   🌞   🌞   🌞


      きゃはっ


クラスCubeのメンバ関数宣言


void display();



クラスCubeのメンバ関数となっている自作関数display()の定義

👇

void Cube::display() {


cout << "立方体につけられたナンバーは" << no << "です" << "\n";

cout << "立方体の縦の長さは" << tate << "です" << "\n";

cout << "立方体の横の長さは" << yoko << "です" << "\n";

cout << "立方体の高さの長さは" << takasa << "です" << "\n";


}

👆



void Cube::display()



みても


おわかりになられますように・・・


🌞   🌞   🌞   🌞   🌞   🌞   🌞



マックス「これは もう わかりそうだ・・・」



🌞   🌞   🌞   🌞   🌞   🌞   🌞


display()のクラスCubeのメンバ関数宣言


つまり


クラスCubeのメンバ関数display()のプロトタイプ宣言である


void display();



             void(戻り値の型💖💖💖💖💖)


                と


                display()


の間に


Cubeにスコープ解決演算子::がくっついた


Cube::


が挟み込まれたもの

👇

void Cube::display()



クラスCubeのメンバ関数display()の定義


で用いられています。


cube::はdisplay()



クラスCubeのメンバ関数であることを示すためにつけられています





マックス「なにぃ


そういえばそうだ


             void(戻り値の型💖💖💖💖💖)


                と


                display()


の間に


cube::



つけられていた


voidは戻り値の型か!


じゃあ


やはり


Point Point::operator+(Point p)



1番左側のPointは


戻り値の型というわけだ


だが


なぜ


戻り値を格納する型がPointになるのかはよくわからないな



ソーラー「そう


voidは戻り値の型です


同様に


Point Point::operator+(Point p)



1番左側のPointは


戻り値の型なんです


Cube::



この定義


👇

void Cube::display() {


cout << "立方体につけられたナンバーは" << no << "です" << "\n";

cout << "立方体の縦の長さは" << tate << "です" << "\n";

cout << "立方体の横の長さは" << yoko << "です" << "\n";

cout << "立方体の高さの長さは" << takasa << "です" << "\n";


}

👆




クラスCubeのメンバ関数display()の定義であることを


はっきり示すために付け加えられています。


つまり


クラスCubeのメンバ関数display()のプロトタイプ宣言


void display();



             💖戻り値の型💖void



              💖関数名💖 display()


          🌞🌞🌞🌞🌞あいだに🌞🌞🌞🌞🌞


クラス名Cubeにスコープ解決演算子::がくっついた


cube::が


挟み込まれたものが


クラスCubeのメンバ関数の定義に用いられるんです


ですので


同様に


クラスPointのメンバ関数operator+のプロトタイプ宣言が


Point operator+(Point p);


となっていると


クラスPointのメンバ関数であるoperator+の定義で



🌞Point(戻り値を格納する型)🌞👈(なぜ戻り値を格納する型がPointになるのかは後に説明されます😊)



🌞operator+(Point p)(関数名)🌞



Point::が挟み込まれた



Point Point::operator+(Point p)


が記述されることになるというわけなんです



このことは


すでに


今までやってきたことなんだよ~ん」



マックス「じゃあ ちょちょっと簡単じゃないか



わかっていたことだったな


int(イント)よ」


int(イント)「めでたし、めでたし」






  • Twitterで共有
  • Facebookで共有
  • はてなブックマークでブックマーク

作者を応援しよう!

ハートをクリックで、簡単に応援の気持ちを伝えられます。(ログインが必要です)

応援したユーザー

応援すると応援コメントも書けます

新規登録で充実の読書を

マイページ
読書の状況から作品を自動で分類して簡単に管理できる
小説の未読話数がひと目でわかり前回の続きから読める
フォローしたユーザーの活動を追える
通知
小説の更新や作者の新作の情報を受け取れる
閲覧履歴
以前読んだ小説が一覧で見つけやすい
新規ユーザー登録無料

アカウントをお持ちの方はログイン

カクヨムで可能な読書体験をくわしく知る