实验任务4
1 #pragma once 2 3 #include<iostream> 4 #include<cmath> 5 6 using std::cout; 7 using std::endl; 8 9 class Complex{ 10 public: 11 //构造函数 12 Complex(float r = 0.0, float i = 0.0): real{r}, imag{i} {} 13 Complex(const Complex &obj): real{obj.real}, imag{obj.imag} {} 14 15 public: 16 //普通成员函数 17 float get_real() const { return real; } 18 float get_imag() const { return imag; } 19 void show() const; 20 void add(const Complex &obj); 21 22 //友源函数 23 friend Complex add(const Complex &c1, const Complex &c2) ; 24 friend bool is_equal(const Complex &c1, const Complex &c2); 25 friend double abs(const Complex &obj); 26 27 private: 28 float real; // 复数的实部 29 float imag; // 复数的虚部 30 }; 31 32 void Complex::show() const { 33 if(imag > 0){ 34 cout << real << " + " << imag << "i"; 35 } 36 else if(imag < 0){ 37 cout << real << " - " << abs(imag) << "i"; 38 } 39 else{ 40 cout << real ; 41 } 42 43 } 44 45 void Complex::add(const Complex &obj) { 46 real += obj.real; 47 imag += obj.imag; 48 } 49 50 Complex add(const Complex &c1, const Complex &c2) { 51 return Complex(c1.real+c2.real, c1.imag+c2.imag); 52 } 53 54 bool is_equal(const Complex &c1, const Complex &c2) { 55 if(c1.real == c2.real && c1.imag == c2.imag) return true; 56 return false; 57 } 58 59 double abs(const Complex &obj) { 60 return sqrt(obj.real * obj.real + obj.imag * obj.imag); 61 }Complex.hpp
1 #include "Complex.hpp" 2 #include <iostream> 3 4 // 类测试 5 void test() { 6 using namespace std; 7 8 Complex c1(3, -4); 9 const Complex c2(4.5); 10 Complex c3(c1); 11 12 cout << "c1 = "; 13 c1.show(); 14 cout << endl; 15 16 cout << "c2 = "; 17 c2.show(); 18 cout << endl; 19 cout << "c2.imag = " << c2.get_imag() << endl; 20 21 cout << "c3 = "; 22 c3.show(); 23 cout << endl; 24 25 cout << "abs(c1) = "; 26 cout << abs(c1) << endl; 27 28 cout << boolalpha; 29 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 30 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 31 32 Complex c4; 33 c4 = add(c1, c2); 34 cout << "c4 = c1 + c2 = "; 35 c4.show(); 36 cout << endl; 37 38 c1.add(c2); 39 cout << "c1 += c2, " << "c1 = "; 40 c1.show(); 41 cout << endl; 42 } 43 44 int main() { 45 test(); 46 }task4.cpp
测试结果截图:
实验任务5
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 using std::string; 10 11 class User{ 12 public: 13 User(string n, string p = "111111", string e = ""); 14 void set_email() ; 15 void change_passwd(); 16 void print_info(); 17 static void print_n() { cout << "there are " << count << " users" <<endl; } 18 19 public: 20 static int count; 21 22 private: 23 string name; 24 string passwd; 25 string email; 26 }; 27 28 int User::count = 0; 29 30 User::User(string n, string p /*= "111111"*/, string e /*= "111111"*/){ 31 name = n; 32 passwd = p; 33 email = e; 34 count++; 35 } 36 37 void User::set_email() { 38 string s1; 39 cout << "Enter email address: "; 40 cin >> s1; 41 email = s1; 42 cout << "email is set successfully..." << endl; 43 } 44 45 void User::change_passwd() { 46 cout << "Enter old password: "; 47 int times = 0; 48 while(times < 3){ 49 string s1; 50 cin >> s1; 51 if(s1 == passwd){ 52 cout << "Enter new passwd: "; 53 string s1; 54 cin >> s1; 55 passwd = s1; 56 cout << "new passwd is set successfully..." << endl; 57 break; 58 } 59 if(times < 2) 60 cout << "password input error. Please re-enter again:"; 61 times++; 62 } 63 if(times >= 3) cout << "passwd input error. Please try affter a while." << endl; 64 } 65 66 void User::print_info(){ 67 cout << "name: " << name << endl; 68 cout << "passwd: "; 69 string s1(passwd.length(), '*'); 70 cout << s1 << endl; 71 cout << "email: " << email << endl; 72 }User.hpp
1 #include "User.hpp" 2 #include <iostream> 3 4 // 测试User类 5 void test() { 6 using std::cout; 7 using std::endl; 8 9 cout << "testing 1......\n"; 10 User user1("Jonny", "92197", "[email protected]"); 11 user1.print_info(); 12 13 cout << endl 14 << "testing 2......\n\n"; 15 16 User user2("Leonard"); 17 user2.change_passwd(); 18 user2.set_email(); 19 user2.print_info(); 20 21 cout << endl; 22 User::print_n(); 23 } 24 25 int main() { 26 test(); 27 }task5.cpp
测试代码:
标签:std,const,cout,对象,void,Complex,实验,using From: https://www.cnblogs.com/gkd-gkd/p/16786400.html