Open In App

Output of C++ Program | Set 19

Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of following C++ programs.

Question 1




#include <iostream>
#include <string.h>
using namespace std;
  
int main()
{
    cout << sizeof("GeeksforGeeks") << endl;
    cout << strlen("GeeksforGeeks");
    return 0;
}


Output:

14
13

Sizeof operator returns the size of string including null character so output is 14. While strlen() function returns the exact length of string excluding null character so output is 13.



Question 2:




#include <iostream>
using std::cout;
class Test
{
public:
    Test();
    ~Test();
};
Test::Test()
{
    cout << "Constructor is executed\n";
}
Test::~Test()
{
    cout << "Destructor is executed\n";
}
int main()
{
    delete new Test();
    return 0;
}


Output:

Constructor is executed
Destructor is executed

The first statement inside the main () function looks strange, but it is perfectly valid. It is possible to create an object without giving its handle to any pointer in C++. This statement will create an object of class Test without any pointer pointing to it. This can be also done in languages like Java & C#.
For example consider following statement:

new  student();  // valid both in Java & C#

The above statement will create an object of student class without any reference pointing to it.



Question 3:




#include <iostream>
using std::cout;
class main 
{
public:
    main()  {cout << "ctor is called\n";}
    ~main() {cout << "dtor is called\n";}
};
int main() 
{
    main m;    // LINE 11
}


Output:

Compiler error:
11 8 [Error] expected ';' before 'm' 

The above program looks syntactically correct but it fails in compilation. The reason class name. Class name is main so it is necessary to tell the compiler that main is the name of class. Generally struct or class keyword is not required to write to create an object of the class or struct. But when the name of class is main it becomes necessary to write struct or class when creating object of class or struct. Remember main is not a reserved word.

Following is a correct version of the above program:




#include <iostream>
using std::cout;
class main
{
public:
   main()  { cout << "ctor is called\n";}
   ~main() { cout << "dtor is called\n";}
};
int main()
{
   class main m;
}


Now predict the output of following program:




#include <iostream>
using std::cout;
class main 
{
public:
   main()  { cout << "ctor is called\n"; }
   ~main() { cout << "dtor is called\n"; }
};
main m;    // Global object
int main()
{             
}


The above program compiles and runs fine because object is global. Global object‘s constructor executes before main() function and it’s destructor executes when main() terminates.

Conclusion: When the class/struct name is main and whenever the local object is created it is mandatory to write class or struct when the object of class / and struct is created. Because C++ program execution begins from main () function. But this rule is not applied to global objects. Again, main isn’t a keyword but treat it as if it were.

This article is contributed Meet Pravasi.



Last Updated : 27 Dec, 2016
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads