print function

why print function do not display output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
 
using namespace std;


class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
     int print()
      {
          return width*height;
      }
   protected:
      int width;
      int height;
};

int main()
{
    Shape *p;
Shape q;
p= & q;
p->setWidth(5);
p->setHeight(4);
p->print();


    system("pause");
    return 0;
    }


you should use cout
1
2
3
4
int print()
      {
          return width*height;
      }


could be something like
1
2
3
4
void print()
      {
          cout<<width*height ;
      }
When you have a function that has a return it basically puts that value into the variable
ex: int a = print();
This would put the return value, width*height, into the variable a. In order to actually print it, it would be better to use a void function with it outputting the value when called, like bupeking2000 says above.
Topic archived. No new replies allowed.