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.
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, i.e:
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.
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.
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.
Related
I'm learning C++.
Came across data encapsulation and data hiding at a website, check out the following piece of code:
#include<iostream.h>
#include<conio.h>
class sum {
private: int a, b, c;
public:
void add() {
clrscr();
cout << "Enter any two numbers: ";
cin >> a >> b;
c = a + b;
cout << "Sum: " << c;
}
};
void main() {
sum s;
s.add();
getch();
}
NOW. As it says here that:
The main advantage of using of encapsulation is to secure the data from other methods, when we make a data private then these data only use within the class, but these data not accessible outside the class.
What happens underneath the code, what does the compiler do that makes it inaccessible to other classes? And in the given example what was the reason behind defining a,b and c as private.
What were they trying to achieve by hiding "just the declarations of the three variables"? Because anyone can see that inside public three numbers being used are a,b, and c - first two for input and third one for output.
How is this possible that the data under private can't be accessed
outside the class?
Compiler makes sure you don't. If you try to access say a outside class, your code will not compile.
And in the given example what was the reason behind defining a,b and c
as private.
It could be anything! But as a result, a,b and c are not accessible outside members of class.
Basically you want to hide some variables in your class for the sake of consistency. So that you or your clients can not produce a code that makes unwanted and uncontrolled changes.
Updates:
What happens underneath the code, what does the compiler do that makes
it unaccessible to other classes?
Compiler implementation check for access level while producing code. If there is something wrong, you will get a syntax error and no machine code will be generated from your file.
And in the given example what was the reason behind defining a,b and c
as private; what were they trying to achieve by hiding "just the
declarations of the three variables"? Because anyone can see that
inside public three numbers being used are a,b, and c - first two for
input and third one for output.
You don't hide variables in your class to make them invisible to others. Private variables that are not intended to be used from outside of the class can be marked as private to limit the potential for coding errors.
As an example consider following class:
class rectangle {
public:
int width;
int height;
int area;
};
void something_important(const rectangle& r) {
// ...
}
What happens if I pass a rectangle of width -10, height 0 and area of -15? There could be a plane crash or a nuclear weapon launched to some wrong target... So I will make sure my rectangles are always valid:
class rectangle {
public:
void set_width(int w) {
if(w) width = w;
else width = 0;
area = width*height;
}
int get_width() const {return width;}
void set_height(int h) {
if(w) height = h;
else height = 0;
area = width*height;
}
int get_height() const {return height;}
int get_area() const {return area;}
private:
int width;
int height;
int area;
};
So no one can make a rectangle of negative height or width, and no one can make a rectangle having a wrong area. (you can not actually change area directly)
I hope it makes sense for you now.
What happens underneath the code, what does the compiler do that makes it unaccessible to other classes?
Not much. The compiler doesn't protect against access to the data. It protects against access to the name of the data. For instance:
void foo(class bar&, int&);
class bar {
int i = 0;
public:
void baz() {
foo(*this, i);
}
};
void foo(class bar& b, int& i) {
//b.i = 42; // This is an error. b.i is private
i = 42; // This is okay, no matter what the local i refers to
}
In the example above, foo() cannot access b.i by name, because it's a private data member. But it can still modify it if it obtains a reference by other means. The member function baz() which has access to that name, binds it to the reference that foo() accepts. Thus allowing for its modification from outside the class's scope.
I hope someone can help me. In an effort to be more specific about what I really need, and trim down my code, I have changed from having a vector purely of my Class, to having a vector of objects of a new class, of which my original class is a type within.
I hope I have explained myself clearly up until this point. I will show the relevant classes:
class screen_area
{
private:
int my_id, my_x, my_y, my_width, my_height;
bool active=true;
public:
screen_area (int button_id=0, int x=0, int y=0, int width=0, int height=0, bool isactive=true)
{
my_id = button_id;
my_x = x;
my_y = y;
my_width = width;
my_height = height;
active = isactive;
}
~screen_area()
{}
class bet
{
private:
int wager = 0;
int multiplier = 0;
public:
screen_area area;
bet(int wager, int multiplier, screen_area area)
{};
~bet()
{};
There is a little more to them, but this is the bread and butter. Now previously I had used a member function within "screenarea", to return any value I had wanted from a specific object:
int getvalue(int value)
{
switch(value)
{
case 1 :
return my_id;
case 2 :
return my_x;
case 3 :
return my_y;
case 4 :
return my_width;
case 5 :
return my_height;
case 6 :
return active;
}
}
And I have modified a lookup function to use this member function on the screenarea that is a type contained within "bet".
int returnbuttonid(int mousex, int mousey, std::vector<bet> *buttons)
{
for (auto ep : *buttons )
{
if ((ep.area.getvalue(2) > mousex) && (ep.area.getvalue(3) > mousey))
{int id_value = ep.area.getvalue(1);
return id_value;
}
}
}
However... it returns garbage. I'm clearly missing something, but I am going through it logically and it all seems to make sense.
Sorry in advance if it is something simple! And I appreciate that this may seem long winded but I would really appreciate some help!
And just to be super clear... this is how I am calling it:
vector<bet> localbuttons; //Declaration of Vector
load_map("data.dat", &localbuttons); //load buttonmap using function
int buttonpressed = returnbuttonid(100,300, &localbuttons);
In response to a very speedy comment. It's clear that the problem at least starts with an unpublished piece of code. My vector of "bet" is not being filled with the arguments I am passing to it when I try to overload the constructor. I presumed I had corrected the syntax correctly when I created the new class "bet", but after probing the vector it is not showing any data.
In my function load_map:
bool load_map(std::string path, std::vector<bet> *buttons)
{
//setup file
ifstream inputFile( path.c_str() );
//
//The stuff in the middle here is irrelevant
//and I've take it out to make this tidier
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
}
return 0;
}
Now the only part of this that has changed since I had this function was working is:
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
So I am guessing this is where the problem originates. The variables are not overloading the default screen_area constructor. So when I:
cout << localbuttons[1].area.my_id << endl;
I always see whatever value I place in the default constructor. It is "0" in the constructor I have posted here, but if I change it, it changes correspondingly.
And I shouldn't have said garbage, I was at fault for thinking I had correctly identified the area of the problem, and trying to be concise. So I guess I should be asking first... How can I correctly overload this "screenarea" constructor?
The problem here was in the Constructor of the Bet class.
After having a look here:
http://www.cplusplus.com/doc/tutorial/classes/
I rewrote the constructor in the Bet class:
bet(int w, int m, int button_id=0, int x=0, int y=0,
int width=0, int height=0, bool isactive=true)
: area(button_id, x, y, width, height, isactive),
wager(w), multiplier(m)
{};
My apologies if I wasted anyone time with misdirection, and thanks for the sensible advice from Jonathon Potter.
I'm not sure why I thought you could call constructors within parentheses. My compiler didn't seem to complain about it, but from what I can gather - I was just creating a temporary object.
I have a take home test that it work a large percentage of my grade. These 3 questions are on it and they are worth half the test. I was wondering if someone could kindly review my quiz and make sure they are correct for me. I’m a worrier and I want to make sure they are correct. Thank you in advance!
Implement the constructor for the class called "SimpleMath". The constructor takes two integer parameters; "var1" and "var2". The constructor is to store the value that was passed into "var1" into the private integer member variable "m_value1" and the value that was passed into "var2" into the private integer member variable "m_value2"
class SimpleMath
{
public:
SimpleMath(int var1, int var2) : m_value1(var1), m_value2(var2) {};
int getVar1() const
{
return m_value1;
}
int getVar2() const
{
return m_value2;
}
private:
int m_value1;
int m_value2;
};
Implement the "Multiply" method for the "SimpleMath" class. This method does not require any parameters and returns an integer value. This method should multiply the values stored in the classes private integer member variables "m_value1" and "m_value2" the resulting value is returned. Assume that "m_value1" and "m_value2" were loaded inside the classes constructor.
class SimpleMath
{
public:
SimpleMath(int var1, int var2);
int Mutiply= m_value1* m_value2;
private:
int m_value1;
int m_value2;
};
Write a class definition called "SimpleMath" that has a constructor that takes two integers "var1" and "var2". It has four public methods that take no parameters and return an integer value; "Add","Subract","Divide" and "Multiply". The class has two private member variables of type integer; "m_value1" and "m_value2".
class SimpleMath
{
SimpleMath(int var1, int var2);
public:
int Add;
int Subract;
int Divide;
int Multiply;
private:
int m_value1;
int m_value2;
};
Am I to assume you have no prior programming experience?
At any rate, the first part looks ok. What the : denotes is the initializer list. It can, and is, correctly used to initialize a class's members.
Second part, you got it dead wrong I am afraid.
First of, you've not implemented the method Multiply() there, you've just declared a variable.
Instead do this:
int Multiply()
{
int sum = m_value1 * m_value2;
return sum;
}
For brevity's sake, you can also do this:
int Multiply()
{
return m_value1 * m_value2;
}
Does the same thing.
Also note that the answer from question 1 has not carried over to question 2, ie the constructor is incomplete. It won't compile.
As for question 3, it's just question 2 all over again, except you've also got to implemented the three remaining arithmetic functions. Suffice it to say, I am sure you can figure it out.
I am implementing a class and I have a function that does things using lots of variables that need to be declared and initialised.
I'd like the variable declarations not to clutter the function and do something like:
doFunction(){
declare();
//Do things with variables declared in declare()
}
void declare(){
//declare lots of variables here
}
This does not work as the variables are scoped to declare() and aren't seen by doFunction(). What's a sensible way to handle this problem?
Since each of the variables that you declare must be assigned a value, you should combine declaration with initialization. In other words, instead of
int x;
double y;
std::string z;
x = 1;
y = 2.0;
z = "3";
do this:
int x = 1;
double y = 2.0;
std::string z("3");
This is pretty much as far as you can push this approach with locals: declaring variables is an essential part of the function body, you cannot (and arguably, should not) move it to a remote location.
You can also move the member function into a nested private class, move the local variables into the class, and do calculations there:
class specialCalc {
int x;
double y;
std::string z;
specialCalc() : x(1), y(2.0), z("3") {}
public:
int calculate() {
...
}
};
void doFunction() {
specialCalc calc;
cout << calc.calculate() << endl;
}
PS: I am deliberately not mentioning preprocessor-based solutions because they would negatively impact readability.
I'm not really advocating this, but:
struct Declare
{
int x;
float y;
char z;
vars() :x(1),y(3.14),z('z') {}
};
void doFunction()
{
Declare vars;
// use vars.x, vars.y and vars.z as your variables
}
You have a number of choices:
1) Get over it. If you need lots of variables, you'll need to suffer the fact they need to be declared somewhere.
2) Put them into a class or in a structure as a member variable so you can declare them in the .h file and they'll be invisible in the .C/.cpp file.
3) Aggregate them into an array, and declare only the array and initialize them in a for() loop or something. This really only works if they're all a similar type and you don't do silly things like "index 4" is my "counter object for this", and "index 5" is my "thing I'm going to print to the screen" as then you loose the name associated with the variable itself, which is rather helpful when reading the code later (of course).
4) put them in a define statement somewhere else:
#define MYVARS int a; char b[1024]; ...
void funstuff() {
MYVARS
}
5) Modify an IDE so that it can hide/collapse the variable declarations when you're viewing the code.
Note that of all of these choices, number 1 is still probably the right answer :-)
I've probably become a bit to used to Java and am finding this harder than it should be. Heres what I have.
myObject[0] = new item1(this);
class item1
{
private:
int x;
int y;
public:
item1( passedPointer* pOne )
{
x = 5;
y = 5;
}
int returnX() { return x; }
int returnY() { return y; }
}
Then in another method I thought I could just say:
void check()
{
int y = item1.returnY();
int x = item1.returnX();
}
But I am getting the common error: a nonstatic member reference must be relative to a specific object.
There is only one instance of this class item1, what would be the best way to do this? This is just a simplified fragment of what I'm actually doing, not the actual code.
Item1 is a class. You have to create an instance of it before you can access its non-static members. Try looking here for some basic information.
void check(){
int y = item1.returnY;
int x = item1.returnX;
}
This would also be incorrect in Java, since neither returnX nor returnY are statics, you need an object on which to apply the operation, and you also need the parenthesis of the method call:
void check() {
item1 i;
int y = i.returnY();
int x = i.returnX();
}
Perhaps implementing the Singleton pattern would not do you harm, since you want only one instance of the object. You could declare the object as global or static to a function too, then get the values.
Then again, you could also declare the functions as static, and add another one to initialize the static values of the variables which need to be returned by those methods. There are a lot of solutions to this depending on your situation which can not be fully grasped by the short amount of code you have pasted.
You created an instance of class item1 with the line
myObject[0] = new item1(this);
Unlike JAVA, in C++ there are pointers and new returns a pointer to the object (so myObject[0] is a pointer to the instance) so you need the -> operator. To activate the method you should write:
myObject[0]->returnX();
If you wish to have only one instance than implement the class as a singleton.