📜  C++程序的输出|套装10

📅  最后修改于: 2021-05-30 17:17:08             🧑  作者: Mango

预测以下C++程序的输出。
问题1

C
#include
#include
using namespace std;
 
class String
{
    char *p;
    int len;
public:
    String(const char *a);
};
 
String::String(const char *a)
{
    int length = strlen(a);
    p = new char[length +1];
    strcpy(p, a);
    cout << "Constructor Called " << endl;
}
 
int main()
{
    String s1("Geeks");
    const char *name = "forGeeks";
    s1 = name;
    return 0;
}


CPP
#include
 
using namespace std;
 
class A
{
    public:
    virtual void fun() {cout << "A" << endl ;}
};
class B: public A
{
    public:
    virtual void fun() {cout << "B" << endl;}
};
class C: public B
{
    public:
    virtual void fun() {cout << "C" << endl;}
};
 
int main()
{
    A *a = new C;
    A *b = new B;
    a->fun();
    b->fun();
    return 0;
}


输出:

Constructor called
Constructor called

输出的第一行由语句“ String s1(“ Geeks”);”打印。第二行由语句“ s1 = name;”打印。第二次调用的原因是,单个参数构造函数还可以用作转换运算符(有关详细信息,请参见此内容)。
问题2

CPP

#include
 
using namespace std;
 
class A
{
    public:
    virtual void fun() {cout << "A" << endl ;}
};
class B: public A
{
    public:
    virtual void fun() {cout << "B" << endl;}
};
class C: public B
{
    public:
    virtual void fun() {cout << "C" << endl;}
};
 
int main()
{
    A *a = new C;
    A *b = new B;
    a->fun();
    b->fun();
    return 0;
}

输出:

C
B
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”