C++函数重载算面积

// function_overload.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;
#define PI 3.14

double area(double a)
{
return PI*a*a;
}
double area(double a,double b)
{
return a*b;
}
double area(double c)
{
return c*c;
}
double area(double a,double b,double c)
{
return (a+b)*c/2;
}
double area(double a,double h)
{
return a*h/2;
}

int main(int argc, char* argv[])
{
double a,b,c,d,e,f,g,h,i;
cin>>a;
cout<<"圆的面积为"<<area(a);
cin>>b>>c;
cout<<"矩形的面积为"<<area(a,b);
cin>>d;
cout<<"正方形的面积为"<<area(c);
cin>>e>>f>>g;
cout<<"梯形的面积为"<<area(a,b,c);
cin>>h>>i;
cout<<"三角形的面积为"<<area(a,h);
return 0;
}
--------------------Configuration: function_overload - Win32 Debug--------------------
Compiling...
function_overload.cpp
C:\C++代码区\function_overload\function_overload.cpp(18) : error C2084: function 'double __cdecl area(double)' already has a body
C:\C++代码区\function_overload\function_overload.cpp(26) : error C2084: function 'double __cdecl area(double,double)' already has a body
C:\C++代码区\function_overload\function_overload.cpp(34) : error C2264: 'area' : error in function definition or declaration; function not called
C:\C++代码区\function_overload\function_overload.cpp(36) : error C2264: 'area' : error in function definition or declaration; function not called
C:\C++代码区\function_overload\function_overload.cpp(38) : error C2264: 'area' : error in function definition or declaration; function not called
C:\C++代码区\function_overload\function_overload.cpp(42) : error C2264: 'area' : error in function definition or declaration; function not called

改好了,底下的输入和调用参数不一致的地方也有好几处

#include <iostream>
using namespace std;
#define PI 3.14

double area(double a)
{
return PI*a*a;
}
double area(double a,double b)
{
return a*b;
}
double area_c(double c)//这个不是重载了,是重复了
{
return c*c;
}
double area(double a,double b,double c)
{
return (a+b)*c/2;
}
double area_t(double a,double h)//这个不是重载了,是重复了
{
return a*h/2;
}

int main(int argc, char* argv[])
{
double a,b,c,d,e,f,g,h,i;
cout<<"输入圆的半径:";
cin>>a;
cout<<"圆的面积为"<<area(a)<<endl;
cout<<"输入矩形两边:";
cin>>b>>c;
cout<<"矩形的面积为"<<area(b,c)<<endl;
cout<<"输入方形边:";
cin>>d;
cout<<"正方形的面积为"<<area_c(d)<<endl;
cout<<"输入梯形上下边和高:";
cin>>e>>f>>g;
cout<<"梯形的面积为"<<area(e,f,g)<<endl;
cout<<"输入三角形底和高:";
cin>>h>>i;
cout<<"三角形的面积为"<<area_t(h,i)<<endl;
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-17
C++函数重载算面积的程序:
  #include <iostream>
  #include <stdexcept>
  #include <cmath>
  struct Shape {
  virtual ~Shape() = 0 {}
  virtual double area() const = 0;
  };
  class Circle : public Shape {
  double r_;
  public:
  Circle(double const r) : r_(r) {
  if (r < 0) throw std::invalid_argument("半径不能为负");
  }
  double area() const {
  static double const pi = 3.14159265358979324;
  return pi*r_*r_;
  }
  };
  class Rectangle : public Shape {
  double x_, y_;
  public:
  Rectangle(double const x, double const y) : x_(x), y_(y) {
  if (x < 0 || y < 0) throw std::invalid_argument("长、宽不能为负");
  }
  double area() const {
  return x_*y_;
  }
  };
  class Triangle : public Shape {
  double x_, y_, z_;
  public:
  Triangle(double const x, double const y, double const z) : x_(x), y_(y), z_(z) {
  if (x < 0 || y < 0 || z < 0) throw std::invalid_argument("边长不能为负");
  }
  double area() const {
  // 海伦公式
  double const p = (x_ + y_ + z_) / 2;
  return sqrt(p*(p - x_)*(p - y_)*(p - z_));
  }
  };
  int main() {
  try {
  Shape const *s[] = { new Circle(1), new Rectangle(1, 1), new Triangle(1, 1, 1) };
  for (size_t i = 0; i != sizeof(s) / sizeof(Shape const*); ++i) {
  std::cout << "第" << i << "个图形的面积为:" << s[i]->area() << "\n";
  delete s[i];
  }
  }
  catch (std::exception const &err) {
  std::cerr << err.what() << "\n";
  }
  return 0;
  }

c++:
  C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛。C++支持多种编程范式 --面向对象编程、泛型编程和过程化编程。最新正式标准C++14于2014年8月18日公布。[1] 其编程领域众广,常用于系统开发,引擎开发等应用领域,是至今为止最受广大程序员受用的最强大编程语言之一,支持类:类、封装、重载等特性。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网