任务四
Complex.hpp
#pragma once #include <iostream> #include <cmath> using std::cout; using std::endl; class Complex { public: Complex(double x0, double y); Complex(const Complex& obj) : x{ obj.x }, y{ obj.y } {} double get_real()const { return x; } double get_imag()const { return y; } void show()const; void add( Complex obj); friend Complex add( Complex c1, Complex c2); friend bool is_equal( Complex c1, Complex c2); friend double abs( Complex obj); private: double x, y; }; Complex::Complex(double x0=0.0, double y0=0.0) { x = x0; y = y0; } void Complex::show() const { if (y > 0) { cout << x << " + " << y << "i"; } else if (y < 0) { cout << x << " - " << abs(y) << "i"; } else { cout << x; } } void Complex::add( Complex obj) { x += obj.x; y += obj.y; } Complex add( Complex c1, Complex c2) { Complex c3; c3.x = c1.x + c2.x; c3.y = c1.y + c2.y; return c3; } bool is_equal( Complex c1, Complex c2) { if (c1.x == c2.x && c1.y == c2.y) return true; else return false; } double abs( Complex obj) { double ab; ab = sqrt(obj.x * obj.x + obj.y * obj.y); return ab; }
test.cpp
#include "Complex.hpp" #include <iostream> void test() { using namespace std; Complex c1(1, 4); const Complex c2(1.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(); }
任务五
User.hpp
#pragma once #include <iostream> #include <string> #include <algorithm> using namespace std; class User { public: User(string name0, string password = "111111" , string email0 = "") :name{name0}, passwd{password}, email{email0}{n++;} void set_email(); void change_passwd(); void print_info(); static void print_n(){ cout << "there are " << n << "users"; } private: string name, passwd, email; static int n; }; int User::n = 0; void User::set_email() { cout << "Enter email address :" ; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { cout << "Enter old passwd :"; int i; for (i = 0;i < 3;i++) { string b; cin >> b; if (b.compare(passwd) != 0) { if (i == 2) cout << "passwd input error.please try after a while."<<endl; else cout << "passwd input error.please re-enter again: "; } else { cout << "Enter new passwd: "; cin >> passwd; cout << "new passwd is set successfully..."; break; } } } void User::print_info() { cout << "name: " << name << endl << "passwd: "; string s1(passwd.length(), '*'); cout << s1 << endl; cout << "email: " << email << endl; }
test.cpp
#include "User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing1......\n"; User user1("Jonney", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
实验总结:对类已经有了较完整清晰的认知,并可以上手运用。但是细节上的问题还需要时间去解决。
标签:const,cout,double,void,Complex,实验,include From: https://www.cnblogs.com/shmily-cwh/p/16796246.html