class

hello im trying to grasp classes do you have any tips for me and can you explain this peice of code to me
1
2
setup setapp1;
setapp1.title();




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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

class setup{
    public:
    void title(){
         cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
         }
};
    


int main()
{
    setup setapp1;
    setapp1.title();
    system("TITLE calculator");
    system("color 2");
    double a;
    double b;
    char cchar;
    char cagain;
    
    do{
    
    cout << "please enter the first number you want to use" << endl;
    cin >> a;
    cout << "please enter the operation you would like to use" << endl;
    cin >> cchar;
    cout << "please enter the second number you would like to use" << endl;
    cin >> b;
    
    switch (cchar){
           
           case '+':
                cout << "the answer is: " << a << " + " << b << " = "
                << (a + b) << endl;
                break;
           case '-':
                cout << "the answer is: " << a << " - " << b << " = "
                << (a - b) << endl;
                break;
           case '*':
                cout << "the answer is: " << a << " * " << b << " = "
                << (a * b) << endl;
                break;
           case '/':
                cout << "the answer is: " << a << " / " << b << " = "
                << (a / b) << endl;
                break;
           default:
                cout << "you cant use that operation";
                break;
                }
                cout << "would you like to start again (y or n)";
           cin >> cagain;
           }while (cagain == 'y' || cagain == 'Y');
           
    system("pause");
    return 0;
}
Last edited on
setup setapp1;
Create an object of type setup. Name this object setapp1. Create it using its default constructor.

setapp1.title();
Call the title() member function of the object setapp1.
What those two lines are doing are creating a setup object, and then accessing its title function. Just like strings are objects, so is setup, so saying

string setapp1;

would create a string object, just like

setup setapp1;

creates a setup object.

Accessing a member function is like accessing a regular function, except it's particular to a certain class. typing in the name of an instance of an object (in this case, setapp1), a '.' (dot), and the name of the function (in this case, title()), will make the code jump to the function up there, and do whatever it says to do.

I dunno if this helps any, but here you go
ok thanks and is there any way to let the user enter any amounts of numbers he wants to use. for example on this program you can only enter two numbers but is there i way i can like let them choose how many numbers they want to add?

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

using namespace std;

class setup{
    public:
    void title(){
         cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
         }
};
    


int main()
{
    setup setapp1;
    setapp1.title();
    system("TITLE calculator");
    system("color 2");
    double a;
    double b;
    char cchar;
    char cagain;
    
    do{
    
    cout << "please enter the first number you want to use" << endl;
    cin >> a;
    cout << "please enter the operation you would like to use" << endl;
    cin >> cchar;
    cout << "please enter the second number you would like to use" << endl;
    cin >> b;
    
    switch (cchar){
           
           case '+':
                cout << "the answer is: " << a << " + " << b << " = "
                << (a + b) << endl;
                break;
           case '-':
                cout << "the answer is: " << a << " - " << b << " = "
                << (a - b) << endl;
                break;
           case '*':
                cout << "the answer is: " << a << " * " << b << " = "
                << (a * b) << endl;
                break;
           case '/':
                cout << "the answer is: " << a << " / " << b << " = "
                << (a / b) << endl;
                break;
           default:
                cout << "you cant use that operation";
                break;
                }
                cout << "would you like to start again (y or n)";
           cin >> cagain;
           }while (cagain == 'y' || cagain == 'Y');
           
    system("pause");
    return 0;
}

    
is there i way i can like let them choose how many numbers they want to add?


Ask them how many numbers they want to add, and then loop that many times, getting a number each time. I refuse to believe you couldn't think of that yourself.
oh wow me too
also in the function"void title" is there a way i can only make the program show what that function says and make it say press enter to continue and then make "hello!!!, IM A CALCULATOR... lets get started" go away and continue with the program
Topic archived. No new replies allowed.