include<iostream.h>
class A
{
private:
int x,y;
public:
void f1(int i=0,int j=0){x=i;y=j;}
void print(){cout<<x<<" "<<y<<endl;}
void f1(int i=0){x=i,y=0;}
};
void main()
{
A a;
a.f1(5);
a.print();
}
第1题:
本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java.awt.*; import java.awt.event.*; import javax swing.*; class MousePanel extends JPanel extends MouseMo- tionListener {public MousePanel {addMouseListener(new MouseAdapter {public void mousePressed(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; current=find(x,y); if(current<0) add(x,y); } public void mouseClicked(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; if(evt.getClickCount>=2) {remove(current); } } }); addMouseMotionListener(this); } public void paintComponent(Graphics g) {super.paintComponent; for(int i=0;i<nsquares;i++) draw(g,i); } public int find(int X,int y) (for(int i=0;i<nsquares;i++) if(squares[i].x-SQUARELENGTH/2<= x X<=squares[i].x+SQuARELENGTH/2 squares[i].Y-SQUARELENGTH/2< =Y y<=squares[i].Y+SQUARELENGTH /2) return i ; return-1 ; } public void draw(Graphics g,int i) {g.drawRect(squares[i].X-SQUARE- LENGTH/2。 squares[i].Y-SQUARELENGTH/2, SQUARELENGTH, SQUARELENGTH); } public void add(int X,int Y) {if(nsquares<MAXNSQUARES) {squares[nsquares]=new Point(x,y); current=nsquares ; nsquares++; repaint; } } public void remove(int n) {if(n<0 ‖ n>=nsquares)return; Nsquares- -; squares[n]=squares[nsquares]; if(current= =n)current= -l; repaint; } public void mouseMoved(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) {} private static final int SQUARELENGTH=10: private static final int MAXNSQUARES=100; private Point[]squares=new Point[MAX- NSQUARES]; private int nsquares=0; private int current=-l; } class MouseFrame. extends JFramc {public MouseFrame {setTitle("java3"); setSize(300,200); addWindowListener(new WindowAdapter {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane; contentPane.add(MousePanel); } } public class java3 {public static void main(String[]args) {JFrame. frame=new MouseFrame; frame.show; } }
第2题:
有如下程序: #include <iostream> using namespace std; class sample { private: int x,y; public: sample(int i,int j) { x=i; y=j; } void disp () { cout<<"disp1"<<end1; } void disp() const { cout<<"disp2"<<end1; };int main () { const sample a(1,2); a.disp(); return 0; } 该程序运行后的输出结果是
A.disp1
B.disp2
C.disp1 disp2
D.程序编译时出错
第3题:
下列程序的输出结果是非曲直【 】。
include<iostream, h>
class base
{
int x, y;
public:
base(int i, int j){x=i; y=j;}
virtual int add(){return x+ y;}
};
class three: public base
{
int z;
public:
three(int i, int j, int k) :base(i, j){z=k; }
int add() { return (base:: add()+z); }
};
void main()
{
three * q=new three(lO,20,30);
cout<<q->add()<<end1;
}
第4题:
下列程序的输出结果是 #include<iostream.h> class Myclass{ public : Myclass( int i=0,int j=0) {x=i; y=j; } void show( ){cout<<"x="<<x<<" "<"y="<<y<<endl;} void show( )const{cout<<"x="<
A.x=4,y=3;x=7,y=8
B.x=3,y=4;x=7,y=8
C.x=7,y=8;x=4,y=3
D.x=8,y=7;x=7,y=8
第5题:
下列程序的输出结果是 #include<iostream.h> class Myclass{ public:Myclass(int i=0,int j=0) { x=i; y=j; } void show( ) {cout < <"x=" < < x < <" " <"y=" < < y < < endl;} void show( ) const{cout < <"x=" < <" " < <"y=" < < y < < endl;} privated: int x; int y; }; void main( ) { Myclass my1(3,4) ; const my2(7,8) ; my1.show( ) ;my2.show( ) ;}
A.x=4,y=3;x=7,y=8
B.x=3,y=4;x=7,y=8
C.x=7,y=8;x=4,y=3
D.x=8,y=7;x=7,y=8
第6题:
在下面的类定义中,this指针的用途是______。
include<iostream.h>
class Sample
{
int x,y;
public:
Sample(int i,int j){x=i;y=j;}
void assign(Sample sa);
};
void Sample::assign(Sample p)
{
if(this!=&p)
{
x=p.x;
y=p.y;
}
}
第7题:
下列程序的运行结果是 #include<iostream.h> class Location{ private: int X.Y; public: void init(int=0,int=0); void valueX(int val){X=val;} int valueX( ){ return X;} void valueY
A.5 0 6 4
B.0 0 6 4
C.5 0 6 2
D.0 0 6 2
第8题:
有如下程序: #include<iostream> using namespace std; Class TestClass { private: int x,y; public: TestClass(int i,int j) { x=i; y=j; } void print() { cout<<“print1”<<endl; } void print()const { cout<<”prin
A.printl
B.print2
C.printl print2
D.程序编译时出错。
第9题:
有以下程序: #include <iostream> using namespace std; class myclass { private: int a, b; public: void init( int i, int j ) { a = i; b = j; } friend int sum( myclass x ); }; int sum( myclass x ) { return x.a + x.b; } int main () { myclass y; y.init( 15, 20 ); cout<<sum( y )<<end1; return 0; } 执行后的输出结果是( )。
A.15
B.20
C.5
D.35
第10题:
有以下程序: #include <iostream> using namespace std; class A { public: A(int i,int j) { a=i; b=j; } void move(int x,int y) { a+=x; b+=y; } void show() { cout<<a<<","<<b<<end1; } private: int a,b; }; class B: private A { public: B(int i,int j): A(i,j) {} void fun() { move(3,5); } void fl () { A::show(); } }; int main() { B d(3,4); d.fun(); d.f1(); return 0; } 程序执行后的输出结果是
A.3,4
B.6,8
C.6,9
D.4,3