0

I'm quite new to C++, and I have a problem with my class definition. This is most likely a pretty basic operation, but I can't find any relevant resources online, as I'm not quite sure what to search for.

The title says it all. I have a header file looking like the following:

class Rectangle{
public:
    int Width();
    int Height();
    int Area();
    void Resize();
private:
    int width;
    int height;
}

And then I have the following .cpp file.

int Rectangle::Width()
{
    //return the width
}

int Rectangle::Height()
{
    //return the height
}

int Rectangle::Area()
{
    //return the area
} 

void Rectangle::Resize()
{
    //resize the rectangle
}

As you can see, I have commented the operations that I wish to do, but I am not quite sure how to access the variables int width and int height from the header file.

8
  • 2
    Did you include the header file in the cpp file? Commented Jun 28, 2016 at 19:57
  • 2
    Just a heads-up: you are missing a ; after your class definition. Commented Jun 28, 2016 at 19:57
  • 2
    You need to include the header file, and make sure that there is a semicolon following the last curly brace of the class definition. Commented Jun 28, 2016 at 19:58
  • I already have the ; after my definition. Just didn't copy paste. I have included the header file, I simply don't know how to access the variables. Commented Jun 28, 2016 at 19:59
  • You may want to use unsigned int for height and width. Regular int can go negative. In my life experience, I haven't come across negative heights or widths, but I'm open to seeing a rectangle with negative width or height. Same with area. Commented Jun 28, 2016 at 20:14

3 Answers 3

4

All you have to do is make sure you include the class header file at the top of your .cpp like so...

#include "THE_FILE_NAME.h"

Then you can access them as you wish. For example, to return the width and height just do:

int Rectangle::Width()
{
    return width; //or this->width
}

int Rectangle::Height()
{
    return height; //or this->height
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. Thank you
These getters and setters may be more efficient by placing them in the header file, allowing the compiler to inline them. (Yes, I know that the compiler could inline them anyways.)
2

Like so.

int Rectangle::Width()
{
  return this->width;  // or `return width;`
}

Note Also please remember to add a header guard at the top of your .h file. And at the top of your .cpp file add a #include "header.h" where header.h should be replaced with the name of the header file with the class definition

9 Comments

This is neat as well. Why not use this-> @NathanOliver? I'd like you to elaborate for me :)
@NathanOliver Just my personal preference
@NathanOliver the this keyword is a pointer reference to the current class object, therefore to access its variables, you must use the pointer accessor operator ->
@m_callens I know what this is. I asked as it is completly unneeded as there is no name hiding going on.
Standard #pragma once warning. This isn't standard C++. The common compilers support it, but if a compiler doesn't recognize a #pragma it is allowed to silently discard it. Be certain your compiler, and all the compilers you may have to support, implement once and implement it the same way. Otherwise you're in for some nasty surprises.
|
0

Yes,what they said is truth.

However if you let me give you an advise, Everytime you have a private variable you want to have get/set functions. Get will return the value of the desired variable Set will assign any value you want.

enter code here    
///Set
 void setWidth(int x){
 width=x;
 }

 //Get
int getwidth(){
  return width;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.