C++におけるキーワードStructとClassの違い
8257 ワード
StructとClassの違い
今日のブログでは、C++のキーワードstructとclassの違いを主に説明します.このブログでは、この2つのキーワードの異なる面を系統的に詳しく説明します.
文法的には、classとstructがタイプ定義をするときに2つの違いしかありません.
1.デフォルトの継承権限は、指定しない場合、classからの継承はprivate継承処理に従い、structからの継承はpublic継承処理に従う.
2.メンバーのデフォルトのアクセス権.classのメンバーのデフォルトはprivate権限、structのデフォルトはpublic権限です.以上の2点もstructとclassの最も基本的な違いであり、最も本質的な違いでもある.
しかし、C++ではstructが拡張され、現在では異なるデータ型を含むデータ構造だけでなく、より多くの機能が含まれています.
Structにはメンバー関数が含まれますか?
はい、答えは肯定的です.コード検証を書かせていただきます.
以上のコードは正しく動作します.そう、structはメンバー関数を含むことができます.
Structには独自の構造関数がありますか?
はい、可能です.次のテストコードを参照してください.
Structは構造関数を持つことができますか?
検証してみましょう.
はい、構造関数を完全にサポートします.
Structは継承をサポートしますか?
コード検証を書かせてください.
上記のコードを実行し、structは継承をサポートします.
Structはマルチステートをサポートしますか?
コードを書いてテストするとわかります.
StructはPrivate、Protected、Publicキーワードをサポートしますか?
こんなにたくさん書いてあると、classの親がstructキーワードで記述されている場合、デフォルトのアクセス属性は何ですか?
この場合、デフォルトがpublic継承なのかprivate継承なのかは、ベースクラスではなくサブクラスに依存します.classはstruct修飾クラスから継承することができる.また、structはclass修飾クラスから継承することもできます.継承プロパティは以下のように説明します.
最後に、structを使うのか、classを使うのか.これは個人的な好みですが、ここにはプログラミング規範の問題があります.データ構造のようにしなければならないと思ったら、structを使って、もしあなたがもっとオブジェクトのようにしなければならないなら、classを使います.
今日のブログでは、C++のキーワードstructとclassの違いを主に説明します.このブログでは、この2つのキーワードの異なる面を系統的に詳しく説明します.
文法的には、classとstructがタイプ定義をするときに2つの違いしかありません.
1.デフォルトの継承権限は、指定しない場合、classからの継承はprivate継承処理に従い、structからの継承はpublic継承処理に従う.
2.メンバーのデフォルトのアクセス権.classのメンバーのデフォルトはprivate権限、structのデフォルトはpublic権限です.以上の2点もstructとclassの最も基本的な違いであり、最も本質的な違いでもある.
しかし、C++ではstructが拡張され、現在では異なるデータ型を含むデータ構造だけでなく、より多くの機能が含まれています.
Structにはメンバー関数が含まれますか?
はい、答えは肯定的です.コード検証を書かせていただきます.
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct Test
{
int a;
int getA()
{
return a;
}
void setA(int temp)
{
a = temp;
}
};
int main(int argc, char* argv[])
{
Test testStruct;
testStruct.setA(10);
cout<
Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout<getA()< delete testStructPointer;
return 0;
}
以上のコードは正しく動作します.そう、structはメンバー関数を含むことができます.
Structには独自の構造関数がありますか?
はい、可能です.次のテストコードを参照してください.
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct Test
{
int a;
Test()
{
a = 100;
}
int getA()
{
return a;
}
void setA(int temp)
{
a = temp;
}
};
int main(int argc, char* argv[])
{
Test testStruct;
testStruct.setA(10);
cout< Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout< delete testStructPointer;
// test the constructor
Test testConstructor;
cout<
return 0;
}
Structは構造関数を持つことができますか?
検証してみましょう.
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct Test
{
int a;
Test()
{
a = 100;
}
int getA()
{
return a;
}
void setA(int temp)
{
a = temp;
}
~Test()
{
cout< }
};
int main(int argc, char* argv[])
{
Test testStruct;
testStruct.setA(10);
cout< Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout< delete testStructPointer;
// test the constructor
Test testConstructor;
cout<
return 0;
}
はい、構造関数を完全にサポートします.
Structは継承をサポートしますか?
コード検証を書かせてください.
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct A
{
int a;
A()
{
a = 10;
}
void print()
{
cout< }
};
struct B : A
{
int b;
B()
{
a = 30; // set a to 30
b = 20;
}
/*void print()
{
cout< }*/
};
int main(int argc, char* argv[])
{
B b1;
cout< cout< b1.print();
A a1;
cout< a1.print();
return 0;
}
上記のコードを実行し、structは継承をサポートします.
Structはマルチステートをサポートしますか?
コードを書いてテストするとわかります.
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct A
{
virtual void print() = 0;
};
struct B : A
{
void print()
{
cout< }
};
struct C : A
{
void print()
{
cout< }
};
int main(int argc, char* argv[])
{
A *a1;
B *b1 = new B;
C *c1 = new C;
a1 = b1;
a1->print(); // call B, not A
a1 = c1;
a1->print(); // call C, not A
return 0;
}
StructはPrivate、Protected、Publicキーワードをサポートしますか?
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#include
using namespace std;
struct A
{
private:
int b;
protected:
int c;
public:
A()
{
b = 10;
c = 20;
d = 30;
}
int d;
};
struct B : A
{
void printA_C()
{
cout<<:c> };
// private member can not see
/*void printA_B()
{
cout<<:b> }*/
void printA_D()
{
cout<<:d> }
};
int main(int argc, char* argv[])
{
A a1;
B b1;
// private member can not see
//cout<
// protected member can not see
//cout<
// public member can see
cout<
return 0;
}
こんなにたくさん書いてあると、classの親がstructキーワードで記述されている場合、デフォルトのアクセス属性は何ですか?
この場合、デフォルトがpublic継承なのかprivate継承なのかは、ベースクラスではなくサブクラスに依存します.classはstruct修飾クラスから継承することができる.また、structはclass修飾クラスから継承することもできます.継承プロパティは以下のように説明します.
class B:A{}; // private
class A{};
struct B:A{}; // public
最後に、structを使うのか、classを使うのか.これは個人的な好みですが、ここにはプログラミング規範の問題があります.データ構造のようにしなければならないと思ったら、structを使って、もしあなたがもっとオブジェクトのようにしなければならないなら、classを使います.