C++问答题 我正在练习一个重要的测验,我发现了20个关于C++的问题,我不太确定。他们没有回答,我想知道你是否能帮我回答他们,因为我还是一个正在学习的新手。我用箭头标出了我认为对每一个问题都正确的答案,并给出了一个简短的理由 问题1 问题4

C++问答题 我正在练习一个重要的测验,我发现了20个关于C++的问题,我不太确定。他们没有回答,我想知道你是否能帮我回答他们,因为我还是一个正在学习的新手。我用箭头标出了我认为对每一个问题都正确的答案,并给出了一个简短的理由 问题1 问题4,c++,inheritance,abstract,C++,Inheritance,Abstract,抽象类中存在什么使其抽象 a) virtual keyword prefix o member function b) virtual keyword prefixed to member function and sufixed with =0 <--(since without the =0 it wouldnt be a pure virtual method which must be overriden) c) member function in protected mo

抽象类中存在什么使其抽象

a) virtual keyword prefix o member function
b) virtual keyword prefixed to member function and sufixed with =0 <--(since without    the =0 it wouldnt be a pure virtual method which must be overriden)
c) member function in protected mode
d) any of the above
问题5 下面执行代码片段的结果是什么

//suitable #includes
class Text
{
public:
    Text(const std::string &text):data(new char[text.size()+1000]){
        std::copy(text.begin(),text.end(),data);
        }
    ~Text(){
        delete [] data;
        }
    void print() const{
        std::cout << data << std::endl;
        }
private:
    char *data;
};

int main(int, char *[])
{
    Text outer("hello");
    {
        const Text inner("world");
        outer = inner;
    }
    outer.print();
    return 0;
}

(No idea abou the answer)
a) prints "world", but there is a buffer overflow in the constructor
b) prints "world", no problems anywhere
c) prints "hello"
d) none of the above
Q1 OK

Q2取决于o的含义。事实上,o被销毁了——它是堆栈上的指针,但是o所指向的对象没有被销毁

Q3 b您不能在返回类型上重载

Q4好的

由于缺少拷贝构造函数,Q5似乎是最合适的。然而,在实践中,它有可能输出世界,即使这个内存被删除

让我看看

选项b确实正确,但不是你的解释。它受到保护,因为SecondClass使用公共继承从基继承。如果是私人继承的话,情况就不会这样了

很可能是正确的。o是指向使用new创建的对象的指针,因此该对象将一直存在,直到对其调用delete为止。这个问题可能指的是对象,但如果它们指的是实际的指针o,那么它会在作用域的末尾被销毁

重载函数必须具有不同的签名。签名由函数名、参数的计数、类型和顺序组成。返回类型不是签名的一部分。你应该可以从这里找到答案

抽象类是至少有一个纯虚函数的类,因此您是正确的

你可以自己测试它,然后思考它为什么会发生。例如,在我的编译器上,它打印世界,并由于尝试删除内存两次而崩溃


除了张贴的其他答案,在问题5中,你有未定义的行为

{语言律师,请确认此问题的答案} 在构造函数中,将字符串中的字符逐个复制到字符数组中;没有终止nul字符。print方法将字符数组视为C样式的字符串,cout操作将打印数据中的所有字符,直到找到终止的nul字符。std::basic_字符串数据结构不能将终止nul字符存储为字符串的一部分

类没有复制构造函数,因此编译器创建复制指针的复制构造函数。因此,将有两个实例指向相同的数据

在内部块中,外部实例将指定给在内部块中创建的实例。内部实例是临时的,外部变量中的指针指向临时对象的数据


语句块消失后,外部变量中的指针现在指向一个未使用的位置,预期数据不再存在。当执行离开内部语句块的范围时,预期数据被删除。因此,外部变量的数据指针指向未知数据或操作系统可能已调出数据的未知位置。这是未定义的行为。

这篇文章中的问题太多了。你为什么不选择一个,然后专注于它,包括你认为答案是什么,在你完成一个后,如果第二个问题需要的话,发布一个新的问题?这大部分是非常糟糕的代码。这个问题似乎离题了,因为它涉及大量的问题。在这里用一个问题来问一个问题。更正:o指指向使用新创建的对象的指针。不是对象本身。这种差异是人们在这里感到困惑的部分原因。对,o是指针,所述的问题是模糊的。当指针在函数作用域的末尾被销毁时,这个问题可能意味着o所指向的对象。我要说的是,您的答案目前将两者混淆在一起。它说o是对象。@BartoszKP-newchar[whatever]不会初始化分配的内存。分配了新属性的对象默认已初始化;对于没有构造函数的类型,这意味着没有进行初始化。对不起,您是对的。我删除了我的评论,不是为了引起混乱。
void doit(char *, int);
int doit(char *);
float doit(float, float);

Which of the following declarations cannot follow in the same header (no idea):
a) void doit(int, char*);
b) float doit(char *);
c) int doit(int, int);
d) int doit(int);
a) virtual keyword prefix o member function
b) virtual keyword prefixed to member function and sufixed with =0 <--(since without    the =0 it wouldnt be a pure virtual method which must be overriden)
c) member function in protected mode
d) any of the above
//suitable #includes
class Text
{
public:
    Text(const std::string &text):data(new char[text.size()+1000]){
        std::copy(text.begin(),text.end(),data);
        }
    ~Text(){
        delete [] data;
        }
    void print() const{
        std::cout << data << std::endl;
        }
private:
    char *data;
};

int main(int, char *[])
{
    Text outer("hello");
    {
        const Text inner("world");
        outer = inner;
    }
    outer.print();
    return 0;
}

(No idea abou the answer)
a) prints "world", but there is a buffer overflow in the constructor
b) prints "world", no problems anywhere
c) prints "hello"
d) none of the above