实验任务4
#pragma once #include<iostream> #include<cmath> using namespace std; //定义Complex类 class Complex{ public: Complex(double r=0,double i=0):real{r},imag{i}{} Complex(const Complex &c) { real=c.real; imag=c.imag; } double get_real()const{return real;} double get_imag()const{return imag;} void show()const { if(imag>0) { cout<<real<<'+'<<imag<<'i'; } else if(imag<0) { cout<<real<<imag<<'i'; } else { cout<<real; } } void add(const Complex c) { real+=c.real; imag+=c.imag; } friend Complex add(const Complex c1,const Complex c2); friend bool is_equal(const Complex c1, const Complex c2); friend double abs(const Complex c1); private: double real,imag; }; Complex add(const Complex c1,const Complex c2) { Complex c3; c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return c3; } bool is_equal(const Complex c1,const Complex c2) { if(c1.real==c2.real&&c1.imag==c2.imag) { return true; } return false; } double abs(const Complex c) { return sqrt(c.real*c.real+c.imag*c.imag); }
#include "Complex.hpp" #include <iostream> // 类测试 void test() { using namespace std; Complex c1(-3, -4); const Complex c2(2.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(); }
实验任务5
#pragma once #include<iostream> #include<iomanip> #include<string> using namespace std; class User{ public: User(string s1,string s2="111111",string s3=""):name{s1},passwd{s2},email{s3}{n++;} ~User(){n--;} void change_passwd() { string s; cout<<"Enter old password: "; cin>>s; if(s==passwd) { cout<<"Enter new passwd: "; cin>>passwd; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again: "; cin>>s; if(s==passwd) { cout<<"Enter new passwd: "; cin>>passwd; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again: "; cin>>s; if(s==passwd) { cout<<"Enter new passwd: "; cin>>passwd; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please try after a while."<<endl; } } } } void set_email() { cout<<"Enter email address: "; cin>>email; cout<<"email is set successfully..."<<endl; } void print_info() { cout<<left<<setw(8)<<"name:"<<name<<endl; cout<<left<<setw(8)<<"passwd:"; for(auto i:passwd) { cout<<'*'; } cout<<endl; cout<<left<<setw(8)<<"email:"<<email<<endl; } private: string name,passwd,email; private: static int n; };
#include "User.hpp" #include <iostream> // 测试User类 void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Mary", "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::n=0; int main() { test(); }
标签:const,cout,对象,imag,passwd,Complex,实验,include From: https://www.cnblogs.com/fch20030128/p/16796801.html