<pre name="code" class="cpp">//编写一个简单的类。包含构造函数,成员函数等。 #include <iostream> using namespace std; class Rec { public: Rec(int l,int w); int Area(); void Print(); private: int length,wide; }; Rec::Rec(int l,int w) { length=l; wide=w; } int Rec::Area() { return length*wide; } void Rec::Print() { cout<<"长宽分别为:"<<length<<","<<wide<<endl; } int main() { Rec rec1(8,5),rec2(18,6); cout<<"长方形1"; rec1.Print(); cout<<" 其面积为:"<<rec1.Area(); cout<<endl; cout<<"长方形2"; rec2.Print(); cout<<" 其面积为:"<<rec2.Area(); cout<<endl; return 0; }
原文地址:http://blog.csdn.net/doudouwa1234/article/details/45199031