KiKi理解了继承可以让代码重用,他现在定义一个基类shape,私有数据为坐标点x,y, 由它派生Rectangle类和Circle类,它们都有成员函数GetArea()求面积。派生类Rectangle类有数据:矩形的长和宽;派生类Circle类有数据:圆的半径。Rectangle类又派生正方形Square类,定义各类并测试。输入三组数据,分别是矩形的长和宽、圆的半径、正方形的边长,输出三组数据,分别是矩形、圆、正方形的面积。圆周率按3.14计算。
输入三行,第一行为矩形的长和宽,第二行为圆的半径,第三行为正方形的边长。
三行,分别是矩形、圆、正方形的面积。
7 8 10 5
56 314 25
# 看了一圈都是C++,我贡献个python码吧 class shape(): def __init__(self, x, y): self.x = x self.y = y self.GetArea() def GetArea(self): return int(self.x) * int(self.y) class Rectangle(shape): def __init__(self, x, y): shape.__init__(self, x, y) class Square(Rectangle): def __init__(self, x): super().__init__(x, y) self.x = x def GetArea(self): return int(self.x) ** 2 class Circle(shape): def __init__(self, x): super().__init__(x, y) self.x = x def GetArea(self): return (314 * int(self.x) ** 2)/100 x, y = input().split() s1 = Rectangle(x, y) x = input() s2 = Circle(x) temp = s2.GetArea() if temp == int(temp): temp = int(temp) x = input() s3 = Square(x) print(Rectangle.GetArea(s1)) print(temp) print(Square.GetArea(s3))
#include<iostream>
#define PAI 3.14
using namespace std;
class Shape
{
private:
int x,y;
public:
virtual double GetArea ()
{
return 0;
}
};
class Rectangle : public Shape
{
public:
void setRectangle(double x,double y)
{
length=x;
width=y;
}
virtual double GetArea()
{
return length*width;
}
private:
double length,width;
};
class Circle : public Shape
{
public:
void setCircle(double r)
{
ridus=r;
}
double GetArea()
{
return PAI*ridus*ridus;
}
private:
double ridus;
};
class Square : public Rectangle
{
public:
void setSquare(double x)
{
side=x;
}
double GetArea()
{
return side*side;
}
private:
double side;
};
int main()
{
Rectangle rec;
Circle cir;
Square squ;
double a,b,c,d;
cin>>a>>b>>c>>d;
rec.setRectangle(a, b);
cir.setCircle(c);
squ.setSquare(d);
cout<<rec.GetArea()<<endl;
cout<<cir.GetArea()<<endl;
cout<<squ.GetArea()<<endl;
return 0;
} import java.awt.font.FontRenderContext;
import java.awt.geom.Area;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.io.IOException;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int width = sc.nextInt();
int r = sc.nextInt();
int side = sc.nextInt();
Rectangle rectangle = new Rectangle(length, width);
Circle circle = new Circle(r);
Square square = new Square(side);
System.out.println(rectangle.GetArea(length, width));
double AreaCircle = circle.GetArea(r);
int AreaCircle1 = (int)circle.GetArea(r);
DecimalFormat df = new DecimalFormat("###.0");
double temp;
if (AreaCircle > AreaCircle1){
temp = Double.parseDouble(df.format(AreaCircle));
if (temp != AreaCircle)
System.out.println(String.format("%.2f", AreaCircle));
else
System.out.println(AreaCircle);
}
else
System.out.println(AreaCircle1);
System.out.println(square.GetArea(side));
}
}
class shape{
private int x;
private int y;
public shape(int x, int y){
this.x = x;
this.y = y;
}
}
class Rectangle extends shape{
private int length;
private int width;
public Rectangle(int length, int width){
super(length, width);
this.length = length;
this.width = width;
}
public int GetArea(int length, int width){
int Area = length * width;
return Area;
}
}
class Circle extends shape{
int r;
public Circle(int r){
super(r, r);
this.r = r;
}
public double GetArea(int r){
double Area;
Area = 3.14 * r * r;
return Area;
}
}
class Square extends Rectangle{
int length;
public Square(int length){
super(length, length);
this.length = length;
}
public int GetArea(int length){
int Area;
Area = length * length;
return Area;
}
}
#include <iostream>
(720)#include <stdio.h>
using namespace std;
const float PI = 3.14;
class Shape
{
public:
virtual float GetArea() = 0;
private:
int x;
int y;
};
class Rectangle : public Shape
{
public:
virtual float GetArea()
{
return length * wide;
}
void set(int length, int wide)
{
this->length = length;
this->wide = wide;
}
private:
int length;
int wide;
};
class Square : public Rectangle
{
public:
virtual float GetArea()
{
return length * length;
}
void set(int length)
{
this->length = length;
}
private:
int length;
};
class Circle : public Shape
{
public:
virtual float GetArea()
{
return PI * radius * radius;
}
void set(int radius)
{
this->radius = radius;
}
private:
int radius;
};
void print(Shape& s)
{
cout << s.GetArea() << endl;
}
int main()
{
int a, b;
cin >> a >> b;
Rectangle r;
r.set(a, b);
cin >> a;
Circle c;
c.set(a);
cin >> a;
Square s;
s.set(a);
print(r);
print(c);
print(s);
return 0;
} #include<iostream>
using namespace std;
class shape{
private:
int x;
int y;
public:
shape() : x(0), y(0){ }
shape(int a, int b) : x(a), y(b){ }
virtual void GetArea() = 0;
};
class Circle : public shape{
private:
double radius;
const static double PI;
public:
Circle() : radius(0) { }
Circle(int r) : radius(r) { }
virtual void GetArea(){
double S;
S = PI * radius * radius;
cout << S << endl;
}
};
const double Circle::PI = 3.14;
class Retangle : public shape{
private:
int w;
int h;
public:
Retangle() : w(0), h(0){ }
Retangle(int x, int y) : w(x), h(y){ }
virtual void GetArea(){
double S;
S = w * h;
cout << S << endl;
}
};
class Square : public Retangle{
private:
int a;
public:
Square() : a(0){ }
Square(int x) : a(x){ }
virtual void GetArea(){
double S;
S = a * a;
cout << S << endl;
}
};
int main(){
int a, b, c, d;
cin >> a >> b >> c >> d;
Retangle re(a, b);
Circle C(c);
Square s(d);
shape* p;
p = &re;
p->GetArea();
p = &C;
p->GetArea();
p = &s;
p->GetArea();
return 0;
} #include<stdio.h>
struct Circle
{
int radicus;
};
struct Square
{
int side_length;
};
struct Recatangle
{
int length;
int width;
struct Square SL; //结构体内衍生新的结构体
};
void GetArea(struct Recatangle * r,struct Circle* x)//解引用输出
{
printf("%d\n",r->length*r->width);
printf("%g\n",3.14*x->radicus*x->radicus);
printf("%d\n",r->SL.side_length*r->SL.side_length);
}
int main()
{
struct Recatangle r={0};
struct Circle x={0};
//struct Square y={0};
scanf("%d %d\n%d\n%d",&r.length,&r.width,&x.radicus,&r.SL.side_length);
GetArea(&r,&x);//指针传参
return 0;
} #include<stdio.h>
#define PI 3.14
//基类
typedef struct shape
{
int x;
int y;
}shape;
//矩形
typedef struct Rectangle
{
int length;
int width;
}rectangle;
int rectangle_GetArea(rectangle*r)
{
return r->length*r->width;
}
//圆形
typedef struct Circle
{
float x;
}circle;
float circle_GetArea(circle*c)
{
return c->x*c->x*PI;
}
//正方形
typedef struct Square
{
int x;
}square;
int square_GetArea(square*s)
{
return s->x*s->x;
}
rectangle r;
circle c;
square s;
int main()
{
rectangle rect;
circle circle;
square square;
// 输入矩形的长和宽
scanf("%d %d", &rect.length, &rect.width);
// 输入圆的半径
scanf("%g", &circle.x);
// 输入正方形的边长
scanf("%d", &square.x);
// 输出矩形面积
printf("%d\n", rectangle_GetArea(&rect));
// 输出圆的面积
printf("%g\n", circle_GetArea(&circle));
// 输出正方形面积
printf("%d\n", square_GetArea(&square));
} #include <stdio.h>
int main() {
struct S
{
int a, b;
int c;
int d;
} S;
scanf("%d %d %d %d", &S.a, &S.b, &S.c, &S.d);
int ret=S.a*S.b;
if (S.c%10==0)
{
printf("%d\n%.0f\n%d", ret, S.c*S.c*3.14, S.d*S.d);
}
else if(S.c%5==0 && S.c%10!=0)
{
printf("%d\n%.1f\n%d", ret, S.c*S.c*3.14, S.d*S.d);
}
else
{
printf("%d\n%.2f\n%d", ret, S.c*S.c*3.14, S.d*S.d);
}
return 0;
} #include <stdio.h>
struct
{
int length;
int broad;
}oblong;
struct
{
int r;
}cir;
struct
{
int length;
}rec;
int main()
{
const float pi = 3.14;
scanf("%d%d", &oblong.broad, &oblong.length);
scanf("%d", &cir.r);
scanf("%d", &rec.length);
printf("%d\n", oblong.length * oblong.broad);
printf("%g\n", cir.r * cir.r * pi);
printf("%d\n", rec.length * rec.length);
return 0;
} #include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int c;
scanf("%d",&c);
int d;
scanf("%d",&d);
printf("%d\n",a*b);
printf("%g\n",3.14*(c*c));
printf("%d\n",d*d);
return 0;
} #include <stdio.h>
float GetArea(int shape, int x, int y, int r) {
float m = 0;
switch (shape ) {
case 1:
m = x * y;
break;
case 2:
m = 3.14 * r * r;
break;
case 3:
m = x * x;
break;
default:
break;
}
return m;
}
int main() {
int x = 0, y = 0, r = 0;
scanf("%d%d", &x, &y);
int re = (int)GetArea(1, x, y, r);
scanf("%d", &r);
float c = GetArea(2, x, y, r);
scanf("%d", &x);
int s = (int)GetArea(3, x, y, r);
printf("%d\n", re);
int cc = (int)c;
if (cc == c) printf("%d\n", cc);
else printf("%g\n", c);
printf("%d\n", s);
return 0;
} #include <iostream>
using namespace std;
class shape{
private:
int x;
int y;
public:
shape(int x = 0,int y = 0):x(x),y(y){}
};
class Rectangle:public shape{
private:
int length;
int wide;
public:
Rectangle(int l = 0 ,int w = 0):length(l),wide(w),shape(){}
int GetArea()
{
return length*wide;
}
};
class Square:public Rectangle{
private:
int a;
public:
Square(int a=0):a(a),Rectangle(){}
int GetArea()
{
return a*a;
}
};
class Circle:public shape{
private:
int r;
public:
Circle(int r = 0):r(r),shape(){}
float GetArea()
{
return 3.14*r*r;
}
};
int main()
{
int len,wid,r,a;
cin>>len>>wid>>r>>a;
Rectangle rec = Rectangle(len,wid);
Circle cir = Circle(r);
Square squ = Square(a);
cout<<rec.GetArea()<<endl;
cout<<cir.GetArea()<<endl;
cout<<squ.GetArea()<<endl;
return 0;
}