#pragma once #include <iostream> #include<string> #include<iomanip> #include<cmath> class Complex { public: Complex() { real = 0; imag = 0; } Complex(double x) {real = x; imag = 0; } Complex(double x, double y){real = x; imag = y; } Complex(Complex& obj) { real = obj.real; imag = obj.imag; } double get_real()const { return real; } double get_imag()const { return imag; } Complex add(const Complex& obj) { Complex c; c.real = real + obj.real; c.imag = imag + obj.imag; return c; }; void show() const { if (imag) { std::cout << real << imag << "i" ; } else { std::cout << real; } } friend Complex add(const Complex &a,const Complex &b); friend bool is_equal(const Complex &a,const Complex &b); friend double abs(const Complex& a); private: double imag, real;
}; Complex add(const Complex &a, const Complex &b) { Complex c; c.real = a.real + b.real; c.imag = a.real + a.imag; return c; } bool is_equal(const Complex& a, const Complex& b) { return (a.real == b.real) && (a.imag == b.imag); } double abs(const Complex& a) { return sqrt(a.real * a.real + a.imag * a.imag); }
#include"Complex.hpp" #include<iostream> void test() { using namespace std; Complex c1(33, -4); const Complex c2(5.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; } int main() { test(); }
#pragma once #include <iostream> #include<string> using namespace std; class User { public: User(string a) { name = a; password = "111111"; email = " "; } User(string a, string b, string c) { name = a; password = b; email = c; } void set_email(); void change_password(); void print_info(); static int print_n(); private: string name, password, email; }; void User::change_password() { cout << "Enter old passord:"; int cnt = 3; while (cnt--) { string a; cin >> a; if (a != password) { cout << (cnt == 0 ? "password input error.Please try after a while\n" : "password input error.Please re-enter again:") ; } else { cout << "Enter new password:"; cin >> password; cout << "new password is set successfully..." << endl; break; } } } void User::set_email() { cout << "Enter email adress:"; cin >> email; cout << "email is set succesfully..." << endl; } void User::print_info() { int n = password.length(); cout << "name: " << name << endl; cout << "password: "; for (int i = 0; i < n; i++) { cout << "*"; } cout << endl; cout << "email: " << email << endl; } int User::print_n() { static int x = 1; x++; cout << "there are " << x << " users." << endl; return x; }
#include"User.hpp" #include<iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_password(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
标签:real,const,cout,imag,Complex,实验,include From: https://www.cnblogs.com/jh025/p/16800182.html