プログラムの出力の説明

このQ&Aのポイント
  • プログラムの出力には基本クラスのoperator=()が呼ばれる
  • coordクラスのoperator+()とoperator-()が使われる
  • coordオブジェクトが加算・減算され、象限が表示される
回答を見る
  • ベストアンサー

プログラムの出力の説明を教えてください。

出力が、画像のようになるのですが、最後に基本クラスのoperator=()が呼ばれるのがわからないです。 なぜ、そのようになるのか教えてください。 /* +,-,=をcoordクラスに関してオーバーロードし、 そのcoordをquadの基本クラスとして使用する */ #include <iostream> using namespace std; class coord{ public: int x, y; //座標 coord(int i = 0, int j = 0){ x = i; y = j; } void get_xy(int &i, int &j){ i = x; j = y; } coord operator+(coord ob2); coord operator-(coord ob2); coord operator=(coord ob2); }; //+をcoordクラスに関してオーバーロードする coord coord::operator+(coord ob2) { coord temp; cout << "coord operator+()を使用" << endl; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } //-をcoordクラスに関してオーバーロードする coord coord::operator-(coord ob2) { coord temp; cout << "coord operator-()を使用" << endl; temp.x = x - ob2.x; temp.y = y - ob2.y; } //=をcoordクラスに関してオーバーロードする coord coord::operator=(coord ob2) { cout << "coord operator=()を使用" << endl; x = ob2.x; y = ob2.y; return *this; //代入先のオブジェクトを返す } class quad :public coord{ int quadrant; public: quad(){ x = 0; y = 0; quadrant = 0; } quad(int x, int y) :coord(x, y) { if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; } void showq() { cout << "象限を示す:" << quadrant << endl; } quad operator=(coord ob2); }; quad quad::operator=(coord ob2) { cout << "quad operator=()を使用" << endl; x = ob2.x; y = ob2.y; if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; return *this; } int main() { quad o1(10, 10), o2(15, 3), o3; int x, y; o3 = o1 + o2; //2つのオブジェクトの加算。演算子+()を呼ぶ o3.get_xy(x, y); o3.showq(); cout << "(o1+o2)X:" << x << ",Y:" << y << endl; o3 = o1 - o2; //2つのオブジェクトの減算 o3.get_xy(x, y); o3.showq(); cout << "(o1-o2)X:" << x << ",Y:" << y << endl; o3 = o1; //オブジェクトを代入する o3.get_xy(x, y); o3.showq(); cout << "(o3=o1)X:" << x << ",Y:" << y << endl; return 0; }

  • RJMS
  • お礼率70% (58/82)

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

> 最後に基本クラスのoperator=()が呼ばれる サブクラスquadに quad oprator=( quad ) がないので、ディフォールトの operator= が使われることになる。その中で基底クラスのoperator=を呼び出している(さらにつづけて基底クラスのフィールドの代入が行われる) ということです。

RJMS
質問者

お礼

ありがとうございました。 よくわかりました。 回答していただきありがとうございました。

