#pragma once
#include<iostream>
#include<math.h>
using namespace std;
class Complex
{
friend Complex add(const Complex& obj1, const Complex& obj2);
friend bool is_equal(const Complex& obj1, const Complex& obj2);
friend double abs(const Complex& obj);
public:
Complex(double q = 0, double p = 0) : real{q}, imag{p} {};
Complex(const Complex& obj);
double get_real()const;
double get_imag()const;
void show()const;
void add(const Complex& obj);
private:
double real;
double imag;
};
double Complex::get_real() const
{
return real;
}
double Complex::get_imag()const
{
return imag;
}
void Complex::show()const
{
if (imag)
cout << real << imag << "i";
else
cout << real << imag;
}
void Complex::add(const Complex& obj)
{
real += obj.real;
imag += obj.imag;
}
Complex::Complex(const Complex& obj)
{
real = obj.real;
imag = obj.imag;
}
Complex add(const Complex& obj1, const Complex& obj2)
{
Complex r;
r.real = obj1.real + obj2.real;
r.imag = obj1.imag + obj2.imag;
return r;
}
bool is_equal(const Complex& obj1, const Complex& obj2)
{
return obj1.real == obj2.real && obj2.imag == obj2.imag;
}
double abs(const Complex& obj)
{
double _real = obj.real;
double _imag = obj.imag;
return sqrt(_real * _real + _imag * _imag);
}
#include<iostream>
#include"Complex.h"
void test() {
using namespace std;
Complex c1(1,-2);
const Complex c2(3.0);
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;
}
int main() {
test();
}
![]()
#include<iostream>
#include<string>
using namespace std;
class User{
public:
User (string n, string p = "111111", string e = ""): name{n},passwd{p},email{e}
{
n++;
}
void set_email(){
cout << ("Enter email address: ");
cin >> email;
cout<<"email is set successfully..."<<endl;
}
void change_passwd(){
string Passwd;
cout << ("Enter old password: ");
cin >> Passwd;
for(int wrong = 1; wrong <= 2 && Passwd != passwd; wrong ++){
cout <<("password input error. Please re-enter again: ");
cin >> Passwd;
}
if(Passwd == passwd){
cout << "Enter new passwd: ";
cin >> passwd;
cout <<"new passwd is set successfully..."<<endl;
}
else
cout <<"password input error. Please try after a while."<<endl;
}
void print_info(){
cout << "name:" << name <<endl;
cout <<"passwd:";
for(int i = 0; passwd[i]; i ++)
cout << "*";
cout << "\n";
cout << "email: " << email <<endl;
}
static void print_n(){
cout << "there are " << n << " users."<<endl;
}
private:
string name, passwd, email;
static int n;
};
int User::n=0;
#include<iostream>
void test(){
using namespace std;
cout << "testing 1......\n";
User user1("Jonny", "92197", "xyz@hotmail.com");
user1.print_info();
cout << endl << "testing 2......\n\n";
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main(){
test();
}
![]()