C++ class and object

Hello, I have been experimenting with classes and objects in c++. I have made a very basic program using a class with 2 basic functions. The program mostly works and can be compiled but I want some feedback.

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
string c, d;
class passWord{
    public:
        void pass(){
            while (c != "24090844" && d != "Ethan") {
                cout << "Enter Username: ";
                cin >> d;
                cout << "Enter Password: ";
                cin >> c;
                system("clear");
                }
            }
    public:
        void passr(){
            while (c != "Miller" && d != "4984") {
            cout << "Enter Username:  ";
            cin >> c;
            cout << "Enter Password:  ";
            cin >> d;
            system("clear");
            }
        }
};
int main()
{
    passWord object;
    object.pass();
    int a;
    cout << "1 = Calc 2 = Money Manager: ";
    cin >> a;
    system("clear");
    if (a == 2){
        object.passr();
        int y, s, d, l;
        s = 4000;
        cout << "Would you like to withdrawl or add to your account:  ";
        cin >> c;
        system("clear");
        if (c == "add")
        {
            cout << "balance: " << s << endl << endl << endl;
            cout << "How much do you want to deposit:  ";
            cin >> d;
            system("clear");
            cout << "Balance: " << s+l << endl << endl << endl;
            cout << "You total money is " << s+l;
        }
        if (c == "withdrawl")
        {
            cout << "balance: " << s << endl << endl << endl;
            cout << "How mcuh do you want to take: ";
            cin  >> l;
            system("clear");
            cout << "Balance: " << s - l << endl << endl << endl;
            cout << "here is your money: " << l << endl;
        }
    }
    if (a == 1){
        string b;
        int flag = 0;
        char a;
        int r, d;
        while (flag==0) {
            cout << "enter the oporator: ";
            cin >> a;
            system("clear");
            cout << "Enter the numbers: ";
            cin >> r >> d;
            system("clear");
            switch (a)
            {
                case '+':
                cout << r + d << endl;
                break;
                case '-':
                cout << r - d << endl;
                break;
                case '*':
                cout << r*d << endl;
                break;
                case '/':
                cout << r/d << endl;
                break;
            }
            cout << "type 'again' to do another calcation: ";
            cin >> b;
            system("clear");
            if (b != "again")
            {
                flag = 1;
            }
        }
    }
return 0;
}

1 Like

Good stuff Ethan.

Why is it a class, though? Is the idea that you’re going to use it in another program?

The purpose of this program was for me to learn how to write basic classes and objects and be able to apply them to the main function.

1 Like

Perfect! That’s exactly how you do it, then.

I’m impressed that you’ve learned all this on your own. We should hook you up with a formal course on code.org or one of those places. Or if you get a chance to take an “advanced coding course” soon, we can get you more resources.

C++ is a good language to learn, and you’re learning the basic structures pretty well. If you ever want to branch out, try Python next :slight_smile:

1 Like

thanks for a good card i can pull when we argue on what language is better

Looks good. This is the first language you learned? If so, wow!

1 Like

I tried convincing him to start off with a more friendly language like Python or JavaScript and didn’t expect him to actually learn c++ and be (somewhat) fluent in it, still surprised to this day. Unfortunately he quit programming.

1 Like