#include<iostream>
using namespace std;
class R{
int len,w;
public:
R(int len,int w);//成员函数声明;
int getArea(); //构造函数声明;
};
R::R(int len,int w){
this->len=len;
this->w=w; //对象数据成员名字一样,用this指针表示
//参数与成员变量名相同时,如this->n = n;
}
int R::getArea(){
return (this->len)*(this->w); //构造函数定义;
}
int main(){
R r1(2,5),r2(3,6);
cout<<"First Area is "<<r1.getArea()<<endl;
cout<<"Second Area is "<<r2.getArea()<<endl;
return 0;
}
输出数据如下:
First Area is 10
Second Area is 18