What is the output of the following code snippet assuming user enters the side as 4?

class square
{
public:
double side1;
double area()
{
return(side1*side1);
}
};

int main(){
double area1=0;
square c1,c2;
cout << "Enter the length of the square" << endl;
cin >> c1.side;
cout << "The area of the square is : " << c1.area1() << endl;
return(0);
}

Options
- 16
- Will result in an error
- 8
- 12


CORRECT ANSWER : Will result in an error

Discussion Board
C++ - Trace the Output

The above program will give compile error.
error 1: ‘class square’ has no member named ‘side’. Because we declared the variable name side1.
error 2 : ‘class square’ has no member named ‘area1’. area1 is a variable, not a function.
Check following program, you will get the proper output:

#include
using namespace std;

class square
{
public:
double side1;
double area()
{
return(side1*side1);
}
};

int main()
{
//double area1=0;
square c1;//,c2;
cout << "Enter the length of the square" << endl;
cin >> c1.side1;
cout << "The area of the square is : " << c1.area() << endl;
return(0);
}

Output:
Enter the length of the square
4
The area of the square is : 16

Prajakta Pandit 01-24-2017 11:32 PM

answer is correct. it will give compile error.

Samrat, I think below output will justify your doubt. You got result because you would have used side1 and area but actually it is written here as side and area1()
================================================================
$ g++ -c prog1.cpp
prog1.cpp: In function ‘int main()’:
prog1.cpp:17:11: error: ‘class square’ has no member named ‘side’
cin >> c1.side;
^
prog1.cpp:18:46: error: ‘class square’ has no member named ‘area1’
cout << "The area of the square is : " << c1.area1() << endl;
==================================================================

Akhilesh 09-3-2015 07:34 PM

additional info

indeed there no "area1()", but there no public "side" member either.

doz 07-12-2015 06:29 PM

giving correct error to audience

the answer should be 16 and it is 16 i have compile it on my visual studio
explanation:
the value enter by user is 4 so we have too return the return the value to function so we multiply side 1 with side 2 and get answer .if anybody has any confusion try it on your own compiler and understand it using break points :
One more thing i have a collection of c++ program if any body is intrsted contect me at my gmail accoount my gmail is :
usmanbashir.bahriya@gmail.com

usman bashir 06-8-2015 01:57 PM

No correct answer

The program won't compile and therefore it can't be run and user cannot enter any value

sdp 05-4-2015 06:49 AM

Answer

There is error, cause in the square class you don't have area1 function and side member variable.

P 01-18-2015 04:12 AM

The answer is currect. It will give an error

no area1()function is defined in class square. its area(). and we are calling area1() function. Hence it will give an error

Akhil 01-4-2015 09:49 PM

Will result in an error

No member function called area1() for class square

rh 01-2-2015 12:41 PM

THe Correct Answer is 16 if suitable headers are included

the correct answer is 16 if side =4
I have checked this program by running it on to my machine's visual studio c++
but i have included suitable header files and namespace std

Ashish 12-28-2014 01:25 PM

error because output is c1.area1() with a "1" instead of c1.area()

i think it errors because output is c1.area1() with a "1" instead of c1.area().
but confirmation would be welcome

francois duglou 07-25-2014 12:51 PM

The answer to this question is incorrect.

The ourput will be 16 if input is 4. Only reason this can give error is because of missing iostream header and missing namespace reference.Please confirm.

Samrat Kumar 06-25-2014 04:04 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement