gpt4 book ai didi

c++ - 从 C++ 中的类访问私有(private)变量

转载 作者:太空狗 更新时间:2023-10-29 23:35:32 25 4
gpt4 key购买 nike

我正在做一个项目,我想在一个类中声明私有(private)变量,因为我在很多地方读到它比将它们声明为公共(public)变量要好,但我如何在 main 中访问它们?我应该使用什么样的功能来使它们易于访问?我想通过一个不是像我所做的那样来自 main 的函数来解决系统问题。到目前为止,这就是我的代码,

#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;
}

最佳答案

一个非常不受欢迎的选择是使用 setter 和 getter。

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_;
};

这些将以下列方式使用:

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
}

这被认为是一种失礼,因为如果您将 getter 和 setter 作为每个私有(private)变量的规则引入,您还不如将变量公开 - 它们无论如何都可以更改。

更好的做法是使用您的构造函数来初始设置您的变量,并创建一个方法来使用您的变量执行您需要的操作 - 程序员不需要了解它们(因此是私有(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_;
};

现在你的 main 看起来像这样:

int main() {
A a(42, "Hello, World!");
a.print_message();
}

关于c++ - 从 C++ 中的类访问私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35003008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com