第一个代码#include <iostream>
#include <cmath>
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;
public:
Distance(double c, double d) : Point(c, d), p1(4, 5) {}
void print() {
cout << sqrt(pow(p1.getx() - x, 2) + pow(p1.gety() - y,2)) << endl;
cout << "(" << p1.getx() << "," << p1.gety() << ")(" << x << "," << y << ")" << 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) : bc(bc) {};
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;
Square::print();
}
};
int main() {
Rectangular r1(4, 5);
r1.f();
r1.print();
return 0;
}
|