Ger*_*iaj 2 c++ private function
我在一个项目上工作,我想在一个类中声明私有变量,因为我在许多地方都读到它比声明它们为公开要好,但是如何在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;
}
Run Code Online (Sandbox Code Playgroud)
一个非常不受欢迎的选择是使用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_;
};
Run Code Online (Sandbox Code Playgroud)
这些将通过以下方式使用:
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
}
Run Code Online (Sandbox Code Playgroud)
这被认为是虚假的,因为如果您将getter和setter作为每个私有变量的规则引入,则不妨将这些变量设为公用-可以随时对其进行更改。
更好的做法是使用构造函数来初始设置变量,并创建一种方法来对变量进行所需的操作-程序员无需了解变量(因此不公开)。
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_;
};
Run Code Online (Sandbox Code Playgroud)
现在您的主体看起来像这样:
int main() {
A a(42, "Hello, World!");
a.print_message();
}
Run Code Online (Sandbox Code Playgroud)