Jump to content

Recommended Posts

Posted
#include<iostream>

#include<math.h>

using namespace std;

class complex

{

public:

//acum metodele si variabilele sunt de tip public, deci pot fi accesate din afara clasei

	float x, y;

	float modul(); //metoda modul, care returneaza modulul a doua numere complexe

	complex(float a, float b); 

	complex(float c=0); 

};

complex::complex(float a, float b)  //constructor de copiere

//nu mai avem la dispozitie constructorul implicit

//adica daca nu mai putem declara un obiect fara parametri

{

	x=a;

	y=b;

}

complex::complex(float c)

{

	x=1; 

	y=c;

}

float complex::modul()

{

	return sqrt(x*x+y*y);

}

int main()

{

	complex z1(3, 2), z2(4); //in cazul lui z2 se apeleaza constructorul cu un parametru, cel in care valoarea lui x este by default 1 iar y primeste valoarea 4

	cout<<z1.x<<" "<<z1.y<<" "<<z2.x<<" "<<z2.y<<" ";

	//avem voie deoarece x si y au fost declarati de tip public

	system("PAUSE");

	return 0;

}

  • 4 weeks later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.