类和对象的初步认识

/*
time: 2020年4月17日22:06:44
where:gfdx
function:类和对象的基本概念*/
#include<iostream>
using namespace std;
class complex {
private:
	double real;//复数的实部
	double imag;//虚数的虚部
public:
	void init(double r, double i)//给real和imag赋初值
	{
		real = r;
		imag = i; 
	}
	double realcomplex()//求复数的 实部
	{
		return real;
	}
	double imagcomplex()//求复数的虚部
	{
		return imag;
	}
	double abscomplex()//求复数的绝对值
	{
		double t;
		t = real * real + imag * imag;
		return sqrt(t);
	}
}Data;
int main()
{
	Data.init(1.1, 2.2);
	cout << "real of complex Data=" << Data.realcomplex() << endl;
	cout << "image of complex Data=" << Data.imagcomplex() << endl;
	cout << "abs of complex Data=" << Data.abscomplex() << endl;
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/qq1480040000/p/12722917.html
今日推荐