関連するQ&A

  • C++について教えてください。(初心者です)

    現在C++についての学習を進めているのですが、 2項演算子のオーバーロードで理解できないところがありますので、よろしかったらご教授ください。 //+をcoordクラスに対してオーバーロードする #include<iostream> using namespace std; class coord{ int x,y; public: coord() {x=0;y=0;} coord(int i,int j) {x=i;y=j;} void get_xy(int &i,int &j) {i=x;j=y;} coord operator+(coord ob2); }; //+をcoordクラスに対してオーバーロードする coord coord::operator+(coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } int main() { coord o1(10,10),o2(5,3),o3; int x,y; o3 = o1 + o2; o3.get_xy(x,y); cout << "(o1+o2) X:" << x << ",Y:" << y << endl; return 0; } この文の中で、o3.get_xy(x,y);というコードがありますが、 ここの部分がよくわからないのです。 そもそも、引数としてx,yがありますが、これはprivateメンバを 見に行きなさい。っと言っているのでしょうか? main()の中から直接使っている?? それとも?? すいません。この辺の理解が薄いようなので、2項演算子のオーバーロードとは関係ないかもしれませんが、教えてください。 よろしくお願いします。

  • C++超初心者質問

    Visual C++ 2008 Express Edition Ver9.0.30729.1 SP を利用し、以下のプログラムを実行するための手順を教えてください。 Visual C++ 6.0では、ファイル>新規作成→ファイルTAB>C++ソースファイルのように、上記の場合はどのように進んだらよいのでしょう? ファイル>新規作成>プロジェクト→Win32 コンソール アプリケーション??? とすると、↓のエラーが出てしまいます。尚、VC2008をインストールしてから何も触っていません。 http://okwave.jp/qa4665322.html ソース #include<iostream> using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int i,int j){x=i,y=j;} void get_xy(int &i,int &j){i=x;j=y;} friend coord operator+(coord ob1,int i); friend coord operator+(int i,coord ob1); }; coord operator+(coord ob1,int i){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } coord operator+(int i,coord ob1){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } int main(){ char c[3]; // 画面固定のため coord o1(10,10); int x,y; o1=o1+10;; o1.get_xy(x,y); cout<<"(o1+10)X:"<<x<<",Y:"<<y<<"\n"; o1=99+o1;; o1.get_xy(x,y); cout<<"(99+o1)X:"<<x<<",Y:"<<y<<"\n"; cout<<"\nエンターで抜けます"<<endl; // 画面固定のため gets(c); return 0; }

  • 派生クラスから基本クラスprotectedメンバへのアクセスについて

    C++初心者です。 以下のような基本クラスcoordと、その派生クラスquadがあり、quadクラスに関して=演算子をオーバーロードしました。 class coord { protected: int x, y; }; class quad : public coord { public: quad operator=(coord &ob2); }; quad quad::operator=(coord &ob2) { x = ob2.x; y = ob2.y; } するとob2からxにアクセスできないとエラーが出ます。 quadクラスのメンバ関数内なので、基本クラスのprotectedメンバにはアクセスできると思ったのですが、違うのでしょうか。 ご教授お願いします。

  • Yesならこっちへ、NOならあっちへ(c++)

    取り組んでる課題で、Yとタイプしたら1のステップにいけて、Nとタイプしたら2のステップに行くというところでわからなくなってしまいました。Userにそういう選択させるのには何をどう書いたらいいのですか? --------------------------------------------------------------------- 問題:1から100までの整数をUserに選ばせ、Userに、「選んだ数字はXX以上ですか?」と質問を繰り返し、最後にUserの選んだ数字を当てるという課題です。(Userはそれに対してYes/Noでしか答えられません。) --------------------------------------------------------------------- int max=100; int min=0; int mid, x; int systemtype=y, sytemtype=n; main(){ while(1){ cout<<"1から100までで好きな数字を選んでね。"; cin>> x; if (n<=0 || n>=100){ break; } if (mid == (max + min)/2){ cout<<"選んだ数字は :" << mid << " より大きい? "<< endl; cout<<" y は YES, n は NO :" << endl; } else if(min == max){ cout<<"その数字は" << x <<endl; break; } else if(min == mid){ mid += ( max - mid)/2; cout<<"あなたの選んだ数は :" << mid <<" より大きい? "<<endl; } else if(max == mid){ mid -= (mid - min)/2; cout<<"あなたの選んだ数は:" << mid << " より大きい? "<< endl; } else cout<<"その数字は :"<< n <<endl; } return 0; }

  • プログラムのどこがエラーか教えてください(C++)

    問題は、buildingという基本クラスを作って、建物の階数、部屋数、床の総面積を格納するクラスを作成し、次にhouseという派生クラスを作り、buildingを継承し、寝室数と、浴室数を格納するクラスで、officeという派生クラスを作り、これもbuildingを継承し、消火器台数と、電話台数を格納するクラスを作成し、それぞれの結果を出力せよ。っていう問題です。 下に僕が作成したプログラムを書きます。雑なので見にくいかもしれませんがお願いします。 #include <iostream> using namespace std; class building{ int Floor,Room,Area; public: building(int f,int r,int a){ Floor=f; Room=r; Area=a; } void get_FRA(int &f,int &r,int &a){ f=Floor; Room=r; Area=a; } }; class house:public building{ int Bedroom,Bathroom; public: house(int f,int r,int a,int Be,int Ba):building(f,r,a){ Bedroom=Be; Bathroom=Ba; } void Hshow(); }; class office:public building{ int FireExtinguisher,Telephone; public: office(int f,int r,int a,int fe,int t):building(f,r,a){ FireExtinguisher=fe; Telephone=t; } void Oshow(); }; void house::Hshow() { int x,y,z; get_FRA(x,y,z); cout << "階数:" << x << "階建て" << endl; cout << "部屋数:" << y << "室" << endl; cout << "床の総面積:" << z << "m2" << endl; cout << "寝室:" << Bedroom << "室" << endl; cout <<"浴室:" << Bathroom << "室" << endl; } void office::Oshow() { int x,y,z; get_FRA(x,y,z); cout << "階数:" << x << "階建て" << endl; cout << "部屋数:" << y << "室" << endl; cout << "床の総面積:" << z << "m2"<< endl; cout << "消火器の数:" << FireExtinguisher << "個" << endl; cout << "電話の数:" << Telephone << "個" << endl; } int main() { house H_ob(5,2,300,3,2); office O_ob(3,5,500,2,8); H_ob.Hshow(); O_ob.Oshow(); return 0; } あと、プログラムの処理結果を貼っておきます。 できれば、詳しく教えていただければ幸いです。

  • VC++でプログラムの勉強をしています。

    プログラムは最近はじめたばかりです。While文とif文を使ってクイズを作ってみたところ、一個目のsinで入力を求めているところから無限ループになってしまいました。色々調べてcin.cler()とsin.ignore()を入れたりもしてみましたが上手くいきませんでした。どこを間違えているのでしょうか? //クイズ #include <iostream> using namespace std; int main()//cin.clear();cin.ignore();???? { int a; int b; while(1) { cout<<"ネコ型のロボットが出てくるアニメといえば?"<<endl; cout<<"A)ドラえもん B)ドラエもん C)ほりえもん D)サザエさん"<<endl; cin>>a; if(a=='A') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){break;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} if(b!='Y'||'N'){cout<<"正しく入力してね!"<<endl;} } if(a=='B'||'C'||'D') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){cout<<"残念!!"<<endl;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} } if(a!='A'||'B'||'C'||'D'){cout<<"正しく入力してね!"<<endl;} } cout<<"正解!!"<<endl; }

  • operator * について

    掛け算ではない方の operator * についての質問です。 下のソースを見てください。関数func()内で、(*this)[idx]というやり方と、直接operator [] を呼び出すやり方を試しています。(*this)[idx]は自分のコピーを作ってしまわないでしょうか?このようなやり方は正しいでしょうか。というのが第1の質問です。 さらに、CTestで operator * を定義していますが、(*this)[idx]で呼び出されません。なぜなのでしょうか。というのが第2の質問です。 どうぞよろしくお願いいたします。 class CTest : public vector<int> { public: CTest() : vector<int>() { push_back(1); push_back(2); push_back(3); } void func() { #if 1 // 質問1 この書き方をしてもコピーが作られないかどうか? cout<<(*this)[0]<<endl; cout<<(*this)[1]<<endl; cout<<(*this)[2]<<endl; #else cout<<operator[](0)<<endl; cout<<operator[](1)<<endl; cout<<operator[](2)<<endl; #endif } CTest& operator * () { // 質問2 なぜこのオペレータが呼び出されないのか cout<<"???"<<endl; return *this; } }; void main(void) { CTest inst; inst.func(); }

  • 下記、プログラム内の「char *」の役割

    C++初心者です。 縦長になってしまいますが、 『 #include <iostream.h> void show(int); void show(double); void show(char *);   ←左記の記述の使い方 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char *z) { cout << z << endl; } 』 のプログラムにおいて、「char *」の使い方がいまいち理解できません。 上記プログラムですとエラーが表示されないのですが、下記のプログラムだとエラーが発生します。 『 #include <iostream.h> void show(int); void show(double); void show(char);   //←---------上記と違う行 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char z) {  //←---------上記と違う行 cout << z << endl; } 』 なぜ、ポインタ(*)を付けないといけないのか分かりやすく教えていただけましょうか。

  • +演算子オーバーロード

    こんにちは。お世話になっております。 // +演算子オーバーロード CPoint CPoint::operator+(CPoint& obj) //~(1) { CPoint tmp; tmp.m_x = m_x + obj.m_x; tmp.m_y = m_y + obj.m_y; return tmp; } int main() { CPoint point1( 100, 150 ); CPoint point2( 200, 50 ); std::cout << "x:" << point1.getx() << "y:" << point1.gety() << std::endl; point1 = point1 + point2; // オーバーロードされた+演算子が呼び出される std::cout << "x:" << point1.getx() << "y:" << point1.gety() << std::endl; point1 += point2; // オーバーロードされた+=演算子が呼び出される std::cout << "x:" << point1.getx() << "y:" << point1.gety() << std::endl; return 0; } 某サイトで上のようなサンプルプログラムがあるのですが これはc++で書かれた「+演算子オーバーロード」の定義で、動作としては 「point1 = point1.operator+( point2 ); // point1 = point1 + point2; と同じ」というような動作です。 それで疑問が出てきたのですが、イコールの右側で足す数が↓のような3つの場合、ans = a + b + c;です。 これだと(1)のところの引数を2つをとる関数を別に作らないとだめでしょうか?それとも、ans = ((a + b) + c);というふうに優先順位で自動的に計算してくれる+演算子オーバーロードのプログラムを教えてくれませんか?↑式のカッコは便宜上付けただけで、出来ればans = a + b + c;だけで計算出来るプログラムを教えてください。

  • C言語 プログラム 確率

    今、学校の授業で病気の感染プログラムを作っています。 内容は、 ・3カテゴリー(0:健康体、1:感染者、2:免疫体)の人間がいる。 ・2カテゴリー(0:病原体を持たない蚊、1:病原体ウイルスを持つ蚊)の蚊がいる。 ・人間と蚊がxy座標上をある速度でランダムに移動している。 ・健康体の人間とウイルスを持った蚊と人間ある一定距離内にいると蚊が人間を刺して病気が感染し、人間が0:健康体から1:感染者に変化する。 簡単に言いましたが、このようなプログラムを作っています。 その中で、人間に感染する部分のプログラムを下に載せます。 double c0x, c0y, ax, ay, bx, by; for(i=0; i<N;++i){ if(b[i].category==1){ c0x=(*cat0ka).coord.x; c0y=(*cat0ka).coord.y; ax=a[i].coord.x; ay=a[i].coord.y; bx=b[i].coord.x; by=b[i].coord.y; if(((c0x-ax)*(c0x-ax)+(c0y-ay)*(c0y-ay))<R){ (*cat0agent).category=1; } else if(a[i].category==1){ c0x=(*cat0agent).coord.x; c0y=(*cat0agent).coord.y; ax=a[i].coord.x; ay=a[i].coord.y; bx=b[i].coord.x; by=b[i].coord.y; if(((c0x-bx)*(c0x-bx)+(c0y-by)*(c0y-by))<R){ (*cat0ka).category=1; return ; } } int t; for(t=1; 7-t;++t ){ if((*cat0agent).category=1){ if(6<t){ (*cat0agent).category=2; } } } (*cat0agent).coord.x+=(frand()-0.5); (*cat0agent).coord.y+=(frand()-0.5); (*cat0ka).coord.x+=(frand()-0.5); (*cat0ka).coord.y+=(frand()-0.5); } } } このプログラムでは、一定距離内にいると100%感染するようになっていますが、 それを、"一定距離内にいると30~50%の確率で感染する"という条件に変えたいと思っていますが、そのやり方がわかりません。 どなかか分かりやすく丁寧に教えていただけませんか? お願いします。 ちなみに感染率は今30%で考えてもらえれば良いです。 載せたプログラム自体も完成しきれていないので、わからないかもしれません。 すみません。

専門家に質問してみよう