Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Why won\'t this program compile? #include <iostream> using namespace std; cla

ID: 3559465 • Letter: 1

Question

1) Why won't this program compile?

#include <iostream>

using namespace std;

class TestClass

{

private:

int val;

void showVal()

{ cout <<val<<endl;}

public:

TestClass(int x)

{val = x; }

};

int main()

{

TestClass test(77);

test.showVal();

return 0;

}

2) Why is the output of this program "Hello!"

#include <iostream>

using namespace std;

class TestClass

{

public:

TestClass(int x)

{ cout << x << endl;}

TestClass()

{ cout << "Hello!" << endl;}

};

int main()

{

TestClass test;

return 0;

}

Explanation / Answer

#include <iostream>

using namespace std;

class TestClass

{

private:

int val;

//////// I have remove show Val from here to public part...
public:

void showVal()//////...............................showVal() should be declare as a public member so that it can be called from any other plcae

{ cout <<val<<endl;}
TestClass(int x)

{val = x; }

};

int main()

{

TestClass test(77);

test.showVal();// main() is calling showVal() so showVal() should be a public member of TestClass

return 0;

}

--------------------------xxxxxxxxxxxxxxxxxxx===============xxxxxxxxxxxxxxxxx-----------------------

#include <iostream>

using namespace std;

class TestClass

{

public:

TestClass(int x)

{ cout << x << endl;}

TestClass()// ............When ever a TestClass object is created this fuction get exicuted

{ cout << "Hello!" << endl;} // this line print "Hello!" on the screen

};

int main()

{

TestClass test; // An object of class TestClass is created so TestClass() function get exicuted.

return 0;

}