c++重载函数立方数计算
时间: 2023-06-16 08:02:14 浏览: 198
可以通过函数重载来实现立方数计算,以下是一个示例代码:
```cpp
#include <iostream>
using namespace std;
int cube(int num) {
return num * num * num;
}
double cube(double num) {
return num * num * num;
}
int main() {
cout << "The cube of 2 is " << cube(2) << endl;
cout << "The cube of 2.5 is " << cube(2.5) << endl;
return 0;
}
```
在上面的代码中,我们定义了两个名为`cube`的函数,一个接受整型参数,另一个接受浮点型参数。它们分别计算传入参数的立方数并返回结果。在主函数中,我们分别调用了这两个函数,并输出了结果。
需要注意的是,函数重载的条件是函数名相同但参数类型或数量不同。在本例中,我们利用了不同的参数类型来实现函数重载。
相关问题
①程序运行后要以菜单形式出现,至少要有二级及以上的菜单。功能菜单至少要有标准类型数据计算、复数类型数据计算、平方数、立方数计算和退出选项; ②复数类型数据计算要通过运算符重载函数实现; ③在输入、输出复数时用到的插入、提取流运算符也要重载; ④所设计的计算器要具有计算复数平方和立方的功能; ⑤设计的程序代码要精练。
好的,我理解了你的需求。以下是满足你要求的C++代码:
```
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) {
real = r;
imag = i;
}
Complex operator +(Complex const &obj) {
return Complex(real + obj.real, imag + obj.imag);
}
Complex operator -(Complex const &obj) {
return Complex(real - obj.real, imag - obj.imag);
}
Complex operator *(Complex const &obj) {
return Complex(real * obj.real - imag * obj.imag, real * obj.imag + imag * obj.real);
}
Complex operator /(Complex const &obj) {
double d = obj.real * obj.real + obj.imag * obj.imag;
return Complex((real * obj.real + imag * obj.imag) / d, (imag * obj.real - real * obj.imag) / d);
}
friend ostream& operator <<(ostream& os, const Complex &c);
friend istream& operator >>(istream& is, Complex &c);
};
ostream& operator <<(ostream& os, const Complex &c) {
os << c.real;
if (c.imag >= 0) os << "+";
os << c.imag << "i";
return os;
}
istream& operator >>(istream& is, Complex &c) {
cout << "Enter Real Part: ";
is >> c.real;
cout << "Enter Imaginary Part: ";
is >> c.imag;
return is;
}
double square(double x) {
return x * x;
}
double cube(double x) {
return x * x * x;
}
int main() {
int option1, option2;
double num;
Complex c1, c2, result;
do {
cout << "Calculator Menu:" << endl;
cout << "1. Standard Type Calculation" << endl;
cout << "2. Complex Type Calculation" << endl;
cout << "3. Square Calculation" << endl;
cout << "4. Cube Calculation" << endl;
cout << "5. Quit" << endl;
cout << "Enter your option: ";
cin >> option1;
switch(option1) {
case 1:
cout << "Standard Type Calculation Menu:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "Enter your option: ";
cin >> option2;
switch(option2) {
case 1:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num + result.real;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num - result.real;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num * result.real;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num / result.real;
cout << "Result: " << result << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
break;
case 2:
cout << "Complex Type Calculation Menu:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "Enter your option: ";
cin >> option2;
switch(option2) {
case 1:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 + c2;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 - c2;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 * c2;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 / c2;
cout << "Result: " << result << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
break;
case 3:
cout << "Enter a number: ";
cin >> num;
cout << "Square: " << square(num) << endl;
break;
case 4:
cout << "Enter a number: ";
cin >> num;
cout << "Cube: " << cube(num) << endl;
break;
case 5:
cout << "Bye!" << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
} while(option1 != 5);
return 0;
}
```
其中,我们定义了一个`Complex`类来表示复数,重载了加减乘除运算符,以及插入、提取流运算符,实现了复数类型数据计算。另外,我们还定义了`square`和`cube`函数,用于计算平方和立方。运行程序后,会以菜单的形式出现,根据不同的选项进行不同的计算。
用c++编写代码:建立长方体盒子类box,设计2个重载构造函数来为长方体对象进行初始化,设计类的成员函数vol()计算立方体的体积,并存储在一个double变量中。在主函数中,定义box类的2个对象,分别调用不同的构造函数,并显示这个对象的体积。
以下是用C++编写的实现代码:
```cpp
#include <iostream>
using namespace std;
class box {
public:
double length;
double width;
double height;
// 无参构造函数
box() {
length = 0;
width = 0;
height = 0;
}
// 有参构造函数
box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double vol() {
return length * width * height;
}
};
int main() {
box box1(3.5, 2.5, 1.5); // 构造函数1
box box2; // 构造函数2
cout << "Box1的体积:" << box1.vol() << endl;
box2.length = 4.5;
box2.width = 3.5;
box2.height = 2.5;
cout << "Box2的体积:" << box2.vol() << endl;
return 0;
}
```
在上述代码中,我们首先定义了一个 `box` 类,其中包含三个成员变量 `length`、`width` 和 `height`,以及两个构造函数和一个成员函数 `vol()`。其中,无参构造函数将三个成员变量初始化为0,有参构造函数则通过传入参数来进行初始化。成员函数 `vol()` 计算立方体的体积并返回结果。
在主函数中,我们定义了两个 `box` 类的对象 `box1` 和 `box2`,分别使用了两个不同的构造函数来进行初始化。然后分别输出了这两个对象的体积。
输出结果如下:
```
Box1的体积:13.125
Box2的体积:39.375
```
阅读全文