实验任务四
main.cpp
#include"Complex.hpp" #include<iostream> using std::cout; using std::endl; 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(); }
Complex.hpp
#pragma once #include <iostream> #include<cmath> using std::cout; using std::endl; class Complex{ public: Complex(float x=0.0,float y=0.0):real{x},imag{y}{ }; Complex(Complex& c):real{c.real},imag{c.imag}{ }; float get_real()const { return real; } float get_imag()const {return imag; } void show() const { if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag<0) cout<<real<<imag<<"i"; if(imag==0) cout<<real; else if(real==0) cout<<imag<<"i"; } void add(const Complex& c){ real=real+c.get_real(); imag=imag+c.get_imag(); } friend Complex add(Complex& c1,const Complex& c2){ Complex c; c.real=c1.get_real()+c2.get_real(); c.imag=c1.get_imag()+c2.get_imag(); return c; } friend bool is_equal(Complex& c1,const Complex& c2){ if(c1.get_real()==c1.get_real()&&c1.get_imag()==c2.get_imag()){ return true; } else{ return false; } return true; } friend float abs(Complex& c){ return sqrt(c.get_real()*c.get_real()+c.get_imag()*c.get_imag()); } private: float real,imag; };
运行结果
试验任务五
main.cpp
#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_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
User.hpp
#include<bits/stdc++.h> using std::cin; using std::cout; using namespace std; class User{ public: User(string a,string b="111111",string c=""):name{a},passwd{b},email{c}{count++;}; void set_email(){ cout<<"Enter email address: "; cin>>email; } void change_passwd(){ int cnt=0; cout<<"Enter old password: "; string pswd,newwd; cin>>pswd; while(pswd!=passwd&&cnt!=3){ cout<<"password input error.Please re-enter again: "; cin>>pswd; cnt++; } if(cnt==3){ cout<<"password input error.Please try again later.\n"; return ; } else{ cout<<"Enter new passwd: "; cin>>newwd; passwd = newwd; cout<<"new passwd is set successfully...\n"; } } void print_info()const { cout<<"name: "<<name<<endl; cout<<"passwd: "; for(int i=0;i<=passwd.length();i++) cout<<"*"; cout<<endl; cout<<"email: "<<email<<endl; } void static print_n(){ cout<<"there are "<<count<<" users"<<endl;} private: string name,passwd,email; static int count; }; int User::count=0;
运行结果
任务五那个string s1(length,'*') 测试编译报错,不得已换了个写法
后面再研究研究这个string类型具体用法
const的接口函数对应不熟练 多看 多练。
标签:std,cout,imag,Complex,实验,using,include From: https://www.cnblogs.com/nitendoblog/p/16789679.html