Want to check if I understand this correctly.

I did a simple addition thing using classes. With some variables being in private. I managed to make it work by trying a lot by looking at lecture notes.

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

class myClass {
	private:
	int length;
	int width;
	
	public:
		myClass(int, int);
		int add(){
			int sum;
			sum=length+width;
			cout<<sum;
		}
	
};

myClass::myClass(int l, int w){
	length=l;
	width=w;
}


int main(){
	int a,b;
	cout<<"enter value for length and width\n";
	cin>>a;
	cin>>b;
	myClass s1(a,b);
	s1.add();
}


This is the image of what i think is happening. I dont know how to put it into words.

https://imgur.com/hhXvBbQ
Last edited on
What you posted won't compile cleanly.
You have an error at line 15.
'myClass::add': must return a value

add is declared as an int function, but you have no return statement.

Adding:
 
    return sum;

The program works correctly.

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
Last edited on
Thank you AbstractionAnon. And Handy Andy for the detailed review. I have understood what you have said but I have more questions.

1. What is ctor?

2. What is line 12 doing? What are the length and width in the normal brackets? And why is there ' {} ' at the end?

3. In line 34, what is the use of putting {} after the variables. These variables are just 'a' and 'b' from my code right? I didnt know we can use same variables as the one inside the class. And also, why did you comment that line in between [width][/width]

4. Are there rules followed while indenting? Or is it solely up to preference mostly?
Last edited on
1. What is ctor?

Programmer speak for "constructor." dtor for "destructor."

2. What is line 12 doing?

A class constructor initializer list.
https://www.learncpp.com/cpp-tutorial/constructor-member-initializer-lists/

3. In line 34, what is the use of putting {} after the variables.

Uniform initialization.
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

4. Are there rules followed while indenting?

About the only "rule" is BE CONSISTENT. How many spaces to indent and whether it is tabs or actual spaces is individual preference. As well as other types of formatting such as code block brace location.

"Internet Religious Wars" have been fought over such minutiae.
another habit to break early in your learning is to not put interface and work together. For a long time, most of your school or learning career actually, cin and cout are your 'interface'.
Function add should not print the value. let the USER do this, the user can say cout << thing.add() << endl; The user may instead have wanted to say myguiwindow.editboxnumber42.settext(to_string(thing.add)); instead, and could be irate as his gui program popping up a console with a random number printed on it because your add function called cout.
So IMHO...
int add()
{
return width+length; //see how I do not need an excess 'sum' variable at all here?
}

for now also note that namespace std is frowned upon. I use it, its ok to use in schoolwork and esp on this site to make your little snippets work in the online compiler, but at least be *aware* that it is not a good plan to use and circle back to that later on. Not using it is messy, and there are a few different approaches that are not important today.
Last edited on
Uhh. Sorry. I'm still completely new. So trying to understand as much as I can.

Regarding line 12.

length(length), width(width)

the bracket part are the variables in the main function? and the length and width from the myClass is being initialized to take those variables from the main function?

Also why is this line ending with a {} as well?
I do not understand what jonnin is saying.. Sorry. I understood how we can avoid the sum variable. But I'm lost at what the other parts are trying to say.
I am telling you to move your print (cout) statement in add out of there (or, in general, that is something you usually want to do as you get into more complicated programs). A function is supposed to do one task. Here, the task is to add numbers, not generate output. Putting it all in one makes it less flexible; you can't add the values and choose to NOT print them as you have it (maybe you just need that sum value), or to print them to a file or GUI or anything else.

Topic archived. No new replies allowed.