#include<iostream>
#include<cmath>
class Complex {
public:
Complex(double i = 0, double j = 0) :a{ i }, b{ j }{}
Complex(Complex const& p) :a{ p.a }, b{ p.b }{}
~Complex() {}
double get_real()const { return a; }
double get_imag() const { return b; }
void show() const {
std::cout << a;
if (b > 0) {
std::cout << '+' << b << 'i';
}
if (b < 0) {
std::cout << b << 'i';
}
}
void add(Complex c) {
a += c.a;
b += c.b;
}
friend Complex add(Complex aa, Complex bb);
friend bool is_equal(Complex a, Complex b);
friend double abs(Complex c);
private:
double a, b;
};
Complex add(Complex aa, Complex bb) {
Complex i;
i.a = aa.a + bb.a;
i.b = aa.b + bb.b;
return i;
}
bool is_equal(Complex a, Complex b) {
if (a.a != b.a)return false;
else if (a.b != b.b)return false;
else return true;
}
double abs(Complex c) {
return sqrt(c.a * c.a + c.b * c.b);
}
int main()
{
using namespace std;
Complex c1(3,-4);
const Complex c2(4.5);
Complex c3(c1);
cout<< "c1 = ";
c1.show();
cout << endl;
cout << "c2 = ";
c2.show();
cout << endl;
cout << "c2.imag = " << c2.get_imag() << endl;
cout << "c3 = ";
c3.show();
cout << endl;
cout << "abs(c1) = ";
cout << abs(c1) << endl;
cout << boolalpha;
cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
Complex c4;
c4 = add(c1, c2);
cout << "c4 = c1 + c2 = ";
c4.show();
cout << endl;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}
![]()
#pragma once
#ifndef USER_h
#define USER_h
#include<iostream>
#include<string>
using namespace std;
class User {
public:
User(string n, string p = "111111", string e = "") :name(n), passwd(p), email(e) { ++count; };
void set_email() {
cout << "Enter email address:";
cin >> email;
cout << endl << "emai is set succesfully..." << endl;
}
static void print_n() { cout << "there are " << count << " users."; }
void change_passwd();
void print_info();
~User(){}
private:
std::string name, passwd, email;
static int count;
};
int User::count = 0;
void User::change_passwd() {
cout << "Enter old passwd:";
string a;
cin >> a;
for (int n = 1; n <= 3; ++n) {
if (a != passwd) {
if (n == 3) {
cout << "password input error.Please try after a while."<<endl;
return;
}
else {
cout << "password input error.Please re-enter again:";
cin >> a;
}
}
else {
cout << "Please change password:";
cin >>passwd;
}
}
}
void User::print_info() {
cout << "name:" << name<<endl;
cout << "passwd:******" << endl;
cout << "email:" << email << endl;
}
#endif // !USER_h
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Jonny", "92197", "xyz@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}
![]()