著作権:帰属、紙ベースを作成するために他人を許可し、(同じライセンスで元のライセンス契約に基づいて用紙配布する必要がありますクリエイティブコモンズ)
継承とC ++言語は、最も重要な特性である派生は、それはC ++の本質をカバーし、C ++は、私たちはそれを探求するために協力しましょう、C言語用度の最大の違いです!
-
継承と派生の場合:
-
派生クラスのメンバのアクセス属性:
実験1:
最初のクラスは、(1,2)として、座標点を出力するためのデータメンバーのX、Y位置(座標)、メンバ関数ボイドプリントを()を含むポイントを確立;基底クラスをポイントクラスを派生クラスサークル、中心点と半径を出力するために、機能部材二重領域()円の面積を計算するためのデータメンバRADUIS(半径)、増加ボイドプリントのメンバー関数()を増加させる;メイン関数で、定義されたテスト・クラス・オブジェクトの宣言。
#include<iostream>
using namespace std;
//Point类:
class Point
{
public:
Point(int xx = 0,int yy = 0)
{
x = xx;
y = yy;
}
protected:
int x,y;
};
//派生类对Pointe基类进行访问(派生类的声明):
class Circle:public Point
{
public:
Circle(int xx = 0,int yy = 0,int rr = 0)
{
Point(xx,yy);
r = rr;
}
void area(void)
{
double s = 3.14*r*r;
cout<<s<<endl;
}
protected:
int r;
};
int main(void)
{
Circle cir(1,2,3);
cir.area();
return 0;
}
-
構造派生クラス/デストラクタ:
-
ベースと派生クラスとの間の多重継承&&変換:
実験2:
基底クラスと派生クラスの割り当てとの例の変換の間。
#include<iostream>
using namespace std;
//基类:
class Base
{
int x;
public:
Base(){x=0;};
Base(int xx){x=xx;};
void print(){cout<<"x="<<x<<endl;}
};
//派生类:
class Derived:public Base
{
int y;
public:
Derived(){y=0;}
Derived(int xx,int yy):Base(xx){y=yy;}
void print(){cout<<"y="<<y<<endl;}
};
int main()
{
Base b1(10);
Derived d1(20,30);
b1.print();
d1.print();
cout<<endl;
Base &rb=b1;
Base &rd=d1;
Derived &r=d1;
rb.print ();
rd.print ();
r.print ();
cout<<endl;
Base *pb;
pb=&b1;
pb->print();
pb=&d1;
pb->print();
Derived *pd=&d1;
pd->print();
b1=d1;
b1.print();
return 0;
}