C#编写一个程序,计算三角形,正方形和圆形的面积,
要求抽象出一个基类BASE利用派生类定义三角形正方形圆形main方法中实现三个对象调用相同方法实现不同求面积结果急求啊拜托...
要求抽象出一个基类BASE 利用派生类定义三角形 正方形 圆形
main方法中实现三个对象调用相同方法实现不同求面积结果
急求啊 拜托 展开
main方法中实现三个对象调用相同方法实现不同求面积结果
急求啊 拜托 展开
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AreaDemo
{
class Program
{
const double PI = 3.1415926;
static void Main(string[] args)
{
//求三角形面积例子,另外两个求法类同
Console.WriteLine("请输入三角形的相应数据:");
Console.Write("底边长:");
string bases = Console.ReadLine();
Console.Write("高:");
string height = Console.ReadLine();
Area method = new Area();
double area = method.GetTriangleArea(Convert.ToDouble(bases), Convert.ToDouble(height));
Console.WriteLine("三角形面积为:" + area.ToString());
Console.Read();
}
class Area
{
/// <summary>
/// 求三角形面积
/// </summary>
/// <param name="bases">底边</param>
/// <param name="height">高</param>
/// <returns>面积</returns>
public double GetTriangleArea(double bases, double height)
{
return bases * height / 2;
}
/// <summary>
/// 求正方形面积
/// </summary>
/// <param name="side">边长</param>
/// <returns>面积</returns>
public double GetSquareArea(double side)
{
return side * side;
}
/// <summary>
/// 求圆面积
/// </summary>
/// <param name="radius"></param>
/// <returns></returns>
public double GetCircleArea(double radius)
{
return PI * radius * radius;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AreaDemo
{
class Program
{
const double PI = 3.1415926;
static void Main(string[] args)
{
//求三角形面积例子,另外两个求法类同
Console.WriteLine("请输入三角形的相应数据:");
Console.Write("底边长:");
string bases = Console.ReadLine();
Console.Write("高:");
string height = Console.ReadLine();
Area method = new Area();
double area = method.GetTriangleArea(Convert.ToDouble(bases), Convert.ToDouble(height));
Console.WriteLine("三角形面积为:" + area.ToString());
Console.Read();
}
class Area
{
/// <summary>
/// 求三角形面积
/// </summary>
/// <param name="bases">底边</param>
/// <param name="height">高</param>
/// <returns>面积</returns>
public double GetTriangleArea(double bases, double height)
{
return bases * height / 2;
}
/// <summary>
/// 求正方形面积
/// </summary>
/// <param name="side">边长</param>
/// <returns>面积</returns>
public double GetSquareArea(double side)
{
return side * side;
}
/// <summary>
/// 求圆面积
/// </summary>
/// <param name="radius"></param>
/// <returns></returns>
public double GetCircleArea(double radius)
{
return PI * radius * radius;
}
}
}
}
展开全部
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class base
{
public:
virtual void disp() = 0;
};
class triangle : public base
{
public:
triangle(int base, int height) : m_base(base), m_height(height) {}
void disp()
{
cout << "三角形面积: " << m_base* m_height / 2 << endl;
}
private:
int m_base;
int m_height;
};
class square : public base
{
public:
square(int side) : m_side(side) {}
void disp()
{
cout << "正方形面积: " << m_side* m_side << endl;
}
private:
int m_side;
};
class circle : public base
{
public:
circle(int radius) : m_radius(radius) {}
void disp()
{
cout << "圆形面积: "
<< setprecision(5) // nasty!!
<< m_radius * m_radius * M_PI << endl;
}
private:
int m_radius;
};
#include <iomanip>
#include <cmath>
using namespace std;
class base
{
public:
virtual void disp() = 0;
};
class triangle : public base
{
public:
triangle(int base, int height) : m_base(base), m_height(height) {}
void disp()
{
cout << "三角形面积: " << m_base* m_height / 2 << endl;
}
private:
int m_base;
int m_height;
};
class square : public base
{
public:
square(int side) : m_side(side) {}
void disp()
{
cout << "正方形面积: " << m_side* m_side << endl;
}
private:
int m_side;
};
class circle : public base
{
public:
circle(int radius) : m_radius(radius) {}
void disp()
{
cout << "圆形面积: "
<< setprecision(5) // nasty!!
<< m_radius * m_radius * M_PI << endl;
}
private:
int m_radius;
};
更多追问追答
追问
。。。这好像不对吧
追答
哪儿有问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询