脚本执行失败问题
1. 虚函数是可以内联的,这样就可以减少函数调用的开销,提高效率(错误
2. 一个类里可以同时存在参数和函数名都相同的虚函数与静态函数(错误
3. 父类的析构函数是非虚的,但是子类的析构函数是虚的,delete子类指针(指向该子类对象),会调用父类的析构函数(正确//任何情况下删除子类都会调用到父类的析构函数
4.对于下面的类CAsizeof(CA) = _B_
A. 4  B. 8  C. 12  D. 16
class CA
{
public:
    CA();
    virtual ~CA(); //因为有虚函数,所以会有4个字节的虚表指针
private:
    int m_iTime;    //成员变量4个字节
public:
    int GetTime();
    int SetTime(int iTime);
};
5.下面这段程序,打印结果是_A_
A. 1  B. 2  C. 3  D. 以上都不对
int g_iCount = 0;
class CParent
{
public:
    CParent()  {}
    ~CParent() {g_iCount += 1;}
};
class CSon : public CParent
{
public:
    CSon()  {}
    ~CSon() {g_iCount += 2;}
};
main()
{
CParent* p = new CSon();
    delete p;
    std::cout << g_iCount << std::endl;
}
6.请问下面这段程序的输出结果是_A_
A. 2,1,  B. 2,2,  C. 1,1,  D. 1,2,
class CParent
{
public:
    CParent() {}
    virtual ~CParent() {}
public:
    virtual void Print()
    { std::cout << "1,"; };
};
class CSon : public CParent
{
public:
构造函数可以被重载
    CSon()  {};
    virtual ~CSon() {};
public:
    void Print()
    { std::cout << "2,"; };
};
void Test1(CParent& oParent)
{oParent.Print();}
void Test2(CParent oParent)
{oParent.Print();}
main()
{
    CSon * p = new CSon();
    Test1(*p); //这里只是一个引用
    Test2(*p); //这里会在栈空间重新构造Cparent类对象
    delete p;
}
7.请问下面这段程序的输出结果是_D_
A. 2,1,  B. 2,2,  C. 1,1,  D. 1,2,
class CParent
{
public:
    CParent() {}
    virtual ~CParent() {}
public:
    void Print(){ std::cout << "1," ; };
};
class CSon : public CParent
{
public:
    CSon()  {}
    virtual ~CSon() {}
public:
    void Print(){ std::cout << "2,"; };
};
main()
{
    CSon oSon;
    CParent * pParent = &oSon;
    CSon    * pSon    = &oSon;
    pParent->Print();
    pSon->Print();
}
8.请问下面这段程序的输出结果是_C_
A. 2,1,  B. 2,2,  C. 1,2,  D. 1,1,
class CParent
{
public:
    CParent() {Print();}
    virtual ~CParent() {}
public:
    virtual void Print(){ std::cout << "1,"; }
};
class CSon : public CParent
{
public:
    CSon()  {Print();}
    virtual ~CSon() {}
public:
    void Print(){ std::cout << "2,"; }
};
main()
{
    CParent * pParent = new CSon();
    delete pParent;
}
9.请问下面这段程序的输出结果是_D_
A. 2,2,  B. 2,  C. 输出结果不确定  D. 以上都不对
class CParent
{
public:
    CParent() {Print();}
    virtual ~CParent() {}
public:
    virtual void Print() = 0;
};
class CSon : public CParent
{
public:
    CSon()  {Print();}
    virtual ~CSon() {}
public:
    void Print()
    { std::cout << "2,"; };
};
main()
{
    CParent * pParent = new CSon();
    delete pParent;
}
10.请仔细阅读以下程序:
class Base
{
public:
    virtual bool operator == (int iValue)
    {
        std::cout << "I am Base class !" << std::endl;
        return true;
    }
    virtual ~Base(){}
};
class Derive: public Base
{
public:
    virtual bool operator == (int iValue)
    {
        std::cout << "I am Derive class !" << std::endl;
        return true;
    }
virtual ~Derive(){}
};
int main()
{
    Derive derive;
    Base* pBase = &derive;
    Derive* pDerive = &derive;
    *pBase == 0;
    *pDerive == 0;
    return 0;
}
程序的输出结果是_B_
AI am Base class !
I am base class !
BI am Derive class !
I am Derive class !
CI am base class !
I am Derive class !
DI am Derive class !
I am Base class !     
11.请仔细阅读以下程序:
class Base
{
public:
    virtual void display(std::string strShow  =  "I am Base class !")
    { std::cout << strShow << std::endl; }
    virtual ~Base(){}
};
class Derive: public Base
{
public:
    virtual void display(std::string strShow  = "I am Derive class !")
    { std::cout << strShow << std::endl; }
    virtual ~Derive(){}
};
int main()
{
    Base* pBase = new Derive();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。