优秀的Aurora 发表于 2020-4-28 17:15:09

卑微求助,简单程序改错。

定义抽象类Geometry,含有计算几何图形周长的纯虚函数double Circumfer();含有计算面积的纯虚函数double Area();在此基础上公有的派生出矩形类Rectangle,和圆类Circle,并且分别给出计算周长函数Circumf()和计算面积函数Area()的定义。最后通过函数fun()分别输出面积和周长。

#include<iostream>
using namespace std;
const double pi = 3.14;

class Geometry{
public:
        Geometry();
        virtual double Circumfer() const=0;                //纯虚函数
        virtual double Area() const=0;
};

class Rectangle:public Geometry{
public:
        Rectangle(double A, double B) {
                a = A;
                b = B;
        }
        double Circumf() const;
        double Area() const;
        void fun();
private:
        double a, b;
};


double Rectangle::Circumf() const {
        double l,a,b;
        l = 2*a+2*b;
}

double Rectangle::Area() const {
        double s,a,b;
        s = a * b;

}


class Circle:public Geometry {
public:
        Circle(int R) {
                r = R;
        }
        double Circumf() const;
        double Area() const;
        void fun();
private:
        int r;
};

double Circle::Circumf() const {
        double l,r;
        l = 2 *pi*r;
}

double Circle::Area() const {
        double s,r;
        s = pi*r*r;

}

void fun(const Geometry * ptr)
{
        ptr->Circumfer();
        ptr->Area();       
}

int main()
{
        Rectangle x1(2, 3);
        Rectangle* p1;
                                             
                cout << "Rectangle:" << endl;
        fun(p1);


        Circle y1(5);
        Circle* p2;

                cout << "Circle:" << endl;
        fun(p2);

        return 0;
}



错误如下:由于知识有限,不太会改。。。
错误(活动)        E0322        不允许使用抽象类类型 "Rectangle" 的对象:       
错误(活动)        E0322        不允许使用抽象类类型 "Circle" 的对象:               
错误        C2259        “Rectangle”: 无法实例化抽象类               
错误        C2259        “Rectangle”: 无法实例化抽象类               
错误        C2259        “Circle”: 无法实例化抽象类       
错误        C2259        “Circle”: 无法实例化抽象类               




人造人 发表于 2020-4-28 17:33:51

#include <iostream>

using namespace std;
const double pi = 3.14;

class Geometry {
public:
    Geometry() {}
    virtual double Circumfer() const = 0;
    virtual double Area() const = 0;
};

class Rectangle: public Geometry {
public:
    Rectangle(double A, double B) {
      a = A;
      b = B;
    }
    double Circumfer() const;
    double Area() const;
private:
    double a, b;
};

double Rectangle::Circumfer() const {
    return 2 * a + 2 * b;
}

double Rectangle::Area() const {
    return a * b;
}

class Circle: public Geometry {
public:
    Circle(int R) {
      r = R;
    }
    double Circumfer() const;
    double Area() const;
private:
    int r;
};

double Circle::Circumfer() const {
    return 2 * pi * r;
}

double Circle::Area() const {
    return pi * r * r;
}

void fun(const Geometry *ptr) {
    std::cout << ptr->Circumfer() << " " << ptr->Area();
}

int main() {
    Rectangle x1(2, 3);
    cout << "Rectangle:" << endl;
    fun(&x1);
    std::cout << std::endl;

    Circle y1(5);
    cout << "Circle:" << endl;
    fun(&y1);
    std::cout << std::endl;
    return 0;
}

人造人 发表于 2020-4-28 17:36:19

#include <iostream>

using namespace std;
const double pi = 3.14;

class Geometry {
public:
    Geometry() {}
    virtual double Circumfer() const = 0;
    virtual double Area() const = 0;
};

class Rectangle: public Geometry {
public:
    Rectangle(double A, double B) {
      a = A;
      b = B;
    }
    double Circumfer() const;
    double Area() const;
private:
    double a, b;
};

double Rectangle::Circumfer() const {
    return 2 * a + 2 * b;
}

double Rectangle::Area() const {
    return a * b;
}

class Circle: public Geometry {
public:
    Circle(int R) {
      r = R;
    }
    double Circumfer() const;
    double Area() const;
private:
    int r;
};

double Circle::Circumfer() const {
    return 2 * pi * r;
}

double Circle::Area() const {
    return pi * r * r;
}

void fun(const Geometry *ptr) {
    std::cout << ptr->Circumfer() << " " << ptr->Area() << std::endl;
}

int main() {
    Rectangle x1(2, 3);
    cout << "Rectangle:" << endl;
    fun(&x1);

    Circle y1(5);
    cout << "Circle:" << endl;
    fun(&y1);
    return 0;
}
页: [1]
查看完整版本: 卑微求助,简单程序改错。