不允许使用友元函数,改写习题15-3程序。 #include using namespace std; //代码修改----- class Car{ public: Car(int j){size=j;} friend int Leisure(int time, Car& aobj, Boat& bobj); protected: int size; }; class Boat{ public: Boat(int j){size=j;} friend int Leisure(int time, Car& aobj, Boat& bobj); protected: int size; }; //------------------------ int Leisure(int time, Car& aobj, Boat& bobj){ return time * aobj.Get() * bobj.Get(); } int main(){ Car c1(2); Boat b1(3); int time =4; cout <<Leisure(time,c1,b1); }
时间: 2023-06-01 21:08:00 浏览: 187
习题15-3程序是一个使用了友元函数的程序,题目要求我们禁止使用友元函数,并对程序进行修改。具体修改方法视程序实际情况而定,但应该是通过其他方式实现程序原有的功能,并且不使用友元函数。例如,可以使用类的public成员函数来操作私有数据,或者使用全局函数但不使用友元函数等。
相关问题
2.定义了Money类请补充完代码 #include <iostream> using namespace std; class Money { int Yuan, Jiao; // 元、角 public: Money(int y = 0, int j = 0) Money(double d) Money operator++(); //①用成员函数实现前置++ Money operator++(int); //②用成员函数实现后置++ friend Money operator--(Money &m); //③用友元函数实现前置-- friend Money operator--(Money &m, int); //④用友元函数实现后置-- operator double(); friend ostream & operator<<(ostream & out,Money & m); int main(void) { Money m(15, 3), m1, m2, m3, m4; cout<<"m:"<<m;//m.Show("m"); m1 = ++m; cout << "m1=++m;\n"<<"m1:"<<m1<<"m:"<<m; m2 = m++; cout << "m2=m++;\n"<<"m2:"<<m2<<"m:"<<m; m3 = --m; cout << "m3=--m;\n"<<"m3:"<<m3<<"m:"<<m; m4 = m--; cout << "m4=m--;\n"<<"m4:"<<m4<<"m:"<<m; Money m5 = m4 + 8.5; //A行 cout<<"m5= m4 + 8.5\nm5:"<<m5; return 0; }
下面是补充完整的Money类代码:
```c++
#include <iostream>
using namespace std;
class Money {
int Yuan, Jiao; // 元、角
public:
Money(int y = 0, int j = 0) : Yuan(y), Jiao(j) {}
Money(double d) {
Yuan = static_cast<int>(d);
Jiao = static_cast<int>((d - Yuan) * 10 + 0.5);
}
Money operator++(); //①用成员函数实现前置++
Money operator++(int); //②用成员函数实现后置++
friend Money operator--(Money &m); //③用友元函数实现前置--
friend Money operator--(Money &m, int);//④用友元函数实现后置--
operator double() { return Yuan + Jiao / 10.0; }
friend ostream & operator<<(ostream & out,Money & m);
Money operator+(double x) {
Money temp(*this);
temp.Yuan += static_cast<int>(x);
temp.Jiao += static_cast<int>((x - static_cast<int>(x)) * 10 + 0.5);
return temp;
}
};
Money Money::operator++() {
Jiao++;
if(Jiao == 10) {
Jiao = 0;
Yuan++;
}
return *this;
}
Money Money::operator++(int) {
Money temp(*this);
Jiao++;
if(Jiao == 10) {
Jiao = 0;
Yuan++;
}
return temp;
}
Money operator--(Money &m) {
m.Jiao--;
if(m.Jiao == -1) {
m.Jiao = 9;
m.Yuan--;
}
return m;
}
Money operator--(Money &m, int) {
Money temp(m);
m.Jiao--;
if(m.Jiao == -1) {
m.Jiao = 9;
m.Yuan--;
}
return temp;
}
ostream & operator<<(ostream & out, Money & m) {
out << m.Yuan << "." << m.Jiao << "元";
return out;
}
int main(void) {
Money m(15, 3), m1, m2, m3, m4;
cout << "m:" << m << endl;
m1 = ++m;
cout << "m1=++m;\n" << "m1:" << m1 << "m:" << m << endl;
m2 = m++;
cout << "m2=m++;\n" << "m2:" << m2 << "m:" << m << endl;
m3 = --m;
cout << "m3=--m;\n" << "m3:" << m3 << "m:" << m << endl;
m4 = m--;
cout << "m4=m--;\n" << "m4:" << m4 << "m:" << m << endl;
Money m5 = m4 + 8.5; //A行
cout << "m5= m4 + 8.5\nm5:" << m5 << endl;
return 0;
}
```
Money类中的主要成员函数和友元函数功能如下:
- Money(int y = 0, int j = 0):构造函数,初始化元和角;
- Money(double d):构造函数,根据浮点数d初始化元和角;
- Money operator++():前置++,使元和角都加1;
- Money operator++(int):后置++,使元和角都加1,但返回原对象的副本;
- friend Money operator--(Money &m):前置--,使元和角都减1;
- friend Money operator--(Money &m, int):后置--,使元和角都减1,但返回原对象的副本;
- operator double():类型转换运算符,将对象转换为浮点数;
- friend ostream & operator<<(ostream & out,Money & m):输出运算符,输出对象的元和角;
- Money operator+(double x):重载+运算符,将Money对象和浮点数相加,返回结果Money对象。
在main函数中,先创建一个Money对象m(15, 3),然后依次调用前置++、后置++、前置--、后置--和重载+运算符,最后将结果输出。
填写下方空白处代码,通过重载运算符使得Counter类型可以以x为值参与整数加法运算 #include<bits/stdc++.h> using namespace std; class Counter { public: Counter(int d){x=d;} int getx() const{return x;} private: int x; }; //------------------code here---------------------------- //------------------------------------------------------------- int main() { int a,y; cin>>a>>y; Counter x(a); cout<<x+y<<' '<<y+x; }
```cpp
#include<bits/stdc++.h>
using namespace std;
class Counter
{
public:
Counter(int d){x=d;}
int getx() const{return x;}
Counter operator+(int y)
{
return Counter(x+y);
}
friend Counter operator+(int y, const Counter& c)
{
return Counter(c.x+y);
}
private:
int x;
};
//-------------------------------------------------------------
int main()
{
int a,y;
cin>>a>>y;
Counter x(a);
cout<<x+y<<' '<<y+x;
}
```
在代码中,我们为Counter类重载了加法运算符。我们使用成员函数的方式重载了`Counter operator+(int y)`,使得可以将整数与Counter对象相加。同时,我们也使用友元函数的方式重载了`friend Counter operator+(int y, const Counter& c)`,使得可以将Counter对象与整数相加。这样,就可以实现Counter类型以x为值参与整数加法运算。
阅读全文