New to OOP, need help with classes

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
#include<iostream>
using namespace std;

class Area
{
      Area(int , int );
      int length;
      int width;
      
      int* IN_DATA();
      int CAMPUS_AREA(int []);
      void DISPLAY(int);
      
} ;

Area::Area (int a, int b)
     {
       length=a;
       width=b;
     }

int main()
{
    
    
    
    
    
    system("pause");
}


When I declare an object of type area, it gives an error, that no matching function to call to Area::Area(). and I want to call class functions in main, but it gives an error that, you cannot call function without object..and, when I declare object it gives the above error. Help!
Last edited on
If you ever want to be able to use DISPLAY() or CAMPUS_AREA in main, you'll need to define them under the access specifier public: . By Default all class members are private unless you specify otherwise.

Set your access specifier in Area:
1
2
3
4
5
6
7
8
9
10
11
12
class Area
{
      int length;
      int width;

public:    // Access Specifier
      Area(int , int );
      int* IN_DATA();
      int CAMPUS_AREA(int []);
      void DISPLAY(int);
      
} ;


Everything after the public: access specifier allows your class methods to be accessed outside of the class itself. Where as your two variables, length and width are private, and can only be altered from the public methods within the Area class.

This isn't a fix all solution, this helps you get on your way. I suggest you read the tutorial here on classes: http://www.cplusplus.com/doc/tutorial/classes/ - I'll be reading it more in depth as well.
Last edited on
^ What about declaring an object, then? The compiler thinks, I am trying to call a constructor..
Last edited on
because you don't have a default constructor (constructor with no argument).

add Area() : length(0), width(0) {} before line Area( int , int );

or change Area( int , int ); to Area( int = 0 , int = 0 );
Last edited on
Thanks, Problem solved!
Topic archived. No new replies allowed.