Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access private variables from class in C++

Im working on a project and I want to declare private variables inside a class because I've read in many places that it's better than declaring them public but how can I access them in main ? What kind of functions should I use to make them accessible ? I want to solve the system through a function not from main as I've done. Thats my code so far,

#include <iostream>
#include <limits>

using namespace std;

class Equation
{
    private:
        int a1, a2, b1, b2, c1, c2;

    public:
};

int main() 
{
    int a, b, c, d, e, f;
    cout << "\n" << endl;
    cout << "                        **** Hello ****               \n\n" << endl;
    cout << "This is a program to solve a system of equation" << endl;
    cout << "Equations will look like a1*x+b1*y=c1" << endl;
    cout << "and  a2*x+b2*y=c2\n" << endl;
    cout << "Enter the values for the first equation \n" << endl;

    while ((cout << "Enter the value of a1 :\n") && !(cin >> a)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    while ((cout << "Enter the value of a2 :\n") && !(cin >> b)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    while ((cout << "Enter the value of b1 :\n") && !(cin >> c)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    while ((cout << "Enter the value of b2 :\n") && !(cin >> d)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    while ((cout << "Enter the value of c1 :\n") && !(cin >> e)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    while ((cout << "Enter the value of c2 :\n") && !(cin >> f)) 
    {
        cout << "Invalid input, please enter a number \n" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "The first equation is : " << a << "x" <<
        "+" << c << "y" <<
        "=" << e << "\n" <<
        endl;

    cout << "The second equation is : " << b << "x" <<
        "+" << d << "y" <<
        "=" << f << "\n" <<
        endl;

    double x = ((c * e) - (b * f)) / ((a * e) - (b * d));
    double y = ((a * f) - (c * d)) / ((a * e) - (b * d));

    cout << "The solution of the system is " <<
        "x = " << x << "\t" <<
        "y = " << y <<
        "\n\n" <<endl;
    cout << "                        **** Thank You ****                 " << endl;

    return 0;
}
like image 727
Germelinda Musliaj Avatar asked Dec 02 '22 15:12

Germelinda Musliaj


1 Answers

A very unpopular option is to use setters and getters.

class A {
  public:
    void set_life(int life) { life_ = life; }
    void set_greeting(std::string greeting) { greeting_ = greeting; }
    int get_life() const { return life_; }
    std::string get_greeting() const { return greeting_; }
  private:
    int life_;
    std::string greeting_;
};

These would be used in the following way:

int main() {
  A a;
  a.set_life(42); // My object's life_ variable is now 42.
  a.set_greeting("Hello, World!"); // My object's greeting_ variable is now "Hello, World!".

  std::cout << a.get_greeting() << " The meaning of life is " << a.get_life() << '\n';
  // "Hello World! The meaning of life is 42
}

This is considered a faux-pas because if you introduce getters and setters as a rule for every private variable, you might as well make the variables public - they can be changed anyway.

A better practice is to use your constructor to set your variables initially and create a method to do what you need with your variables - the programmer shouldn't need to know about them (hence private).

class A {
  public:
    // Construct life_ with life and greeting_ with greeting
    A(int life, std::string greeting) : life_(life), greeting_(greeting) {}
    void print_message() const { 
        std::cout << greeting_ << " The meaning of life is " << life_ << '\n';
    }
  private:
    int life_;
    std::string greeting_;
};   

and now your main would look like this:

int main() {
  A a(42, "Hello, World!");
  a.print_message();
}
like image 164
erip Avatar answered Dec 25 '22 11:12

erip