C++实验一
#ifndef __Complex_HPP__ #define __Complex_HPP__ #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(); Complex(double a); Complex(double a, double b); Complex(const Complex& c); ~Complex() = default; double get_real()const; double get_imag()const; void show()const; void add(const Complex& c); friend Complex add(const Complex& c1, const Complex& c2); friend bool is_equal(const Complex& c1, const Complex& c2); friend double abs(const Complex& c); private: double real, imag; }; Complex::Complex() :real(0.0), imag(0.0) {} Complex::Complex(double a) { real = a; imag = 0.0; } Complex::Complex(double a, double b) { real = a; imag = b; } Complex::Complex(const Complex& c) { real = c.get_real(); imag = c.get_imag(); } double Complex::get_real()const { return(real); } double Complex::get_imag()const { return (imag); } void Complex::show()const { double c, d; c = get_real(); d = get_imag(); if (d > 0) { cout << c << " + " << d << "i" << endl; } else if (d == 0) cout << c << endl; else cout << c << d << "i" << endl; } void Complex::add(const Complex& c) { real = real + c.get_real(); imag = imag + c.get_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; else return false; } double abs(const Complex& c) { double x, y; x = c.real; y = c.imag; return(sqrt(x * x + y * y)); } #endif // !__Complex_HPP__
#include "complex1.hpp" #include <iostream> int main() { using namespace std; Complex c1(12, -4); const Complex c2(6.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; }
#ifndef __User_HPP__ #define __User_HPP__ #include<iostream> #include<iomanip> #include<string> using namespace std; class User { public: User(string a, string b = "111111", string c = "") :name{ a }, password{ b }, email{ c } { n++; } ~User() = default; void set_email(); void change_passwd(); void print_info(); int get_n(); static void print_n() { cout << "there are " << n << "users" << endl; } private: string name; string password; string email; static int n; }; int User::n = 0; void User::set_email() { cout << "please enter email:"; cin >> email; cout << "the email address is set sucessfully."; } void User::change_passwd() { string text; int i=1; cout << "enter the old password:"; cin >> text; for(i;i<=2;i++) { if (text != password) { cout << "Password input error.Please renter it again:"; cin >> text; } else { cout << "enter the new password:"; cin >> text; cout << endl; cout << "new password is set sucessfully"<<endl; password = text; break; } if (i == 2) { cout << "Password input error.Please try after a while."<<endl; } } } void User::print_info() { cout << "name: " << name << endl; cout << "email: " << email << endl; cout << "password: ******" << endl; } int User::get_n() { return n; } #endif // !__User_HPP__
#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_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }