#pragma once #include<iostream> #include <cmath> using std::cout; using std::endl; class Complex { public: Complex(double r = 0, double i = 0):real(r),imag(i){}; Complex(const Complex& r):real(r.real),imag{r.imag}{}; double get_real() const{ return real; }; double get_imag() const{ return imag; }; void add(const Complex& r) { real += r.real; imag += r.imag; }; void show() const; friend Complex add(const Complex& r1,const Complex& r2); friend bool is_equal(const Complex& r1,const Complex& r2); friend double abs(const Complex& r1); private: double real, imag; }; void Complex::show()const { cout << real; if (imag > 0)cout << "+" << imag << "i"<<endl; else if(imag<0)cout << imag <<"i"<< endl; } Complex add(const Complex &r1,const Complex &r2) { Complex r3; r3.real = r1.real + r2.real; r3.imag = r1.imag + r2.imag; return r3; } bool is_equal(const Complex& r1, const Complex& r2) { if (r1.real == r2.real && r1.imag == r2.imag) return true; else return false; } double abs(const Complex& r1) { double x; x= sqrt(r1.real * r1.real + r1.imag * r1.imag); return x; }
修改后:
#pragma once
#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::endl;
using std::cin;
class User
{
public:
User(string n, string p = "111111" , string e = "") :
name{ n }, password{ p }, email{ e } {total++;};
void set_email();
void change_passwd();
void print_info();
void static print_n();
static int total;
private:
string name;
string password;
string email;
};
int User::total = 0;
void User::set_email()
{
cout << "Enter email address:";
cin >> email;
cout << endl << "email is set successfully";
}
void User::change_passwd()
{
int x = 0;
cout << "Enter old password:";
cin >> password;
x++;
while (password != "111111" && x < 3)
{
cout << "password input error.please re-enter again:";
cin >> password;
cout << endl;
x++;
}
if (x < 3)
{
cout << "Enter new passd:";
cin >> password;
cout << endl;
cout << "new passwd is set sucessfully..." << endl;
}
else
{
password = "111111";
cout << "password input error.please try after a while." << endl;
}
}
void User::print_info()
{
int m = size(password);
cout << "name:" << name << endl;
cout << "passwd:";
for (int i = 0; i < m; i++)
{
cout << "*";
}
cout << endl;
cout << "email:" << email << endl;
}
void User::print_n()
{
cout << "there are" << total << "users";
}
标签:real,test2,const,cout,void,Complex,imag From: https://www.cnblogs.com/lzygxz/p/16795087.html