- 類的組合
- 代碼示例
一、類的組合
1. 類組合: 類中的成員數據是另一個類的對象或者是另一個類的指針或引用。通過類的組合可以在已有的抽象的基礎上實現更復雜的抽象。
2. 原則: 不僅負責對本類中的基本類型成員數據賦初值,也要對內嵌對象成員初始化。
3. 聲明形式:
可以是含參構造函數,也可以複製構造函數
類名::類名(形參):內嵌對象1(參數),內嵌對象2(參數),... { }
4. 構造函數的調用:
- 先調用內嵌對象的構造函數(按內嵌時的聲明順序,先聲明者先構造),然後再調用本類的構造函數(析構函數的調用順序相反)
- 若調用默認構造函數(即沒有形參的),則內嵌對象的初始化也將調用相應的默認構造函數。
二、代碼示例
例1. 【兩點間距離】實現計算兩個點(Point)之間的距離(Distance),Distance類的數據成員有Point類的對象。
這裏插入代碼片
#include<iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int xx, int yy);
Point(Point &p);
~Point() { cout << "Point's destructor is called" << endl; }
int getX() { return x; }
int getY() { return y; }
};
Point::Point(int xx, int yy) {
x = xx;y = yy;
cout << "Point's constructor is called" << endl;
}
Point::Point(Point &p) {
x = p.x;y = p.y;
cout << "Point's copy constructor is called" << endl;
}
class Distance {
private:
Point p1, p2; // Point類的對象作爲Distance類的成員數據,聲明順序先p1後p2
d ouble dist;
public:
//Distance(Point a, Point b);
Distance(Point &a, Point &b);
Distance(Distance &d);
double getDis() { return dist; }
~Distance() {cout << "Distance's destrutor was called" << endl;}
};
//Distance::Distance(Point a, Point b) :p1(a), p2(b) { //Point複製構造函數被調用四次
Distance::Distance(Point &a, Point &b) :p1(a), p2(b) { //Point複製構造函數被調用二次,傳引用就是給變量起別名
double x = double(p1.getX() - p2.getX());
double y = double(p1.getY() - p2.getY());
dist = sqrt(x * x + y * y);
c out << "Distance's constructor was called" << endl;
}
Distance::Distance(Distance &d) :p1(d.p1), p2(d.p2) {
dist = d.dist;
cout << "Distance's copy constructor was called" << endl;
}
int main() {
Point myp1(1, 2), myp2(3, 5);
Distance myd(myp1, myp2);
cout << "The distance is: " << myd.getDis() << endl;
cout << "==============================" << endl;
Distance myd2(myd);
cout << "The distance is: " << myd2.getDis() << endl;
return 0;
}
在創建myd對象時,首先跑到Distance的含參構造函數處不進去,依次調用內嵌對象myp1、myp2的構造函數,由於形參是傳引用(給變量起別名,myp1和a是同一個,只需一次複製構造)並且是p1(a)(即本類對象給新對象初始化),所以調用2次Point類複製構造,然後調用Distance構造函數。myd2的分析也是類似,首先跑到Distance的複製構造處不進去,調用2次Point類複製構造,然後調用Distance複製構造函數。
另外讀者可以思考一下上述Distance含參構造形參不傳引用,即換爲註釋代碼會出現什麼不同。
例2.【點線三角形的類組合】Point類,Line類(含有兩個Point類對象),Triangle類(含有三個Line類對象),設計Triangle類的成員函數完成三條邊是否能構成三角形的檢驗和三角形面積計算,面積顯示。
#include <iostream>
#include <cstring>
using namespace std;
class Point {
public:
float x, y;
static int i;//靜態數據成員,對象共有,用來計點的個數,類內聲明
Point() {
cout << "請輸入第" << i << "個點的橫縱座標:";
cin >> x >> y;
cout << "(" << x << ", " << y << ")" << endl;
i++;
}
Point(float xx, float yy) {
x = xx;y = yy;
}
Point(Point &p) {
x = p.x; y = p.y;
}
~Point() {}
};
int Point::i = 1;//類外定義並初始化
class Line
{
public:
float l = 0;//邊長
static int j;
Point p1, p2;
Line(Point &a, Point &b) :p1(a), p2(b) {
l = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
cout << "第" << j << "條邊的邊長爲:" << l << endl;
j++;
};
Line(Line &ll) :p1(ll.p1), p2(ll.p2) {
l = ll.l;
}
~Line() {}
};
int Line::j = 1;
class Triangle {
float area;
public:
static bool flag;
Triangle(Line l1, Line l2, Line l3)//構造函數,判斷能否構成三角形,Line類對象作爲形參調用Line的複製構造函數
{
float a, b, c;
a = l1.l;b = l2.l;c = l3.l;
if (a + b > c&&a + c > b&&b + c > a){
flag = true;
cout << "可以構成三角形" << endl;
float p = (a + b + c) / 2;
area = sqrt(p*(p - a)*(p - b)*(p - c));
}
else {
flag = false;
cout << "不能構成三角形" << endl;
}
}
void show() {
if (flag == true)
{cout << "三角形的面積是:" << area << endl;}
else if (flag == false) {}
};
};
bool Triangle::flag = false;
int main(){
Point p1, p2, p3, p4, p5, p6;//點對象
Line l1(p1, p2), l2(p3, p4), l3(p5, p6);//邊對象
Triangle t(l1, l2, l3);//三角形對象
t.show();
return 0;
}
例3.【點三角形的類組合】 Triangle類含有Point類三個對象,含有獲取三角形周長和麪積的方法。輸入三個點座標,輸出三角形周長和麪積。
#include<iostream>
#include<cstring>
using namespace std;
class Point {
public:
Point(float x = 0, float y = 0) :x(x), y(y) {}
Point(Point & p);
~Point() { };
float GetX() { return x; }
float GetY() { return y; }
void SetX(float Newx) { x = Newx; }
void SetY(float Newy) { y = Newy; }
float dis(Point B);
private:
float x, y;
};
Point::Point(Point & p) {
x = p.x; y = p.y;
}
float Point::dis(Point B) {
float xx = pow((B.x - x), 2);
float yy = pow((B.y - y), 2);
return sqrt(xx + yy)
}
class Triangel {
public:
Triangel() {};
Triangel(Point newp1, Point newp2, Point newp3) :p1(newp1), p2(newp2), p3(newp3) {}
Triangel(Triangel & p);
~Triangel() {};
Point getPoint1() { return p1; } //獲取點的接口
Point getPoint2() { return p2; }
Point getPoint3() { return p3; }
void setPoint1(Point point1) { p1 = point1; } //設置點的接口
void setPoint2(Point point2) { p2 = point2; }
void setPoint3(Point point3) { p3 = point3; }
float getArea();
float getPeri();
private:
float edge1, edge2, edge3, peri, area;
Point p1, p2, p3;
};
Triangel::Triangel(Triangel & p) {
p1 = p.p1;
p2 = p.p2;
p3 = p.p3;
}
float Triangel::getPeri() {
edge1 = getPoint1().dis(p2);//點p1到p2的距離
edge2 = getPoint2().dis(p3);
edge3 = getPoint3().dis(p1);
return peri = edge1 + edge2 + edge3;
}
float Triangel::getArea() {
float half = getPeri() / 2;
return area = sqrt(half*(half - edge1)*(half - edge2)*(half - edge3));
}
int main() {
Point p1(0, 0), p2(0, 3), p3(4, 0);
Triangel tri(p1, p2, p3);
cout << "三角形周長爲:" << tri.getPeri() << ", 面積爲:" << tri.getArea() << endl;
Point p4(2, 0);
tri.setPoint1(p4);
cout << "三角形周長爲:" << tri.getPeri() << ", 面積爲:" << tri.getArea() << endl;
}
附:前向引用聲明
在提供一個完整的類聲明之前,不能聲明該類的對象,即下面的例子是錯的:
class B;
class A{
public:
B b;
};
class B{
public:
A a;
};
使用前向引用聲明時只能使用被聲明的符號,而不能涉及類內的任何細節。下面的例子是對的:
class B;
class A{
public:
B &b;
};
class B{
public:
A *a;
};
總結: 類的組合是類中的成員數據是另一個類的對象或者是另一個類的指針或引用。在聲明時注意內嵌對象要在參數列表裏初始化。代碼實現重在邏輯誰用誰的對象,複製構造和含參構造的調用情況,讀取和設置私有數據可以寫個函數作爲藉口,注意含參構造函數的形參裏是對象還是對象引用。
歡迎關注微信公衆號:學編程的金融客,作者:小笨聰