鱼C论坛

 找回密码
 立即注册
查看: 1919|回复: 1

[已解决]两个代码都有问题,希望大佬帮忙修正一下

[复制链接]
发表于 2019-12-23 17:25:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;
class Point{
protected:
        double x;
        double y;
public:
        Point(double a=0,double b=0){
                x=a;
                y=b;
        }
        double getx(){
                return x;
        }
        double gety(){
                return y;
        }
        void print(){
                cout<<"("<<x<<","<<y<<")"<<endl;
        }
};
class Distance:public Point{
protected:
        Point p1(4,5);
public:
        Distance(double c,double d):Point(c,d)
        void print(){
                cout<<sqrt(pow(p1.getx()-c,2)+pow(p1.gety()-d))<<endl;
                cout<<"("<<p1.getx()<<","<<p1.gety()<<")("<<c<<","<<d<<")"<<endl;
        }
};
int main(){
        Distance d1(0,0);
        d1.print();
        return 0;
}
#include<iostream>
using namespace std;
class Square{
protected:
        double bc;
        double zc;
public:
        Square(double bc=0);
        void virtual f()=0;
        void print(){
                cout<<zc<<endl;
        }
};
class Rectangular:public Square{
        double bc1;
public:
        Rectangular(double a,double b):Square(a){
                bc1=b;
        }
        void f(){
                zc=2*(bc+bc1);
        }
        void print(){
                cout<<bc<<bc1<<endl;
                print();
        }
};
int main(){
        Rectangular r1(4,5);
        r1.f();
        r1.print();
        return 0;
}
最佳答案
2019-12-23 19:41:43
第一个代码
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;

  4. class Point {
  5. protected:
  6.     double x;
  7.     double y;
  8. public:
  9.     Point(double a = 0, double b = 0) {
  10.         x = a;
  11.         y = b;
  12.     }
  13.     double getx() {
  14.         return x;
  15.     }
  16.     double gety() {
  17.         return y;
  18.     }
  19.     void print() {
  20.         cout << "(" << x << "," << y << ")" << endl;
  21.     }
  22. };

  23. class Distance : public Point {
  24. protected:
  25.     Point p1;
  26. public:
  27.     Distance(double c, double d) : Point(c, d), p1(4, 5) {}
  28.     void print() {
  29.         cout << sqrt(pow(p1.getx() - x, 2) + pow(p1.gety() - y,2)) << endl;
  30.         cout << "(" << p1.getx() << "," << p1.gety() << ")(" << x << "," << y << ")" << endl;
  31.     }
  32. };

  33. int main() {
  34.     Distance d1(0, 0);
  35.     d1.print();

  36.     return 0;
  37. }
复制代码


第二个代码:
  1. #include<iostream>
  2. using namespace std;

  3. class Square {
  4. protected:
  5.     double bc;
  6.     double zc;
  7. public:
  8.     Square(double bc = 0) : bc(bc) {};
  9.     void virtual f() = 0;
  10.     void print() {
  11.         cout << zc << endl;
  12.     }
  13. };

  14. class Rectangular : public Square {
  15.     double bc1;
  16. public:
  17.     Rectangular(double a, double b) : Square(a) {
  18.         bc1 = b;
  19.     }
  20.     void f() {
  21.         zc = 2 * (bc + bc1);
  22.     }
  23.     void print() {
  24.         cout << bc << " " << bc1 << endl;
  25.         Square::print();
  26.     }
  27. };

  28. int main() {
  29.     Rectangular r1(4, 5);
  30.     r1.f();
  31.     r1.print();

  32.     return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-23 19:41:43 | 显示全部楼层    本楼为最佳答案   
第一个代码
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;

  4. class Point {
  5. protected:
  6.     double x;
  7.     double y;
  8. public:
  9.     Point(double a = 0, double b = 0) {
  10.         x = a;
  11.         y = b;
  12.     }
  13.     double getx() {
  14.         return x;
  15.     }
  16.     double gety() {
  17.         return y;
  18.     }
  19.     void print() {
  20.         cout << "(" << x << "," << y << ")" << endl;
  21.     }
  22. };

  23. class Distance : public Point {
  24. protected:
  25.     Point p1;
  26. public:
  27.     Distance(double c, double d) : Point(c, d), p1(4, 5) {}
  28.     void print() {
  29.         cout << sqrt(pow(p1.getx() - x, 2) + pow(p1.gety() - y,2)) << endl;
  30.         cout << "(" << p1.getx() << "," << p1.gety() << ")(" << x << "," << y << ")" << endl;
  31.     }
  32. };

  33. int main() {
  34.     Distance d1(0, 0);
  35.     d1.print();

  36.     return 0;
  37. }
复制代码


第二个代码:
  1. #include<iostream>
  2. using namespace std;

  3. class Square {
  4. protected:
  5.     double bc;
  6.     double zc;
  7. public:
  8.     Square(double bc = 0) : bc(bc) {};
  9.     void virtual f() = 0;
  10.     void print() {
  11.         cout << zc << endl;
  12.     }
  13. };

  14. class Rectangular : public Square {
  15.     double bc1;
  16. public:
  17.     Rectangular(double a, double b) : Square(a) {
  18.         bc1 = b;
  19.     }
  20.     void f() {
  21.         zc = 2 * (bc + bc1);
  22.     }
  23.     void print() {
  24.         cout << bc << " " << bc1 << endl;
  25.         Square::print();
  26.     }
  27. };

  28. int main() {
  29.     Rectangular r1(4, 5);
  30.     r1.f();
  31.     r1.print();

  32.     return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 19:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表