Skip to content
Related Articles

Related Articles

C++ | Class and Object | Question 6
  • Last Updated : 30 Jul, 2014

Which of the following is true about the following program




#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;
}


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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :