Question

please, Answer all questions and give a detailed explanation.

What is output by the following? class A public: AO { ++x; } static int x; int A::X - 1; int maino vector<A> vec(8): cout <<

What is output by the following? struct X{ x(int „a, int b) : a(a), b(b) () int a, b: int main x myx(1, 5): cout << myx.a <<

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

OUTPUT:

2

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
class A{
public:
    A() { ++x; } // Constructor which increments x everytime a new object of A is created
    static int x; // static variable is common accross all objects of A
};
int A::x = 1; // Initialize x with 1
int main(){
    vector<A> vec(8);   // Reserves memory for 8 elements of type A and calls constructor of A only once
                        // to initialize all 8 objects and then after initialization destructor is called.
    cout << A::x; // Since, constructor has been called only once, therefore, A::x = 2 at this point of time
    return 0;
}

Answer 2:

OUTPUT:

1

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
class A{
public:
    A() { ++x; } // Constructor which increments x everytime a new object of A is created
    ~A() { --x; } // Destructor which decrements x everytime an object goes out of scope
    static int x; // static variable is common accross all objects of A
};
int A::x = 1; // Initialize x with 1
int main(){
    vector<A> vec(5);   // Reserves memory for 5 elements of type A and calls constructor of A only once
                        // to initialize all 5 objects and then after initialization destructor is called.
    // Since, constructor and destructor have been called once, therefore, x remains 1 at this point of time
    
    // A::x < 10 is true because A::x = 1
    if (A::x < 10){
        A b, c, d, e, f, g; // Creates 6 objects of A and A::x becomes 1 + 6 = 7. These objects are local to
                            // this if block only and will be destroyed once goes out of the if block.
    }
    // Here, above 6 objects created goes out of scope therefore destructor will be called for each object
    // and A::x will be decremented 6 times in total. Hence, A::x will become 7 - 6 = 2 at this point of time.
    cout << vec.at(2).x; // vec.at(2).x is same as A::x. Therefore, 1 will be printed here.
    return 0;
}

Answer 3:

OUTPUT:

15

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
struct X{
    // Constructor to initialize this->a = _a and this->b = b
    X(int _a, int b) : a(_a), b(b) {}
    int a, b;
};
int main(){
    X myX(1, 5); // Create object of struct with myX.a = 1 and myX.b = 5
    cout << myX.a << myX.b; // Here, myX.a = 1 and myX.b = 5. So, 15 will be printed
    return 0;
}

Answer 4:

OUTPUT:

welp

CODE (WITH COMMENTS):

#include <iostream>
#include <string>
using namespace std;
// This function takes a reference to the string object and an integer value
// and returns a reference to the character stored at i-th index in the string s.
// Reference to ith character is returned so that character can be changed by callee function
char& GetChar(string& s, int i){
    return s.at(i);
}
int main(){
    string message = "Help"; // Here, message[0] = 'H', message[1] = 'e', message[2] = 'l', and message[3] = 'p'
    GetChar(message, 0) = 'w'; // GetChar returns refernce to message[0] and then message[0] = 'w' is done
    // Therefore, message[0] = 'w', message[1] = 'e', message[2] = 'l', and message[3] = 'p'
    // message becomes "welp"
    cout << message; // Prints "welp"
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
please, Answer all questions and give a detailed explanation. What is output by the following? class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT