C++中症結字Struct和Class的差別。本站提示廣大學習愛好者:(C++中症結字Struct和Class的差別)文章只能為提供參考,不一定能成為您想要的結果。以下是C++中症結字Struct和Class的差別正文
Struct和Class的差別
明天這篇博文重要講授在C++中症結字struct和class的差別。這篇博文,將會體系的將這兩個症結字的分歧面停止具體的講授。
從語法下去講,class和struct做類型界說時只要兩點差別:
1.默許繼續權限,假如不指定,來自class的繼續依照private繼續處置,來自struct的繼續依照public繼續處置;
2.成員的默許拜訪權限。class的成員默許是private權限,struct默許是public權限。以上兩點也是struct和class最根本的差異,也是最實質的差異;
然則在C++中,struct停止了擴大,如今它曾經不只僅是一個包括分歧數據類型的數據構造了,它包含了更多的功效。
Struct能包括成員函數嗎?
是的,謎底是確定的。如今就讓我寫一段代碼驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.jb51.net
*/
#include <iostream>
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<<"Get the value from struct:"<<testStruct.getA()<<endl;
Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout<<"Get the value from struct again:"<<testStructPointer->getA()<<endl;
delete testStructPointer;
return 0;
}
以上的代碼會很准確的運轉,是的;沒錯,struct能包括成員函數的。
Struct有本身的結構函數嗎?
是的,可以的。看以下測試代碼:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.jb51.net
*/
#include <iostream>
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<<"Get the value from struct:"<<testStruct.getA()<<endl;
Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
delete testStructPointer;
// test the constructor
Test testConstructor;
cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
return 0;
}
Struct可以有析構函數麼?
讓我來驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.jb51.net
*/
#include <iostream>
using namespace std;
struct Test
{
int a;
Test()
{
a = 100;
}
int getA()
{
return a;
}
void setA(int temp)
{
a = temp;
}
~Test()
{
cout<<"Destructor function called."<<endl;
}
};
int main(int argc, char* argv[])
{
Test testStruct;
testStruct.setA(10);
cout<<"Get the value from struct:"<<testStruct.getA()<<endl;
Test *testStructPointer = new Test;
testStructPointer->setA(20);
cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
delete testStructPointer;
// test the constructor
Test testConstructor;
cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
return 0;
}
是的,完整支撐析構函數。
Struct支撐繼續麼?
再讓我寫代碼驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.jb51.net
*/
#include <iostream>
using namespace std;
struct A
{
int a;
A()
{
a = 10;
}
void print()
{
cout<<"I am from A"<<endl;
}
};
struct B : A
{
int b;
B()
{
a = 30; // set a to 30
b = 20;
}
/*void print()
{
cout<<"I am from B"<<endl;
}*/
};
int main(int argc, char* argv[])
{
B b1;
cout<<b1.a<<endl;
cout<<b1.b<<endl;
b1.print();
A a1;
cout<<a1.a<<endl;
a1.print();
return 0;
}
運轉上述代碼,struct支撐繼續。
Struct支撐多態麼?
寫代碼測試一下便知:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.jb51.net
*/
#include <iostream>
using namespace std;
struct A
{
virtual void print() = 0;
};
struct B : A
{
void print()
{
cout<<"I am from B"<<endl;
}
};
struct C : A
{
void print()
{
cout<<"I am from C"<<endl;
}
};
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 http://www.jb51.net
*/
#include <iostream>
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<<A::c<<endl;
};
// private member can not see
/*void printA_B()
{
cout<<A::b<<endl;
}*/
void printA_D()
{
cout<<A::d<<endl;
}
};
int main(int argc, char* argv[])
{
A a1;
B b1;
// private member can not see
//cout<<a1.b<<endl;
// protected member can not see
//cout<<a1.c<<endl;
// public member can see
cout<<a1.d<<endl;
return 0;
}
寫了這麼多了,那末會湧現這類一個狀態,假如是class的父類是struct症結字描寫的,那末默許拜訪屬性是甚麼?
當湧現這類情形時,究竟默許是public繼續照樣private繼續,取決於子類而不是基類。class可以繼續自struct潤飾的類;同時,struct也能夠繼續自class潤飾的類,繼續屬性以下列描寫:
class B:A{}; // private 繼續
class A{};
struct B:A{}; // public 繼續
最初,那末究竟是應用struct,照樣應用class呢?這個看小我愛好,然則這裡有一個編程標准的成績,當你認為你要做的更像是一種數據構造的話,那末用struct,假如你要做的更像是一種對象的話,那末用class。