Help with Class please

closed account (1Ap9LyTq)
Here is my code :
#include<iostream>
#include<conio.h>
#include<ostream>
using namespace std;
class MyClass
{
private:
int a, b,c;
public:
MyClass(int temp){
b = temp;
}
void add(int a)
{
int total;
total = a + b;
cout << total << endl;
}

char* display(){
return "This is the display method";
}

};
int main(int argc, char *argv[])
{
MyClass mc(2);
mc.add(6);
cout << mc.internalSum << endl;// and here where I have problem I cant understand what method is this one.
cout << mc.display() << endl;
system("pause");

return EXIT_SUCCESS;
}
cout << mc.internalSum << endl;// and here where I have problem I cant understand what method is this one.
Neither do I. You can't call non-existing member functions

PS: you use #include<conio.h> I think that comes from C, not C++. Not needed
closed account (1Ap9LyTq)
That's my problem I could write the operator add and display but I can't figure out what I am suppose to write for internalsum. I used void , int ... internalsum as a function in my class but it did not work.
I did something so silly but gave me the output I wanted .


class MyClass
{
private:
int a, b,c;
public:
int MyClass::internalSum= 2+6;
MyClass(int temp){
b = temp;
}
void add(int a){
int total;
total = a + b;
cout << total << endl;
cout << "The internal sum is ";
}
char* display(){
return "This is from the display method";
}

with the same main. Then I will get something close to my desirable outcomes.
8
                 The internal sum is 8 - This is from the display method

                 Press any key to continue
Please use Code tags when posting snippets.

int MyClass::internalSum= 2+6;

Here you are assignig the value 8 to the variable "internalSum". It is never changed, so it will always return 8.

My guess is that you want the result of "add()" to be saved in "internalSum". Now, you are adding a to b in total, but total is discarded as it only exists in the scope of the add() function. Once add() returns, total is destroyed and its value lost. Instead, try saving the result of "a + b" into the internalSum value.

Also, you have three ints: 'a', 'b' and 'c'. However, you only use one: 'b'. The naming of internalSum makes me think that your class is only supposed to carry one number (internalSum), which is manipulated via functions and printed via "display".

As a hint, here's how your main should look and what it displays:
1
2
3
4
5
6
MyClass mc(2);
mc.display(); // "The value is now 2"
mc.add(8);
mc.display(); // "The value is now 10"
mc.add(4); 
mc.display(); // "The value is now 14" 


The key features of the code you have to write are these:
a) "MyClass" holds one value: the current total.
b) The function add() should add the parameter value to the current total.
c) The function display() should print out the current total.
[Optional d) Outside access to variables should be restricted.]
closed account (1Ap9LyTq)
If I have to write a code specifically for that main to get that result, I can do it but I am stocked in the internalsum part. The second post as I said was so silly way. My brain is frozed. I have written that code like 100 times with my own way but here with this main I can't figure out the internalsum.

cout << mc.internalSum << endl;// 

if someone explain what this line is. I will try to figure it out .
The int c was a faild try. Sorry.

Last edited on
Topic archived. No new replies allowed.