Cから見るC++の(四)構造関数と構造関数
1837 ワード
コンストラクション関数の意味:
コンストラクション関数の宣言テンプレート:
構造関数:
使用制限:
構造関数の宣言テンプレート:
コンストラクション関数とコンストラクション関数の例:
コンパイル実行:
.
使用制限:1. ;
2. ,void ;
3. public, , protect;
呼び出しメカニズム: , . , . , , .
コンストラクション関数の宣言テンプレート:
className::className()
{
...;
}
構造関数:
, , . ,new , delete.
使用制限:
1. , “~”;
2. , , ;
3. , , ;
4. .
構造関数の宣言テンプレート:
ClassName::~ClassName( )
{
...;
}
コンストラクション関数とコンストラクション関数の例:
#include <iostream>
class simpleClass
{
private:
float x,y;
public:
float m,n;
void setXY(float,float);
void getXY();
simpleClass(float,float);
~simpleClass();
};
simpleClass::~simpleClass()
{
std::cout << "GoodBye,Crue World!"<< std::endl;
}
simpleClass::simpleClass(float a,float b)
{
x = a;
y = b;
}
void simpleClass::setXY(float a,float b)
{
x = a;
y = b;
}
void simpleClass::getXY(void)
{
std:: cout << x << '\t' << y << std::endl;
}
int main(void)
{
simpleClass cls1(3.0,6.0);
cls1.m = 10;
cls1.n = 20;
cls1.getXY();
cls1.setXY(2.0,5.0);
cls1.getXY();
return 0;
}
コンパイル実行:
root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# g++ this.cpp -o this
root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# ./this
3 6
2 5
GoodBye,Crue World!