D
D
DjTolib2021-06-25 11:17:25
C++ / C#
DjTolib, 2021-06-25 11:17:25

What does the program print and why?

#include <iostream>
using namespace std;
struct  A{
    A(){cout<<"A";}
    A(A&){cout<<"B";}
    virtual void f(){cout<<"C";}

};
int main() {
    A a[10];
    std::cout<<'\n';
    for(auto x: a)
    {
        x.f();
    }
    
}

Justifications are needed, a link to the standard, for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2021-06-25
@DjTolib

Program prints

AAAAAAAAAA
BCBCBCBCBCBCBCBCBCBC

Here 10 structures of type A are created and the constructor of type A() is called 10 times. Here the copy constructor of type A(A&) is called for each element from the array a[10]. Here the function f() of type A is called
A a[10];
for(auto x: a)
x.f();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question