C++题:请创建一个Test类.....

2025年03月25日 17:47
有1个网友回答
网友(1):

写个简单的类和main函数,你看看吧
#include
using namespace std;

class Test
{
private:
int m_nWidth;
int m_nLength;
public:
Test();
Test(int w, int l);
~Test();
void SetWidth(int w);
void SetLength(int l);
int GetWidth();
int GetLenght();
void Output();
};

Test::Test()
{
m_nWidth = 0;
m_nLength = 0;
}

Test::~Test()
{

}

Test::Test(int w, int l)
{
m_nWidth = w;
m_nLength = l;
}

void Test::SetWidth(int w)
{
m_nWidth = w;
}

void Test::SetLength(int l)
{
m_nLength = l;
}

int Test::GetWidth()
{
return m_nWidth;
}

int Test::GetLenght()
{
return m_nLength;
}

void Test::Output()
{
cout << "Width = " << m_nWidth << ", Length = " << m_nLength << endl;
}

int main()
{
Test test1;
test1.Output();

Test test2(2, 5);
int w = test2.GetWidth();
int l = test2.GetLenght();
cout << "width = " << w << ", length = " << l << endl;

Test test3(2, 5);
test3.SetWidth(4);
test3.SetLength(6);
test3.Output();

return 0;
}