程序填空
#include <iostream>
using namespace std;
class Base {
public:
Base(int a): idata(a) { }
void print() { cout<<idata<<", "; }
private:
int idata;
};
class Derived:public Base {
public:
Derived(int a, double b):Base(a) { ddata=b; }
void print() {
Base::print();
cout<<ddata<<endl;
}
private:
double ddata;
};
int main() {
Derived d1(1,2.3), d2(2,4.6);
d1.Base::print();
d1.print();
d2.print();
return 0;
}
2
#include<iostream>
using namespace std;
class B0 {
protected:
int a;
public:
B0(int x) { a=x; }
void f() { cout<<"B0:"<<a<<endl; }
};
class B1 : virtual public B0 {
int b;
public:
B1(int x, int y):B0(x),b(y) { }
void f() { cout<<"B1:"<<b<<endl; }
};
class B2: virtual public B0 {
int b;
public:
B2(int x, int y):B0(x),b(y) { }
void f() { cout<<"B2:"<<b<<endl; }
};
class D:public B2,public B1 {
public:
D(int x, int y,int z):B0(x),B1(x,y),B2(x,z) { }
void f() { cout<<"D:"<<a<<endl; }
};
int main() {
D d(1,2,3);
d.B0::f();
d.B1::f();
d.B2::f();
d.f();
return 0;
}
改错
1
#include <iostream>
using namespace std;
class Base1 {
public:
Base1(int x) {
b1=x;
cout<<"Base1"<<endl;
}
protected:
int b1;
};
class Base2 {
public:
Base2() {
b2++;
cout<<"Base2"<<endl;
}
protected:
static int b2;
};
int Base2::b2=0;
class Derived:public Base1,public Base2
{ public:
Derived(int a,int b):Base1(a)
{ d=b; }
void Display() const
{ cout<<b1<<", "<<b2<<", "<<d<<endl; }
private:
int d;
};
int main() {
const Derived d(3,4);
d.Display();
return 0;
}
2
#include <iostream>
using namespace std;
class Base{
public:
Base(){cout<<"classBase"<<endl;}
void f(){cout<<"finBase"<<endl;}
};
class D1: virtual public Base
{ public:
D1(){cout<<"classD1"<<endl;}
void f(){cout<<"finD1"<<endl;}
};
class D2:virtual protected Base
{ public:
D2(){cout<<"classD2"<<endl;}
void f(){cout<<"finD2"<<endl;}
};
class D3:public D1,public D2
{ public:
D3(){cout<<"classD3"<<endl;}
};
int main() {
D3 d;
d.Base::f();
d.D1::f();
d.D2::f();
return 0;
}
程序设计
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
const double PI=3.14159;
class Point{
public:
Point(float xx, float yy):x(xx),y(yy){}
float getX() const{return this->x;}
float getY() const{return this->y;}
void setX(float newX) {this->x=newX;}
void setY(float newY){this->y=newY;}
private:
float x, y;
};
class Circle:public Point {
public:
Circle(float x=0.0, float y=0.0, float r=1.0):Point(x,y){
this->radius=r;
}
void moveTo(float newX, float newY){
setX(newX);setY(newY);
}
float getRadius() const {
return this->radius;
}
double getCircumference() const {
return 2*PI*radius;
}
double getArea() const {
return PI*radius*radius;
}
double dist(const Circle &c) const{
double x=this->getX()-c.getX();
double y=this->getY()-c.getY();
return sqrt(x*x+y*y);
}
bool isEqual(const Circle &c) const{
if(this->getRadius()==c.getRadius())return true;
return false;
}
private:
float radius ;
};
int main() {
float x,y,r;
cin>>x>>y>>r;
Circle c1(x,y,r),c2;
cout<<"初始: "<<endl;
cout<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;
cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
cin>>x>>y;
c2.moveTo(x,y);
cout<<"平移后: "<<endl;
cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;
cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
ifstream in1("7.1.2_1-s2_in.dat");
ofstream out1("7.1.2_1-s2_out.dat");
while(in1>>x>>y>>r) {
Circle c1(x,y,r),c2;
out1<<"初始: "<<endl;
out1<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;
out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
in1>>x>>y;
c2.moveTo(x,y);
out1<<"平移后: "<<endl;
out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;
out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}
2
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class Point{
public:
Point(float xx, float yy):x(xx),y(yy){}
float getX() const {return this->x;}
float getY() const {return this->y;}
void moveTo(float newX, float newY){
this->x=newX;this->y=newY;
}
private:
float x, y;
};
class Square:protected Point{
public:
Square(float x=0.0,float y=0.0,float len=1.0):Point(x,y){
this->length=len;
}
float getX() const{
return Point::getX();
}
float getY() const{
return Point::getY();
}
float getLen() const{
return this->length;
}
double getArea() const{
return this->length*this->length;
}
void moveTo(float newX, float newY) {
Point::moveTo(newX,newY);
}
double dist(const Square &s) const{
double x=this->getX()-s.getX();
double y=this->getY()-s.getY();
return sqrt(x*x+y*y);
}
private:
float length ;
};
int main() {
float x,y,len;
cin>>x>>y>>len;
Square s1(x,y,len),s2;
cout<<"初始: "<<endl;
cout<<"s1左下角: ("<<s1.getX()<<", "<<s1.getY()<<")"<<endl;
cout<<"边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
cout<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;
cout<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
cout<<"距离:"<<s2.dist(s1)<<endl;
cin>>x>>y;
s2.moveTo(x,y);
cout<<"平移后: "<<endl;
cout<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;
cout<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
cout<<"距离:"<<s2.dist(s1)<<endl;
ifstream in1("7.1.3_5-3_in.dat");
ofstream out1("7.1.3_5-3_out.dat");
while(in1>>x>>y>>len) {
Square s1(x,y,len),s2;
out1<<"初始: "<<endl;
out1<<"s1左下角: ("<<s1.getX()<<", "<<s1.getY()<<")"<<endl;
out1<<"边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
out1<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;
out1<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
out1<<"距离:"<<s2.dist(s1)<<endl;
in1>>x>>y;
s2.moveTo(x,y);
out1<<"平移后: "<<endl;
out1<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;
out1<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
out1<<"距离:"<<s2.dist(s1)<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}