写一个程序,定义抽象基类Shape由它派出2个派生类:Circle (圆形),Rectangle(矩形),用一个函数printA 5
期待朋友的帮助,谢谢!期待有心的朋友们给予完整的回复的,谢谢了啦!考试用!急 展开
#include <iostream>
using namespace std;
#define PI 3.14159
class Shape {
public:
virtual void printArea() = 0;
};
class Circle : public Shape 闹渗{
public:
Circle(float Radius) {
mRadius = Radius;
}
void printArea() {
cout<<"The Circle's Area 液正脊is "<<PI*mRadius*mRadius<<endl;
}
private:
float mRadius;
};
class Rectangle : public Shape {
public:
Rectangle(float Length, float Height) {
mLength = 清数Length;
mHeight = Height;
}
void printArea() {
cout<<"The Rectangle's Area is "<<mLength*mHeight<<endl;
}
private:
float mLength, mHeight;
};
int main() {
Circle mCircle(2.0);
mCircle.printArea();
Rectangle mRectangle(3.0, 4.0);
mRectangle.printArea();
}
看看这个符合不
#include<iostream>
using namespace std;
#define pi 3.14
class Shape//定义抽象基类,搭行仅供继承用
{
virtual void printArea()=0;//纯虚函数
};
class Circle:Shape
{
private:
double radi;
public:
Circle(double ra)
{radi=ra;}
void printArea()
{cout<<"圆的面积为:"<<pi*radi*radi<<endl;}
};
class Rectangle:Shape
{
private:
double height;
double length;
public:
Rectangle(double hei,double len)
{height=hei;length=len;}
void printArea()
{cout<<"矩形的面积为:"<<height*length<<endl;}
};
void main()
{
Circle cir(4);
Rectangle rect(5,4);
cir.printArea();
rect.printArea();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
abstract class Shape
{
返陪旅 public abstract void printArea()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Circle:Shape
{
public Circle(double r,double r1)
{
this.R1 = r;
this.R2 = r1;
乱高 }
private const double R = 3.14;
private double r1;
public double R1
{
get { return r1; }
set { r1 = value; }
}
漏凳 private double r2;
public double R2
{
get { return r2; }
set { r2 = value; }
}
public void printArea()
{
double result = R1 * R2 * R;
Console.Write(result);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Rectangle:Shape
{
public Rectangle(double length, double width)
{
this.Length = length;
this.Width = width;
}
private double length;
public double Length
{
get { return length; }
set { length = value; }
}
private double width;
public double Width
{
get { return width; }
set { width = value; }
}
public override void printArea()
{
double result = this.Width * this.length;
Console.Write(result);
}
}
}