A.距离计算(友元函数)
题目描述
Point类的基本形式如下:
1 2 3 4 5 6 7
| class Point { private: double x, y; public: Point(double xx, double yy); };
|
请完成如下要求:
1.实现Point类;
2.为Point类增加一个友元函数double Distance(Point &a, Point &b),用于计算两点之间的距离。直接访问Point对象的私有数据进行计算。
3.编写main函数,输入两点坐标值,计算两点之间的距离。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<iostream> #include<cmath> using namespace std;
class Point { private: double x, y; public: Point(double x1, double y1); friend double Distance(Point& a, Point& b); };
Point::Point(double x1, double y1) { x = x1; y = y1; }
double Distance(Point& a, Point& b) { double dis; dis = sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2)); return dis; }
int main() { int t; cin >> t; while (t--) { double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; Point a(x1, y1); Point b(x2, y2);
double dis; dis = Distance(a, b); cout << (int)dis << endl; }
return 0; }
|
B. 银行账户(静态成员与友元函数)
题目描述
银行账户类的基本描述如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Account { private: static float count; static float interestRate; string accno, accname; float balance; public: Account(string ac, string na, float ba); ~Account(); void deposit(float amount); void withdraw(float amount); float getBalance(); void show(); static float getCount(); static void setInterestRate(float rate); static float getInterestRate(); };
|
要求如下:
实现该银行账户类
为账户类Account增加一个友元函数,实现账户结息,要求输出结息后的余额(结息余额=账户余额+账户余额*利率)。友元函数声明形式为 friend void update(Account& a);
在main函数中,定义一个Account类型的指针数组,让每个指针指向动态分配的Account对象,并调用成员函数测试存款、取款、显示等函数,再调用友元函数测试进行结息。
大家可以根据实际需求在类内添加其他方法,也可以修改成员函数的参数设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<iostream> #include<string> using namespace std; class Account { public: Account(string accno,string name,float balance); ~Account(); void Deposit(float amount); void Withdraw(float amount); float GetBalance(); void Show(); static int GetCount(); static float GetInterestRate(); static void setcount(int c); static void setrate(float rate); friend void Update(Account & a); private: static int count; static float InterestRate; string _accno,_accname; float _balance; }; int Account::count; float Account::InterestRate; Account::Account(string accno,string name,float balance):_accno(accno),_accname(name),_balance(balance){} Account::~Account(){} void Account::Deposit(float amount) { _balance+=amount; } void Account::Withdraw(float amount) { _balance-=amount; } float Account::GetBalance() { return _balance; } void Account::Show() { cout<<_accno<<' '<<_accname<<' '; } int Account::GetCount() { return count; } float Account::GetInterestRate() { return InterestRate; } void Account::setcount(int c) { count=c; } void Account::setrate(float rate) { InterestRate=rate; } void Update(Account & a) { a._balance+=a._balance*a.InterestRate; cout<<' '<<a._balance<<' '; } int main() { int n,i; float rate,balance_sum=0,balance,amount; string accno,accname; cin>>rate; cin>>n; Account::setcount(n); Account::setrate(rate); Account ** p=new Account*[n]; for(i=0;i<n;i++) { cin>>accno>>accname>>balance; p[i]=new Account(accno,accname,balance); p[i]->Show(); cin>>amount; p[i]->Deposit(amount); cout<<p[i]->GetBalance(); Update(*p[i]); cin>>amount; p[i]->Withdraw(amount); cout<<p[i]->GetBalance()<<endl; balance_sum+=p[i]->GetBalance(); } cout<<balance_sum; if(p) { for(i=0;i<n;i++) delete p[i]; delete[] p; } }
|
C.复数运算(友元函数)
题目描述
复数类的声明如下:
1 2 3 4 5 6 7 8 9 10 11
| class Complex { private: double real; double imag; public: Complex(); Complex(double r, double i); friend Complex addCom(const Complex& c1, const Complex& c2); friend void outCom(const Complex& c); };
|
要求如下:
1. 实现复数类和友元函数addCom和outCom。
2. 参考addCom函数为复数类增加一个友元函数minusCom,用于实现两个复数的减法
3. 在main函数中,通过友元函数,实现复数的加减法和复数的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include<iostream> using namespace std; class Complex { public: Complex(); Complex(double r, double i); friend Complex addcom(Complex a1, Complex a2); friend Complex minuscom(Complex a1, Complex a2); friend void outcom(Complex c); private: double real; double imag; }; Complex::Complex(double r, double i) { real = r, imag = i; } Complex addcom(Complex a1, Complex a2) { Complex c(a1.real + a2.real, a1.imag + a2.imag); return c; } Complex minuscom(Complex a1, Complex a2) { Complex c(a1.real - a2.real, a1.imag - a2.imag); return c; } void outcom(Complex c) { cout << "(" << c.real << "," << c.imag << ")" << endl; } int main() { int t, real, imag; char sym; cin >> real >> imag; Complex c(real, imag); cin >> t; while (t--) { cin >> sym >> real >> imag; Complex c1(real, imag); if (sym == '+') { c = addcom(c, c1); } else { c = minuscom(c, c1); } outcom(c); } return 0; }
|
D.旅馆顾客统计(静态成员)
题目描述
编写程序,统计某旅馆住宿客人的总数和收入总额。要求输入客人的姓名,输出客人编号(2015+顺序号,顺序号4位,如第1位为0001,第2位为0002,依此类推)、姓名、总人数以及收入总额。总人数和收入总额用静态成员,其他属性采用普通的数据成员。旅馆类声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Hotel { private: static int totalCustNum; static float totalEarning; static float rent; char *customerName; int customerId; public: Hotel(char* customer); ~Hotel(); void Display(); };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include<iostream> #include<string.h> #include<iomanip> using namespace std; class Hotel{ private: static int m_TotalCusNum; static float m_TotalEearning; static float m_Rent; char* m_CustomerName; int m_CustomerID; public: Hotel(char *a_Customer){ m_CustomerName=new char[strlen(a_Customer)+1]; strcpy(m_CustomerName,a_Customer); m_TotalCusNum++; m_CustomerID=m_TotalCusNum; m_TotalEearning=m_TotalCusNum*m_Rent; } ~Hotel(){ delete m_CustomerName; } static void setRent(float r){ m_Rent=r; } void Display(){ cout<<m_CustomerName<<" 2015"<<setfill('0')<<setw(4)<<m_CustomerID; cout<<" "<<m_CustomerID<<" "<<m_TotalEearning<<endl; } }; float Hotel::m_Rent=0; int Hotel::m_TotalCusNum=0; float Hotel::m_TotalEearning=0; int main(){ float rent; char name[50]; cin>>rent; Hotel::setRent (rent); while(1){ cin>>name; if(name[0]=='0') break; Hotel h1(name); h1.Display(); } return 0; }
|
E.日期时间合并输出(友元函数)
题目描述
已知日期类Date,有属性:年、月、日,其他成员函数根据需要自行编写,注意该类没有输出的成员函数
已知时间类Time,有属性:时、分、秒,其他成员函数根据需要自行编写,注意该类没有输出的成员函数
现在编写一个全局函数把时间和日期的对象合并起来一起输出,
函数原型为:void display(const Date &d, const Time &t)
函数输出要求为:
1、时分秒输出长度固定2位,不足2位补0
2、年份输出长度固定为4位,月和日的输出长度固定2位,不足2位补0
例如2017年3月3日19时5分18秒
则输出为:2017-03-03 19:05:18
程序要求
1、把函数display作为时间类、日期类的友元
2、分别创建一个日期对象和时间对象,保存日期的输入和时间的输入
3、调用display函数实现日期和时间的合并输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include<iostream> #include<iomanip> using namespace std; class Time; class date { private: int year, month, day; public: date(int y, int m, int d) { year = y, month = m, day = d; } friend void show(date&, Time&); }; class Time { private: int hour, minute, second; public: Time(int h, int m1, int s) { hour = h, minute = m1, second = s; } friend void show(date&, Time&); }; void show(date& a, Time& b) { cout << a.year << "-"; cout << setfill('0') << setw(2) << a.month << "-"; cout << setfill('0') << setw(2) << a.day << " "; cout << setfill('0') << setw(2) << b.hour << ":"; cout << setfill('0') << setw(2) << b.minute << ":"; cout << setfill('0') << setw(2) << b.second << endl; } int main() { int t; cin >> t; while (t--) { int year, month, day, hour, minute, second; cin >> year >> month >> day >> hour >> minute >> second; date a(year, month, day); Time b(hour, minute, second); show(a, b); } return 0; }
|