04. Python vs. C++:类的使用

在程序中使用类

在深入了解如何编写 C++ 类的细节之前,首先要了解如何在 main.cpp 程序中使用类。在处理大项目时,你可能需要使用一个类,而实际上并不负责实施该类。

下面是一个使用 Gaussian 类的 main.cpp 文件。

首先,你的 main.cpp 文件需要在文件的顶部声明所有的变量和函数。然后,main() 函数需要有权访问该类,并可以使用该类的方法:

# include <iostream>

//声明类
class Gaussian
{
    private:
        float mu, sigma2;

    public:

        //构造函数
        Gaussian ();
        Gaussian (float, float);

        //改变均差和标准偏差值 
        void setMu(float);
        void setSigma2(float);

        //输出均差和标准偏差值
        float getMu();
        float getSigma2();

        //待评估函数 
        float evaluate (float);
        Gaussian mul (Gaussian);
        Gaussian add (Gaussian);
};

int main ()
{

    Gaussian mygaussian(30.0,20.0);
    Gaussian othergaussian(10.0,30.0);

    std::cout << "average " << mygaussian.getMu() << std::endl;
    std::cout << "evaluation " << mygaussian.evaluate(15.0) << std::endl;

    std::cout << "mul results sigma " << mygaussian.mul(othergaussian).getSigma2() << std::endl;
    std::cout << "mul results average " << mygaussian.mul(othergaussian).getMu() << std::endl;

    std::cout << "add results sigma " << mygaussian.add(othergaussian).getSigma2() << std::endl;
    std::cout << "add results average " << mygaussian.add(othergaussian).getMu() << std::endl;

    return 0;
}

接下来,我们学习类的编程步骤。

对象实例化

首先,你需要在程序的顶部声明该类。然后,在 main 函数内部,你可以实例化对象:

    Gaussian mygaussian(30.0,20.0);
    Gaussian othergaussian(10.0,30.0);

第一个对象叫做 mygaussian,平均值为 30,方差为 20。第二个对象叫做 othergaussian,平均值为 10,方差为 30。

使用对象方法

然后,你可以通过以下代码使用对象方法:

mygaussian.getMu()

它可以输出 mygaussian 对象内的平均值。

另一个例子:

mygaussian.add(othergaussian)

它可以把 mygaussian 和 othergaussian 相加。

下面是在这个代码中使用的所有文件,方便你观察 main.cpp 和 gaussian.cpp 之间的关系

这个程序首先通过以下命令被编译,但你在后端看不到:

g++ main.cpp gaussian.cpp

点击“测试运行”按钮,然后查看结果。

Start Quiz:

#include <iostream>

// class declaration
class Gaussian
{
	private:
		float mu, sigma2;

	public:
		
		// constructor functions
		Gaussian ();
		Gaussian (float, float);

		// change value of average and standard deviation 
		void setMu(float);
		void setSigma2(float);

		// output value of average and standard deviation
		float getMu();
		float getSigma2();

		// functions to evaluate 
		float evaluate (float);
		Gaussian mul (Gaussian);
		Gaussian add (Gaussian);
};

int main ()
{

	Gaussian mygaussian(30.0,100.0);
	Gaussian othergaussian(10.0,25.0);
	
	std::cout << "average " << mygaussian.getMu() << std::endl;
	std::cout << "evaluation " << mygaussian.evaluate(15.0) << std::endl;

	std::cout << "mul results variance " << mygaussian.mul(othergaussian).getSigma2() << std::endl;
	std::cout << "mul results average " << mygaussian.mul(othergaussian).getMu() << std::endl;

	std::cout << "add results variance " << mygaussian.add(othergaussian).getSigma2() << std::endl;
	std::cout << "add results average " << mygaussian.add(othergaussian).getMu() << std::endl;

	return 0;
}
#include <math.h>       /* sqrt, exp */

// class declaration
class Gaussian
{
	private:
		float mu, sigma2;

	public:
		
		// constructor functions
		Gaussian ();
		Gaussian (float, float);

		// change value of average and standard deviation 
		void setMu(float);
		void setSigma2(float);

		// output value of average and standard deviation
		float getMu();
		float getSigma2();

		// functions to evaluate 
		float evaluate (float);
		Gaussian mul (Gaussian);
		Gaussian add (Gaussian);
};


Gaussian::Gaussian() {
	mu = 0;
	sigma2 = 1;	
}

Gaussian::Gaussian (float average, float sigma) {
	mu = average;
	sigma2 = sigma;
}

void Gaussian::setMu (float average) {
	mu = average;
}

void Gaussian::setSigma2 (float sigma) {
	sigma2 = sigma;
}


float Gaussian::getMu () {
	return mu;
}

float Gaussian::getSigma2() {
	return sigma2;
}

float Gaussian::evaluate(float x) {
	float coefficient;
	float exponential;

	coefficient = 1.0 / sqrt (2.0 * M_PI * sigma2);
	exponential = exp ( pow (-0.5 * (x - mu), 2) / sigma2 );
	return coefficient * exponential;
}

Gaussian Gaussian::mul(Gaussian other) {
	float denominator;
	float numerator;
	float new_mu;
	float new_var;

	denominator = sigma2 + other.getSigma2();
	numerator = mu * other.getSigma2() + other.getMu() * sigma2;
	new_mu = numerator / denominator;

	new_var = 1.0 / ( (1.0 / sigma2) + (1.0 / other.sigma2) );

	return Gaussian(new_mu, new_var);
}

Gaussian Gaussian::add(Gaussian other) {

	float new_mu;
	float new_sigma2;

	new_mu = mu + other.getMu();
	new_sigma2 = sigma2 + other.getSigma2();

	return Gaussian(new_mu, new_sigma2);
}