8.2 方孔钱币类设计-类组合

这个题跟8.1的类似 作为练习独立思考一下吧

前置代码::

#include <iostream>
#include <string>
using namespace std;
class Square//正方形类
{
private:
	double x;//边长
public:
	Square(double i=0)//带默认参数值的构造函数
	{	x=i;		
	}
	double getx()
	{
		return x;
	}    
};
class Circle//圆类
{
private:
	double r;//半径
public:
	Circle(double i=0)//带默认参数值的构造函数
	{	r=i;		
	}
	double getr()
	{
		return r;
	}    
};

后置代码::

int main()
{
	Square x(0.2);
	Circle y(1.25);
	Coin m(0.3,1.3,5.13,"开元通宝","银"),n(x,y,3.5,"五铢","铜");
	m.Show();
	n.Show();
	return 0;
}

期待的输出::

钱币文字=开元通宝↵
材质=银↵
直径=2.6厘米↵
方孔边长=0.3厘米↵
重量=5.13克↵
钱币文字=五铢↵
材质=铜↵
直径=2.5厘米↵
方孔边长=0.2厘米↵
重量=3.5克↵

coin类的设计::

class Coin
{
	private:
    	Square x;
	    Circle y;
	    double m;
	    string name;
	    string cl;
	public:
		Coin(double a,double b,double c,const string &d,const string &e) :x(a),y(b)
		{
			m=c;
			name=d;
			cl=e;
		}
		Coin(Square &x,Circle &y,double c,const string &d,const string &e) :x(x),y(y)
		{
			m=c;
			name=d;
			cl=e;
		}
	    void Show()
	    {
	    	
	    	cout<<"钱币文字="<<name<<endl;
            cout<<"材质="<<cl<<endl;
            cout<<"直径="<<y.getr()*2<<"厘米"<<endl;
            cout<<"方孔边长="<<x.getx()<<"厘米"<<endl;
            cout<<"重量="<<m<<"克"<<endl;
		}
};
原文地址:https://www.cnblogs.com/hzshisan/p/12571110.html