当前位置: 代码迷 >> C语言 >> 万分火急的求教,会的进C++简单运行结果题
  详细解决方案

万分火急的求教,会的进C++简单运行结果题

热度:192   发布时间:2008-03-26 12:49:19.0
万分火急的求教,会的进C++简单运行结果题
1、    写出下列程序的结果
#include <iostream.h>
Class Myclass
{
    Public:
            Myclass(int a){x=a;}
            Void fun1(){x++;}
            Void fun2(){x+=2;}
            Void fun3(){x+=3;}
            Void print(){cout<<”x=”<<x<<endl;}
    Private:
            Int x;
};
Void main()
{
    Myclass my(8);
    Void(Myclass::*pf)();
    my.print();
    pf=Myclass::fun2;
    (my.*pf)();
    my.print();
    pf=Myclass::fun3;
    (my.*pf)();
    my.print();
}
运行结果为:


2、写出下列程序的运行结果。
#include<iostream.h>
int Div(int x,int y)
{
if(y= =0)
throw y;
retrun x/y;
}
viod main()
{
try
{
cout<<"7/3="<<Div(7,3)<<endl;
cout<<"9/0="<<Div(9,0)<<endl;
cout<<"8/4=""<<Div(8,4)<<endl;
}
catch(int)
{
cout<<"Exception of dividing zero. "<<endl;
}
cout<<"It is OK."<<endl;
}

运行结果为:
搜索更多相关的解决方案: Void  火急  Myclass  int  

----------------解决方案--------------------------------------------------------
哪位会的前辈帮帮忙哈,谢谢了
----------------解决方案--------------------------------------------------------
第一题是:8,10,13 这是一个指向类成员函数指针..看看书就好了
#include <iostream.h>
class Myclass
{
    public:
            Myclass(int a){x=a;};
            void fun1(){x++;};
            void fun2(){x+=2;};
            void fun3(){x+=3;};
            void print(){cout<<"x="<<x<<endl;};
    private:
            int x;
};
void main()
{
    Myclass my(8);
    void(Myclass::*pf)();
    my.print();
    pf=Myclass::fun2;
    (my.*pf)();
    my.print();
    pf=Myclass::fun3;
    (my.*pf)();
    my.print();
}
第二:
7/3=2
Exception of dividing zero.
It is OK.
这是个处理异常的例子...当遇到异常的时候抛出....
#include<iostream.h>
int Div(int x,int y)
{
if(y==0)
throw y;
return x/y;
}
void main()
{
try
{
cout<<"7/3="<<Div(7,3)<<endl;
cout<<"9/0="<<Div(9,0)<<endl;
cout<<"8/4="<<Div(8,4)<<endl;
}
catch(int)
{
cout<<"Exception of dividing zero. "<<endl;
}
cout<<"It is OK."<<endl;
}
----------------解决方案--------------------------------------------------------
x=8
x=10
x=13

7/3=2
Exception of dividing zero.
It is OK.
----------------解决方案--------------------------------------------------------
前辈们,这题到底是怎么做的呀?我糊涂了~~
----------------解决方案--------------------------------------------------------
  相关解决方案