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

2014-11-24 03:33:27 · 作者: · 浏览: 0
[cpp]

//一个完整的类可以是

//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
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 "<
cout<<"the Rectangle Width is "<
cout<
}
//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;
}