exam2_1.cpp

来自「C++程序设计源代码例」· C++ 代码 · 共 47 行

CPP
47 行
字号
#include "iostream.h"
class Square
{
protected:
	int len;
public: 
	Square(){} 
	Square(int x) {len=x;} 
	int SetLen(int x);
	int GetLen(){return len;}
	void DrawSquare();
};

int  Square::SetLen(int x)
{ 
	if(x<1 ||x>30 )    //x的值在1-30之间   
		return 0; 
	len=x; 
	return 1;
}
void Square::DrawSquare()
{  
	int i,j;	
	for(i=0;i<len;i++)	
	{
		for(j=0;j<len;j++)	  
			cout<<'*';	 
		cout<<endl;	
	}
}

void main()
{ 
	Square s(5);  
	cout<<"draw the square,len="<<s.GetLen()<<endl;   
	s.DrawSquare();  
	cout<<"input the new side length:";  
	int x;  
	cin>>x;  
	if (!s.SetLen(x))    
		cout<<"error, x should be  between 1 and 30! ";  
	else
	{
		cout<<"draw the square,len="<<s.GetLen()<<endl; 
		s.DrawSquare();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?