ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

利用C++面向对象范式编程求矩形面积 ← 类

2026/9/21 16:37:11 拓冰建站 浏览量
利用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