利用C++面向对象范式编程求矩形面积 ← 类
【面向对象代码】
#include <bits/stdc++.h>
using namespace std;class Rectangle {private:double x,y;public:Rectangle() {} //无参构造函数Rectangle(double x,double y) { //有参构造函数this->x=x;this->y=y;}double getArea() {return x*y;}
};int main() {int x,y;cin>>x>>y;Rectangle rec(x,y);cout<<rec.getArea()<<endl;
}/*
in:2 3
out:6
*/
【面向过程代码】
#include <bits/stdc++.h>
using namespace std;int main() {int x,y;cin>>x>>y;cout<<x*y<<endl;
}/*
in:2 3
out:6
*/
【参考文献】
https://www.runoob.com/cplusplus/cpp-this-pointer.html