实验一类与对象
任务三源码:
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> class Complex{ public: Complex(){}; Complex(double r,double i=0):real(r),imag(i){}; Complex(Complex &c); double get_real()const; double get_imag()const; void show()const; void add(const Complex &c); friend Complex add(Complex &c1,Complex &c2); friend bool is_equal(Complex &c1,const Complex &c2); friend double abs(Complex &c); private: double real,imag;}; Complex::Complex(Complex &c):real(c.real),imag(c.imag){}; double Complex::get_real()const { return real;} double Complex::get_imag()const { return imag;} void Complex::show()const { if(imag>=0) std::cout<<real<<"+"<<imag<<"i"; else if(imag<0) std::cout<<real<<imag<<"i"; } void Complex::add(const Complex &c) { real+=c.real; imag+=c.imag;} Complex add(Complex &c1,const Complex &c2) { return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); } bool is_equal(Complex &c1,const Complex &c2) { if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false;} double abs(Complex &c) { return sqrt(c.real*c.real+c.imag*c.imag);} #endif
#include"Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(3,-5); const Complex c2(1.2); 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; }
任务四源码:
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<string> using namespace std; class User{ public: User(string n,string p="111111",string e=""); void set_email(); void change_passd(); void print_info(); static void print_n(); private: string name,password,email; static int count; }; int User::count=0; User::User(string n,string p,string e):name(n),password(p),email(e){count++;}; void User::print_n() { cout<<"there are "<<count<<" users";} void User::print_info() { cout<<"name:"<<name<<endl <<"password:******"<<endl <<"email:"<<email<<endl; } void User::change_passd() { string passwrd; cout<<"Enter old password:"; cin>>passwrd; for(int i=0;i<3;i++) if(passwrd==password) {cout<<"Enter new password:"; cin>>passwrd; password=passwrd; cout<<"new passwd is set successfully..."<<endl; break;} else {if(i==2) cout<<"password intput error.Please try after a while."<<endl; else {cout<<"password intput error.Please re-enter again:"; cin>>passwrd;} } } void User::set_email() { cout<<"Enter email address:"; cin>>email; cout<<"email is set successfully..."<<endl;} #endif
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passd(); user2.set_email(); user2.print_info(); User::print_n(); }