程序填空
1
#include <iostream>
using namespace std;
class myClass {
static int a;
public:
myClass() { a++; }
~myClass() { a--; }
static int getA(){
return a;
}
};
int myClass::a =10;
int main() {
cout<< myClass::getA <<endl;
myClass a;
cout<<a.getA()<<endl;
myClass b;
cout<<b.getA()<<endl;
return 0;
}
2
#include <iostream>
using namespace std;
class Test {
const int x =0 ;
static int y ;
public:
Test() { y+=1; }
Test(int i,int j):x(i) { y+=j; }
void display() const;
void display() { cout<<"x="<<x<<", y="<<y<<endl; }
};
int Test::y=0;
void Test::display() const {
cout<<"x="<<x<<"; y="<<y<<endl;
}
int main() {
Test t1; t1.display();
Test t2(1,2); t2.display();
const Test t3(3,4);
t3.display();
return 0;
}
改错
1
#include <iostream>
using namespace std;
class myClass {
public:
myClass(int i)
:a(i){
}
void print()
{ cout<<a<<','<<b<<endl; }
void static showB()
{ cout<<b<<','; }
private:
const int a;
static int b;
};
int myClass::b=0;
int main() {
myClass::showB();
myClass a1(2); a1.print(); a1.showB();
myClass a2(4); a2.print();
return 0;
}
程序设计
1
#include<iostream>
#include<fstream>
using namespace std;
class Rectangle {
public :
Rectangle(float xx1=0.0,float yy1=0.0,float xx2=1.0,float yy2=4.0):x1(xx1),y1(yy1),x2(xx2),y2(yy2){
circumference=2*(x2-x1+y2-y1);
}
void moveTo(float newX, float newY) {
float x_offset=newX-x1,y_offset=newY-y1;
x1=newX;y1=newY;
x2+=x_offset,y2+=y_offset;
}
double getCircumference (){
return circumference;
}
bool isSquare() {
if(x2-x1==y2-y1)return true;
return false;
}
bool isEqual(Rectangle &r) {
if(r.getCircumference()==this->circumference) return true;
else return false;
}
private:
float x1, y1, x2, y2 ;
double circumference ;
};
int main() {
float x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Rectangle r1(x1,y1,x2,y2),r2(r1);
cout<<"矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;
cout<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
cout<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
cin>>x1>>y1;
r2.moveTo(x1,y1);
cout<<"平移后:\n矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;
cout<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
cout<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
ifstream in1("4.1.1.3_4-2_in.dat");
ofstream out1("4.1.1.3_4-2_out.dat");
while(in1>>x1>>y1>>x2>>y2)
{
Rectangle r1(x1,y1,x2,y2),r2(r1);
out1<<"矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;
out1<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
out1<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
in1>>x1>>y1;
r2.moveTo(x1,y1);
out1<<"平移后:\n矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;
out1<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
out1<<"周长是否相等: "<<r2.isEqual(r1)<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}
2
点(Point)类成员如下:
(1)公有成员:
Point(float xx, float yy)
void moveTo(float newX, float newY)
(2)私有成员:
float x, y
在此基础上,定义正方形(Square)类,其成员如下:
(1)公有成员:
Square(float x=0.0,float y=0.0,float len=1.0)
void resetSquare(float newX, float newY, float newLen)
double getLen()
double getCircumference()
bool isEqual(Square &s)
(2)私有成员:
Point p
float length
double circumference
请根据上述说明,完成Point,Square两个类的定义。
注意:部分源程序给出,仅允许在注释“Begin”和“End”之间填写内容,不得改动main函数和其他已有的任何内容。
试题程序:
*/
#include<iostream>
#include<fstream>
using namespace std;
class Point {
public:
Point(float xx, float yy) :x(xx),y(yy){}
void moveTo(float newX, float newY) {
x=newX;y=newY;
}
private:
float x, y ;
};
class Square{
public:
Square(float x=0.0,float y=0.0,float len=1.0):p(x,y){
length=len;
circumference=4*length;
}
void resetSquare(float newX, float newY, float newLen){
p.moveTo(newX,newY);
length=newLen;
circumference=4*length;
}
double getLen(){
return length;
}
double getCircumference(){
return circumference;
}
bool isEqual(Square &s){
if(s.length==this->length) return true;
else return false;
}
private:
Point p ;
float length ;
double circumference;
};
int main() {
float x,y,len;
cin>>x>>y>>len;
Square s1(x,y,len),s2;
cout<<"s1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
cout<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
cin>>x>>y>>len;
s2.resetSquare(x,y,len);
cout<<"重置后:\ns1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
cout<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
ifstream in1("4.2.3_2-2_in.dat");
ofstream out1("4.2.3_2-2_out.dat");
while(in1>>x>>y>>len)
{
Square s1(x,y,len),s2;
out1<<"s1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
out1<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
out1<<"是否相等: "<<s2.isEqual(s1)<<endl;
in1>>x>>y>>len;
s2.resetSquare(x,y,len);
out1<<"重置后:\ns1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
out1<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
out1<<"是否相等: "<<s2.isEqual(s1)<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}