Hello hishiiro,
Your lines are a bit off.
The red line ends at the right place, but should start at line 30. Line 30 is defining an object of the class using the overloaded ctor to set the private variables.
The black line should be pointing to the private variables of the class because that is what the ctor is doing.
The red lines from line 11 pointing to line 13 should not be there. You could start a line on 13 and point to the private variables to show what you are using.
Line 31 should be pointing to the function on line 11.
Along with what
AbstractionAnon has noted consider this for your program:
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 42 43 44 45 46 47
|
#include <iostream>
using namespace std;
class myClass
{
private:
int length;
int width;
public:
myClass(int length, int width) : length(length), width(width){}
int add()
{
return length + width;
//int sum;
//sum = length + width;
//cout << sum;
}
};
//myClass::myClass(int l, int w)
//{
// length = l;
// width = w;
//}
int main()
{
int length{}, width{}; // <--- ALWAYS initialize all your variables.
cout << "Enter value for length: ";
cin >> length;
std::cout << "Enter value for width: ";
cin >> width;
myClass s1(length, width);
std::cout << "\n The sum is: " << s1.add();
return 0; // <--- Not required, but makes length good break point for testing.
}
|
Just a note. Everything between the {}s of a class is private by default until you change it, so saying "private:" is OK, but not necessary unless you put the "public:" section first.
Indenting under the "private" and "public" sections is not requires, as far as I know, but it does make it easier to read. Your choice.
In the "add" function you already have values for the 2 variables, so all you need is to return the sum. This also takes care of the error that
AbstractionAnon mentioned.
If you can use the initialization list, line 12, the function at line 25 is not needed.
Andy
Edit: tag