不允许使用友元函数,改写习题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 08:08:00 浏览: 41
习题15-3程序是一个使用了友元函数的程序,题目要求我们禁止使用友元函数,并对程序进行修改。具体修改方法视程序实际情况而定,但应该是通过其他方式实现程序原有的功能,并且不使用友元函数。例如,可以使用类的public成员函数来操作私有数据,或者使用全局函数但不使用友元函数等。
相关问题
帮我用C++写一个结构体,包含+-*/重载运算符,拷贝函数,赋值函数,友元函数
好的,以下是一个示例:
```c++
#include <iostream>
using namespace std;
struct MyStruct {
int a, b;
MyStruct(int a = 0, int b = 0) : a(a), b(b) {}
// 加法运算符重载
MyStruct operator+(const MyStruct& other) const {
return MyStruct(a + other.a, b + other.b);
}
// 减法运算符重载
MyStruct operator-(const MyStruct& other) const {
return MyStruct(a - other.a, b - other.b);
}
// 乘法运算符重载
MyStruct operator*(const MyStruct& other) const {
return MyStruct(a * other.a, b * other.b);
}
// 除法运算符重载
MyStruct operator/(const MyStruct& other) const {
return MyStruct(a / other.a, b / other.b);
}
// 赋值函数
MyStruct& operator=(const MyStruct& other) {
a = other.a;
b = other.b;
return *this;
}
// 拷贝函数
MyStruct(const MyStruct& other) {
a = other.a;
b = other.b;
}
// 友元函数
friend ostream& operator<<(ostream& os, const MyStruct& obj) {
os << "a: " << obj.a << ", b: " << obj.b;
return os;
}
};
int main() {
MyStruct a(1, 2), b(3, 4), c;
// 测试加法运算符重载
c = a + b;
cout << "a + b = " << c << endl;
// 测试减法运算符重载
c = a - b;
cout << "a - b = " << c << endl;
// 测试乘法运算符重载
c = a * b;
cout << "a * b = " << c << endl;
// 测试除法运算符重载
c = a / b;
cout << "a / b = " << c << endl;
// 测试赋值函数
MyStruct d = a;
cout << "d = " << d << endl;
// 测试拷贝函数
MyStruct e(a);
cout << "e = " << e << endl;
return 0;
}
```
输出结果:
```
a + b = a: 4, b: 6
a - b = a: -2, b: -2
a * b = a: 3, b: 8
a / b = a: 0, b: 0
d = a: 1, b: 2
e = a: 1, b: 2
```
帮我用c++写一个类 包含+ - / * == > <重载运算符,友元函数,以及使用,写出完整代码
好的,以下是一个简单的C++类,包含重载+、-、*、/、==、>、<运算符,以及友元函数。同时,还提供了一个使用该类的示例。
```c++
#include <iostream>
class MyClass {
public:
MyClass(int a = 0, int b = 0) : x(a), y(b) {}
MyClass operator+(const MyClass& other) const {
return MyClass(x + other.x, y + other.y);
}
MyClass operator-(const MyClass& other) const {
return MyClass(x - other.x, y - other.y);
}
MyClass operator*(const MyClass& other) const {
return MyClass(x * other.x, y * other.y);
}
MyClass operator/(const MyClass& other) const {
return MyClass(x / other.x, y / other.y);
}
bool operator==(const MyClass& other) const {
return (x == other.x) && (y == other.y);
}
bool operator>(const MyClass& other) const {
return (x > other.x) && (y > other.y);
}
bool operator<(const MyClass& other) const {
return (x < other.x) && (y < other.y);
}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
private:
int x;
int y;
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj) {
os << "(" << obj.x << ", " << obj.y << ")";
return os;
}
int main() {
MyClass a(1, 2);
MyClass b(3, 4);
MyClass c = a + b;
MyClass d = a - b;
MyClass e = a * b;
MyClass f = a / b;
std::cout << "a = " << a << ", b = " << b << std::endl;
std::cout << "a + b = " << c << std::endl;
std::cout << "a - b = " << d << std::endl;
std::cout << "a * b = " << e << std::endl;
std::cout << "a / b = " << f << std::endl;
std::cout << "a == b? " << (a == b) << std::endl;
std::cout << "a > b? " << (a > b) << std::endl;
std::cout << "a < b? " << (a < b) << std::endl;
return 0;
}
```
输出结果为:
```
a = (1, 2), b = (3, 4)
a + b = (4, 6)
a - b = (-2, -2)
a * b = (3, 8)
a / b = (0, 0)
a == b? 0
a > b? 0
a < b? 1
```