• 締切済み

C++ネットのサイトで独学で勉強してます

http://wisdom.sakura.ne.jp/programming/cpp/index.html のクラスという所での #include <iostream> using namespace std; class Kitty { private: int point; public: void setPoint(int i); int getPoint(int i); }; void Kitty::setPoint(int i) { point = i; } int Kitty::getPoint(int i) { point += i; return point; } int main() { Kitty obj; obj.setPoint(0); for (int i = 0 ; i < 10 ; i++) cout << obj.getPoint(i) << "\n"; return 0; } についてなんですが、 Kitty obj;というのが説明なしでいきなり出てきたので困ってます。この意味を教えて欲しいです。よろしくお願いします。C言語は一応勉強しました。 ちなみに、できればC++で詳しく書かれていて、理解しやすいサイトを教えて欲しいです。http://www5c.biglobe.ne.jp/~ecb/cpp/cpp00.htmlは見たのですがこのサイトも途中でつまずきました。

みんなの回答

noname#208124
noname#208124
回答No.1

ページの一番下の赤字でclassって書いてるところから推測できませんか? class Kitty obj; の省略形です

c-gongo
質問者

お礼

ありがとうございます。

関連するQ&A

  • 【C++】0保証の有無

    Cの開発経験のみで、C++に関しては初心者です。 下記のサンプルコードで、main()関数にてobj1.xとobj2.y の初期値設定をしているのですが、 obj1.yとobj2.xに関しては何も設定していません。 obj2.xはobj1.xの値をコピーしているので問題ないと思いますが、 obj1.yは不定な値をインクリメントする事には ならない(0保証される)のでしょうか? (出力結果を見ると、0が入っていたと認識できるんですが、 結果論で片付けるのはイヤなので・・・) 以下、サンプルコード ---------------------------------- #include<iostream> using namespace std; class POINT { public: long x; long y; void operator ++(int n) { x++; y++; } POINT operator ++() { ++x; ++y; return *this; } } obj1 , obj2 ; int main() { obj1.x = 10; obj2.y = 5; obj2 = ++obj1; obj2++; cout << "x = " << obj1.x << "\ty = " << obj1.y << '\n'; cout << "x = " << obj2.x << "\ty = " << obj2.y << '\n'; return 0; } ----------------------------

  • C++で解りません。

    C++で #include <iostream.h> int main(void) { int x; int y; cout << "xを入力してください:"; cin >> x; cout << "yを入力してください:"; cin >> y; cout << "x+yは" << x + y << "です。\n"; return 0; } ----------- で、 エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) とでます。どこがおかしいのか解りません。

  • 2つのcppファイルから

    1つのexeファイルを作る必要性が出てきました。 (現在質問中の質問に関連しています。) 例えば file.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } void main(void) { for(int i=0;i<9;Output(i++)); } をコンパイルリンクすれば済むことを file1.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } file2.cpp: void main(void) { for(int i=0;i<9;Output(i++)); } という風に分かれているファイルをコンパイルリンクしなければならないのです。 cpp -e file.exe -c file1.cpp -c file2.cpp ではエラーになります。 どうすれば良いのでしょうか?

  • C++のファイルの分割について教えてください。

    分割したプログラムの書き方を練習中なのですが、 下のようなプログラムを書いて、ビルドしようとしたら失敗します。 どこが悪いのか、教えていただけないでしょうか? (5)\test30\Debug\test30.exe : fatal error LNK1120: 外部参照 1 が未解決です。 1>test30 - エラー 2、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== //test30.cpp #include <iostream> #include "sub.h" int main(void) { std::cout<<"メインプログラムです。"<<std::endl; sub1(); sub2(); return 0; } ------------------------------------------------------------- //sub1.cpp #include <iostream> void sub1(void) { std::cout<<"サブ1です。"<<std::endl; } ------------------------------------------------------------- //sub2.cpp #include <iostream> void sub1(void) { std::cout<<"サブ2です。"<<std::endl; } --------------------------------------------------------------- //sub.h void sub1(void); void sub2(void);

  • 【C++】関数からクラスに変更するには?

    いつも大変お世話になっています。 VC++初心者です。 関数をクラス化していきたいのですが、具体的にどのようにしたら良いか ご指導頂けませんでしょうか。 例えば、このようなソースがあった場合、どのようにクラス化させるのでしょうか。 (また、下記の例ですと、pulsとsubで1つのグループ、 goodMoringとgoodNightで1つのグループにさせる場合には どうすれば宜しいでしょうか。) //======================= //Tool.hの中身 //======================= int plus(int x, int y); int sub(int x, int y); void goodMorning(); void goodNight(); //======================= //Tool.cppの中身 //======================= #include <iostream.h> int plus(int x, int y){ return x + y; } int sub(int x, int y){ return x - y; } void goodMorning(){ cout << "おはよう\n"; } void goodNight(){ cout << "こんばんは\n"; } //======================= //Main.cppの中身 //======================= #include <iostream.h> #include "Tool.h" int main(){ int a = puls(1,1); cout << a <<'\n'; int b = puls(2,1); cout << b <<'\n'; goodMorning(); goodNight(); }

  • C++関数の仮引数について

    関数の引数に配列を指定したいのですが たとえば、 double* point[4]; void setPoint(double _point[]) { for(int i = 0; i < 4; i++){ point[i] = (_point + i); } } int main(void) { setPoint((double[])(1.0, 2.0, 3.0, 4.0)); } '型キャスト' : 'double' から 'double []' に変換できません。 とエラーが出てしまいます。 どのようにすればよいでしょうか?

  • C++について。

    現在”猫でもわかるプログラミング”のC++編をSDKと共に勉強している身です。 現在第22章、第23章を勉強中です。 22章 http://www.kumei.ne.jp/c_lang/cpp/cpp_22.htm 23章 http://www.kumei.ne.jp/c_lang/cpp/cpp_23.htm 作業環境はVisual Studio 2005.net C++です 22章でのプログラムを作成し、実行した結果エラーが出てしまいました。 ソースです #include <iostream> class xy_position { int x; int y; public: xy_position(int x = 0, int y = 0){ xy_position::x = x; xy_position::y = y; } int X() const {return x;} int Y() const {return y;} }; ostream& operator << (ostream& o, const xy_position& p) { return o << "(" << p.X() << "," << p.Y() << ")"; } int main(void) { xy_position a(50, 60), b; std::cout << a << b << std::endl; return 0; } (17):error C2143: 構文エラー : ';' が '&' の前にありません。 (17):error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません (17):error C2065: 'o' : 定義されていない識別子です。 (17):error C2059: 構文エラー : 'const' (18):error C2143: 構文エラー : ';' が '{' の前にありません。 (18):error C2447: '{' : 対応する関数ヘッダーがありません (旧形式の仮引数リスト?) (26):error C2679: 二項演算子 '<<' : 型 'xy_position' の右オペランドを扱う演算子が見つかりません (または変換できません)。 このようなエラーが出てしまいました。 もちろんソースは全て同様に書いています。 この”猫でも”で使用しているコンパイラと異なるために出たエラーでしょうか? それに単に cout << のように記述するとエラーが出てしまい、 std::cout << のように記述しなければ通りません。 また、エラーとは別の質問になってしまいますが、プログラム中に int X() const {return x;} という記述がありますが、このconstの意味が分かりません。 単純に return x が変更不可能という意味でしょうか? 次に23章についての質問です。 ここでもソースは同じなのに以下のようなエラーが出てしまいます。 ソースです。 #include <iostream> int main(void) { int x=10, y=15, z=20; std::cout << "16進表示" << std::endl; std::cout.setf(ios::hex); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; std::cout.unsetf(iostream::hex); std::cout << "8進数" << std::endl; std::cout.setf(ios::oct); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; return 0; } (8):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (8):error C2065: 'hex' : 定義されていない識別子です。 (13):error C2653: 'iostream' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2065: 'oct' : 定義されていない識別子です。 これも何か設定をしなければいけないのでしょうか? なにぶんC++は・・・というかオブジェクト指向の言語は初心者なもので疑問も多いですorz

  • C++

    今、下のようなプログラムを作っています #include <iostream> #include <iostream> using namespace std; int i=0, c=0, n; char str[10]; class X16karax10{ //16進から10進ヘ public: void keisan(); }; void X16karax10::keisan(void){ cout<<"16進を入力して下さい"<<endl; cout<<"英数字は大文字で入力してください(F→○ f→×)" <<endl; scanf("%s",str); while(str[i] != '\0'){ n = n * 0x10; c = str[i++]; if((c >= '0') && (c <= '9')){ n += c - '0'; } else if((c >= 'A') && (c <= 'F')){ n += c - 'A' + 10; } } cout<<("%d\n",n)<<"です\n"<<endl; } int main(){ for(i=0; ; i++){ X16karax10 p; p.keisan(); } } 16進を十進に変えるものなのですがreturn 0を使うと「X16karax10::keisan()' は値を返せない」と、でてしまうのですがどうしたらよいでしょうか?

  • C++でどうすればcoutで表示できるのですか?

    C++についての質問です coutで表示するときに kannsuu関数の返り値を main関数とmain関数の cout << ++kannsuu(i) << endl; の部分を変更せずに kannsuu関数のみを変更して インクリメントして「2」と表示したいのですが どのようにすればいいですか? #include <iostream> using namespace std; int kannsuu(int i) {return i;} int main() { int i = 1; cout << ++kannsuu(i) << endl; return 0; }

  • 構造体のリンク?

    こんにちは。 超初心者です。 どうぞよろしくお願いしますm(__)m 下記の三つのファイル、コンパイルすると「パブリックシンボル_high、_lowがi.obj、c.objの両方に定義されている」旨のエラーが出てしまいます。 ヘッダファイルに#ifndef~を書くくらいしか見当がつけられない初心者です(T_T) どこをどう直せばいいのか教えてください。 やりたいことは、high->i+high->j、low->i+low->jを関数一つに纏めて、mainから別ファイルに独立させることです。 *********c.h********* #ifndef __c__ #define __c__ struct type{ int i; int j; }*high,*low; extern int f(struct type *temp); #endif *********c.cpp********* #include"c.h" int f(struct type *temp); int f(struct type *temp){ return temp->i+temp->j; } *********i.cpp********* #include<iostream.h> #include"c.h" void main(void){ high->i=10; high->j=30; low->i=100; low->j=300; cout<<f(high)<<","<<f(low)<<endl; }