写一个程序,定义抽象基类Shape由它派出2个派生类:Circle (圆形),Rectangle(矩形),用一个函数printA

作者&投稿:漕韩 (若有异议请与网页底部的电邮联系)
C++问题问题,/*写一个程序,定义抽象基类shape,由他派生出3个派生类:circle(圆形),rectangle(矩形~

//错误略多。。看我的注释


#include
using namespace std;
class shape
{
protected:
virtual float area() {return 0;}//面积还是返回浮点数比较合适
virtual void shapename() = 0;
};
class circle:public shape
{
protected:
int x;
int y;
int r;
public:
circle(){x=0;y=0;r=0;}
circle(int a,int b,int c){x=a;y=b;r=c;}
void shapename() {cout<<"我是圆啊"<<endl;}
float area(){return 3.14*r*r;}
};
class rectangle:public shape
{
protected:
int x,y;
public:
rectangle(){x=0;x=0;}
rectangle(int a,int b){x=a,y=b;}
void shapename(){cout<<"我是长方形啊"<<endl;}
float area(){return x*y;}
};
class triangle:public shape
{
protected:
int h,x;
public:
triangle(){h=0;x=0;}
triangle(int a,int b){h=a;x=b;}
void shapename(){cout<<"我是三角形啊"<<endl;}
float area(){return 1.0*h*x/2;}//为使其返回正确的浮点数,在运算时加一个1.0*
};
void printarea(circle c,rectangle r,triangle t)//这个函数仅仅是打印信息,没必要声明一个类来将它包含进去,然后又再多继承...只需要把它写成一个普通的全局函数即可
{
cout<<"circlearea is "<<c.area()<<endl;
cout<<"rectanglearea is "<<r.area()<<endl;
cout<<"trianglearea is "<<t.area()<<endl;
}
void main()
{
circle c(1,1,1);
rectangle r(1,1);
triangle t(1,1);
printarea(c,r,t);//传递参数给printarea函数
}

不晓得你显示所有图形的总面积的函数放在哪里?

const double PI = 3.14;

class Shape
{
public:
virtual double ShowArea() = 0;
};

class Circle:public Shape
{
private:
double radius;
public:
Circle(double r):radius(r){}
double ShowArea(){return PI*radius*radius;}
};

class Rectangle:public Shape
{
private:
double length;
double width;
public:
Rectangle(double l, double w):length(l), width(w){}
double ShowArea(){return length*width;}
};

class Square:public Shape
{
private:
double length;
public:
Square(double l):length(l){}
double ShowArea(){return length*length;}
};

void main()
{
Circle circle(2);
Rectangle rect(3,4);
Square square(5);

Shape * arr[3] = {&circle, &rect, &square};

for(int i=0; i<3; i++)
{
cout ShowArea() << endl;
}
}

#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();
}

看看这个符合不



//下面是C++版,因为比较简单,所以成员函数直接就在类内定义了
#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);

        }

    }

}




编写一个程序,其中包括一些接口、类、抽象类的定义,定义它们的成员(属 ...
什么语言的呢,都没有相应的业务要求,,,~~~

编写java程序,程序中定义一个抽象类Area、两个Area类的派生子类RectArea...
abstract class Area{ abstract double area();} \/\/矩形面积类 class RectArea extends Area{ private double width; \/\/宽 private double height; \/\/高 public RectArea(){ } public RectArea(double width, double height){ this.width = width;this.height = height;} public double area(...

java程序题:定义一个抽象类-水果,其中包括getWeight()方法,编写程序分别...
水果类 abstract public class Fruit {abstract public double getWeight();} 苹果类 public class Apple extends Fruit {private double weight;public Apple(double weight) {this.weight = weight;}@Overridepublic double getWeight() {return weight;}} 橘子类 public class Orange extends Fruit {...

抽象类的应用场景
3. 代码复用在大型应用程序中,代码复用是关键。通过定义抽象类,我们可以定义一些通用的行为和接口,并被多个不同的子类共享。这样,我们不仅可以减少代码的重复性,而且也可以改善代码的可维护性和稳定性。4. 可扩展性当我们设计一个应用程序时,我们通常希望它具有可扩展性。这意味着,我们需要一种设计...

java程序设计:创建一个打印类Printer,定义抽象方法print();
public abstract class Printer { public abstract void print();} class A extends Printer{ Override public void print() { System.out.println("---AAAAAAAAAA---");} } class B extends Printer{ Override public void print() { System.out.println("---BBBBBBBBBB---");} } class C...

求大神帮忙用JAVA编个程序,定义抽象类Function,其中包含抽象方法doFunct...
interface Function { public float doFunction(float x,float y);} class Function11 implements Function{ Override public float doFunction(float x, float y) { \/\/ TODO Auto-generated method stub return x+1;} } class Function12 implements Function{ Override public float doFunction(float ...

什么是抽象方法?
抽象类中可以定义构造函数,但是不能使用关键字new 创建其对象。 如果某个类继承于抽象类,应将父类的抽象方法都覆盖,否则子类也是一个抽象类。覆盖实现则可以创建子类的对象。 静态方法和final方法都不能声明为抽象。 数据抽象方法的定义 数据类型是和数据结构密切相关的一个概念。它最早出现在高级程序设计语言中,用...

C++多态性实验 定义一个抽象类—容器内container,其中定义了若干纯虚...
这样:include<iostream> using namespace std;class container \/\/定义抽象类conyainer {public:container(...)virtual xx(){}; \/\/虚函数 virtual xx(){}; \/\/虚函数 virtual void xxx() const =0; \/\/纯虚函数 private:...};class xxxx:public container \/\/公用派生类xxxx {public:vitual ...

抽象方法有什么作用?
抽象类本身不具备实际的功能,只能用于派生其子类。抽象类中可以包含构造方法,但是构造方法不能被声明为抽象。 抽象类不能用final来修饰,即一个类不能既是最终类又是抽象类。 abstract不能与private、static、final、native并列修饰同一个方法。 程序举例:abstract class Animal 定义抽象类 abstract void...

编写一个java程序,其中包含至少一个抽象类以及一个接口,再定义一个类...
接口 package com.cmb.reqmgr;public interface HelloInterface { public void sayHello();} 接口实现 package com.cmb.reqmgr;public class HellpImpl implements HelloInterface { public void sayHello() { System.out.println("hello");} } 抽象类 package com.cmb.reqmgr;public abstract class ...

淮滨县19867924704: JAVA程序编写. 定义一个抽象类shape ,用来表示一般图形. -
苗闵瑞诺: Java程序: public class Main {public static void main(String[] args) {Shape s = null;s = new Circle(3); System.out.println("圆的面积:" + s.area());System.out.println("圆的周长:" + s.perimeter());} }/*** 形状类:抽象类* @...

淮滨县19867924704: 定义一个抽象类Shape,在其中说明一个纯虚函数area()作为一个接口,用来计算圆形、三角形、正方形和长方形的面积.要求由抽象类派生出圆形、三角形... -
苗闵瑞诺:[答案] class Shape{public: virtual float area() const{return 0;}};class Circle{private: float radius;public: float area() const{return 3.14159*radius * radius};其他的依次类似定义纯虚函数就是在声明虚函数...

淮滨县19867924704: 写一个程序,定义抽象基类Shape由它派出2个派生类:Circle (圆形),Rectangle(矩形),用一个函数printA -
苗闵瑞诺: //下面是C++版,因为比较简单,所以成员函数直接就在类内定义了 #include<iostream> using namespace std; #define pi 3.14 class Shape//定义抽象基类,仅供继承用 {virtual void printArea()=0;//纯虚函数 };class Circle:Shape { private:...

淮滨县19867924704: 程序题.写一个C++程序,定义抽象基类Shape,由它派生出一个基类Point,再由Point派生出2个派生类 -
苗闵瑞诺: #include<iostream.h> #include<cmath> const double pi=3.14; class Shape//抽象基类 { public:virtual void display()=0; };class Point:public Shape { public:void display(){}; };class Circle:public Point { private:double r; public:void set(double r){ r=r;...

淮滨县19867924704: 要求定义一个描述形状的抽象类shape,类内包括求面积的area和求各图形总面积的total函数.1.从shape派生出三角形,圆形,正方形类,要求类中有构造函... -
苗闵瑞诺:[答案] class Trapezoid : public Shape{private:double top;double bottom;double height;public:Trapezoid(double t, double b, double h){top = t;bottom = b;height = h;}double Area(){return (top + bottom) * height...

淮滨县19867924704: 写一个程序,定义抽象基类Shape,由他派生出3个派生类,Circle(圆形)、 -
苗闵瑞诺: 虚函数

淮滨县19867924704: 用c++写一个程序,定义抽象类型Shape,由它派生三个类:Circle(圆形),Rectangle(矩 -
苗闵瑞诺: 已修改,你看看 int main() { Circle c1(3.0); Rectangle r1(4.2,5.5); Square s1(1.2); Shape *arr[3]={&c1,&r1,&s1}; for(int i=0;iShowArea()ShowArea(); } //delete arr; //不是new出来的对象,不用delete return 0; }

淮滨县19867924704: 写一个程序,定义抽象基类Cshape,由它派生出5个派生类: -
苗闵瑞诺: #include using namespace std; //定义抽象基类Shape class Shape {public: virtual double area() const =0; //纯虚函数 }; //定义Circle(圆形)类 class Circle:public Shape {public: Circle(double r):radius(r){} //结构函数 virtual double area...

淮滨县19867924704: C++试用纯虚函数和抽象类编程设计几个图形,有长方形,三角形,圆等,这几个图形均有显示,移动,复制等功能如题 -
苗闵瑞诺:[答案] class CShape{ public: virtual void Display() = 0; // 纯虚函数,该类不能实例化 &...

淮滨县19867924704: 定义一个抽象基类Shape,它包含一个抽象方法getArea(),从Shape类派生出Rectangle和Circle类,这两个类都 -
苗闵瑞诺: public class Test { public static void main(String[] args) { Rectangle rectangle = new Rectangle(3, 5...

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