task 4
Complex.h
#pragma once #include<iostream> 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 _real = 0, double _image = 0); Complex(const Complex& obj); double get_real()const; double get_imag()const; void show()const; void add(const Complex& obj); private: double real; double image; };
Complex.cpp
#include"Complex.h" double Complex::get_real() const { return real; } double Complex::get_imag()const { return image; } void Complex::show()const { if (image) cout << real << image << "i"; else cout << real << image; } void Complex::add(const Complex& obj) { real += obj.real; image += obj.image; } Complex::Complex(double _real, double _image) { real = _real; image = _image; } Complex::Complex(const Complex& obj) { real = obj.real; image = obj.image; } Complex add(const Complex& obj1, const Complex& obj2) { Complex ret; ret.real = obj1.real + obj2.real; ret.image = obj1.image + obj2.image; return ret; } bool is_equal(const Complex& obj1, const Complex& obj2) { return obj1.real == obj2.real && obj2.image == obj2.image; } double abs(const Complex& obj) { double _real = obj.real; double _image = obj.image; return sqrt(_real * _real + _image * _image); }
test4.cpp
#include<iostream> #include"Complex.h" using namespace std; void test() { 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; } int main() { test(); }
task 5
user.h
#pragma once #include<iostream> #include<string> using namespace std; class User { public: User(const string& _name, const string& _passwd="111111", const string& _email=""); void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name; string passwd; string email; static int count; };
user.cpp
#include"user.h" User::User(const string& _name, const string& _passwd, const string& _email) { name = _name; passwd = _passwd; email = _email; count++; } void User::set_email() { cout << "Enter email address: "; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { cout << "Enter old passwd: "; string _passwd; int input_count = 0; while (true) { cin >> _passwd; input_count++; if (_passwd != passwd) cout << "passwd input error. "; else { break; } if (input_count == 3) { cout << "Please try after a while"<<endl; break; } else { cout << "Please re-enter again: "; } input_count++; } } void User::print_info() { cout << "name: " << name << endl; cout << "passwd: "; int passwd_len = passwd.size(); for (int i = 1; i <= passwd_len; i++) cout << "*"; cout << endl; cout << "email: " << email << endl; } void User::print_n() { cout << "there are " << count << " users."; }
test5.cpp
#include "User.h" #include <iostream> // 测试User类 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_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int User::count = 0; int main() { test(); }
标签:const,string,passwd,void,Complex,实验,include From: https://www.cnblogs.com/xelfin/p/16785528.html