|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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;
}
第一个代码
- #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;
- }
复制代码
|
|