简体   繁体   中英

C++ Rectangle Class clarification regarding syntax

I'll try to make this as concise as possible and while I understand that these questions can be considered "basic" I have already looked at websites such as cplusplus.com and yolinux tutorials but i need somebody to explain this to me like I have just had a major head trauma..

1)

class Rectangle {
private:
    int lineNumber; // LineNumber of the ACSIL Tool
    float valueMax; // value of the higher limit of the rectangle
    float valueMin; // value of the lower limit of the rectangle
public:
    Rectangle(SCStudyInterfaceRef sc, int lineNumber, float valueMax, float valueMin);
    int getLineNumber();
    float getValueMax();
    float getValueMin();
};

So int linenumber, valueMax and ValueMin are declared private members and thus are only accessible by members of the same class, thats fine. But what about the part that follows the "public:" ?

a) Is Rectangle(SCStudyInterfaceRef sc, int lineNumber, float valueMax, float valueMin); a function that is being overloaded? and if yes are int getLineNumber() etc part of that function or seperate members of the public part of the class?

2)

Rectangle::Rectangle(SCStudyInterfaceRef sc, int lineNumber0, float value1, float value2) {
    lineNumber = lineNumber0;
    int value2_greater_than_value1 = sc.FormattedEvaluate(value2, sc.BaseGraphValueFormat, GREATER_OPERATOR, value1, sc.BaseGraphValueFormat); 
    if (value2_greater_than_value1 == 1) {
        valueMax = value2;
        valueMin = value1;
    } else {
        valueMax = value1;
        valueMin = value2;
    }
}

int Rectangle::getLineNumber() {
    return lineNumber;
}

float Rectangle::getValueMax() {
    return valueMax;
}

float Rectangle::getValueMin() {
    return valueMin;
}

a) I'm pretty sure that the functions defined inside the public part of the rectangle class are being "defined" here, or something along those lines.

b) I am really confused about what is happening here on the Rectangle::Rectangle(SCStudyInterfaceRef sc, int linenumber0, float value1, float value2) part. I understand the logic of what is happening within the function itself but i am confused about the paramters being input within the " ( ) " and how exactly this relates to what happenes inside the class public part. This really is the most important question that needs answering.

I have tried to be as concise and onpoint as possible, would appreciate some help in understanding this syntax.

Rectangle::Rectangle is the class constructor. It is called whenever a Rectangle object is created. Read about constructors to understand better.

The constructor is setting initial values for the valueMax and valueMin member variables. It uses the parameters passed to the constructor to do this. Read about function parameters to understand better.

Question 1
It's a constructor with 4 parameters.

int getLineNumber();
float getValueMax();
float getValueMin();

are all member functions in the class.

Question 2
The constructor defined earlier is called with 4 parameters. If no other constructor is defined then you'll have to instantiate the class with exactly 4 parameters, ie:

Rectangle *rect = new Rectangle(sc, 100, 1.2, 6.8);

or simply:

Rectangle rect(sc, 100, 1.2, 6.8);

These parameteres are then used to "set the object in an initial state".

The member functions are used to get various values in their current (or final or only) state.

1) a: If no ctor function is declared, then the compiler writes a ctor for the class. But when a ctor is provided by the class no default ctor is written by the class and hence no overloading is taking place. Now if you go on and define one more ctor, may be because you want the object to be constructed in some other way, then you will have an overloaded ctor. In your case no overloading is taking place.

int getLineNumber() is just another member of the class.

2) a: You are correct. b: The parameters put inside "( )" are arguments list and if this function is called somewhere, then this list is type-matched and then function is called(in case of overloading). Now if you write a statement like:

Rectangle x(a, b, c, d);

then it means that your sc=a, lineNumber0=b, value1=c, value2=d for this function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM