C++ | Class and Object | Question 6

Which of the following is true about the following program

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
class Test
{
public:
    int i;
    void get();
};
void Test::get()
{
    std::cout << "Enter the value of i: ";
    std::cin >> i;
}
Test t;  // Global object
int main()
{
    Test t;  // local object
    t.get();
    std::cout << "value of i in local t: "<<t.i<<'\n';
    ::t.get(); 
    std::cout << "value of i in global t: "<<::t.i<<'\n';
    return 0;
}

chevron_right


Contributed by Pravasi Meet
(A) Compiler Error: Cannot have two objects with same class name
(B) Compiler Error in Line “::t.get();”

(C) Compiles and runs fine


Answer: (C)

Explanation: The above program compiles & runs fine. Like variables it is possible to create 2 objects having same name & in different scope.

Quiz of this Question

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.