complex.hpp
//#include "complex.h"
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
public:
Complex(int r = 0, int i = 0) : real((double)r), imag((double)i) {}
// Complex();
Complex(double r); //带一个参数
Complex(double r, double i); //带两个参数
~Complex() = default;
Complex(const Complex &a) //复制构造函数
{
real = a.real;
imag = a.imag;
}
double get_real() const //返回函数实部
{
return real;
}
double get_imag() const //返回函数虚部
{
return imag;
}
void show() const //输出复数
{
if (get_imag() > 0)
cout << get_real() << " + " << get_imag() << "i" << endl;
else if (get_imag() == 0)
cout << get_real() << endl;
else
cout << get_real() << " - " << -get_imag() << "i" << endl;
}
void add(const Complex &a) //用于把一个复数加到自身
{
real += a.real;
imag += a.imag;
}
friend Complex add(const Complex &a1, const Complex &a2); //友元函数:实现两个复数相加
friend bool is_equal(const Complex &a1, const Complex &a2); //友元函数,判断相等
friend double abs(const Complex &a); //友元函数,对复数进行取模运算
private:
double real;
double imag;
};
Complex::Complex(double r)
{
real = r;
imag = 0;
}
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex add(const Complex &a1, const Complex &a2)
{
Complex a3;
a3.real = a1.real + a2.real;
a3.imag = a1.imag + a2.imag;
return a3;
}
bool is_equal(const Complex &a1, const Complex &a2)
{
if ((a1.real == a2.real) && (a1.imag == a2.imag))
return true;
else
return false;
}
double abs(const Complex &a)
{
double sum;
sum = sqrt(a.real * a.real + a.imag * a.imag);
return sum;
}
task3.cpp
#include "complex.hpp"
#include <iostream>
// using namespace std;
int main()
{
using namespace std;
Complex c1(4, -5);
const Complex c2(3.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;
}
![]()
uesr.hpp
#include <iostream>
#include <string>
using namespace std;
class user
{
private:
string name;
string password;
string email;
static int n;
public:
user(string name1); //带一个参数
user(string nameq, string password1, string email1); //带三个参数
~user() = default;
void set_email()
{
string h;
cout << "Enter email address: ";
cin >> h;
email = h;
// cout << endl;
cout << "email is set successfully" << endl;
}
void change_passwd()
{
string p, newp; // p为旧密码,newp为新密码
cout << "Enter old password: ";
// cin >> p;
int t = 0;
for (int i = 1; i <= 3; i++)
{
cin >> p;
// int t = 0;
if (p != password)
{
t++;
if (t == 3)
{
cout << "password input error.Please try after a while." << endl;
;
break;
}
else
cout << "password input error. Please re-enter again: ";
}
else
{
cout << "Enter new password: ";
cin >> p;
cout << "new password is set successfully..." << endl;
break;
}
}
}
void print_info()
{
cout << "name: " << name << endl;
cout << "password: ******" << endl;
cout << "email: " << email << endl;
}
static void print_n()
{
cout << "there are " << n << " users." << endl;
}
};
int user::n = 0;
user::user(string name1)
{
name = name1;
password = "111111";
email = " ";
n++;
}
user::user(string name1, string password1, string email1)
{
name = name1;
password = password1;
email = email1;
n++;
}
task4.cpp
#include "user.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
user user1("Lisa", "11342", "lisa@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
user user2("Sumille");
user2.change_passwd();
user2.set_email();
user2.print_info();
user::print_n();
}
![]()