C++一个完整的种实例及其调用

C++一个完整的类实例及其调用

 

//一个完整的类可以是 //Rect.h文件内容如下:

class Rect{ private: int height; int width;

public: Rect(); Rect(int,int); void SetWidth(int); void SetHeight(int); int GetHeight(); int GetWidth(); void Print(); };

 

//Rect.cpp文件,主要成员函数的实现 #include "Rect.h" #include<iostream> using namespace std;

Rect::Rect() { } Rect::Rect(int a,int b):width(a),height(b) { }

void Rect::SetHeight(int x) {    height=x; } void Rect::SetWidth(int x) {    width=x; } int Rect::GetWidth() { return width; } int Rect::GetHeight() { return height; } void Rect::Print() {   cout<<"the Rectangle Height is "<<height<<endl;   cout<<"the Rectangle Width is "<<width<<endl;   cout<<endl;  }    //main.cpp函数对类的使用,类只是定义了一个架构。但是具体的实现,要通过先定义一个类的对象  #include "Rect.h" int main()  {    Rect a(2,3);    Rect b;    b.SetHeight(4);    b.SetWidth(5);    a.Print();    b.Print();       return 0;    }

 

 

1楼u0100168332小时前
看不懂!