实验1
Complex.hpp文件源码
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include<bits/stdc++.h> using namespace std; class Complex{ public: Complex(); Complex(double x); Complex(double x,double y); Complex(Complex &p); ~Complex() {} double get_real() const; double get_imag() const; void show() const; void add(Complex &c2); void add(const Complex &c2); friend Complex add(Complex &c1,Complex &c2); friend Complex add(Complex &c1,const Complex &c2); friend bool is_equal(Complex &c1,Complex &c2); friend bool is_equal(Complex &c1,const Complex &c2); friend double abs(Complex &c1); private: double real; double image; }; Complex::Complex():real{0},image{0} { } Complex::Complex(double x):real{x},image{0} { } Complex::Complex(double x,double y):real{x},image{y} { } Complex::Complex(Complex &p):real{p.real},image{p.image} { } double Complex::get_real() const { return real; } double Complex::get_imag() const { return image; } void Complex::show() const { cout << real; if(image>0) { cout << " + " << image << "i"; } else if(image<0) { double x = -image; cout << " - " << x << "i"; } } void Complex::add(Complex &c2) { Complex c1; c1.real += c2.real ; c1.image += c2.image ; } void Complex::add(const Complex &c2) { real += c2.real ; image += c2.image ; } Complex add(Complex &c1,Complex &c2) { Complex c3; c3.real = c1.real + c2.real; c3.image = c1.image + c2.image; return c3; } Complex add(Complex &c1,const Complex &c2) { Complex c3; c3.real = c1.real + c2.real; c3.image = c1.image + c2.image; return c3; } bool is_equal(Complex &c1,Complex &c2) { return (c1.real == c2.real && c1.image == c2.image); } bool is_equal(Complex &c1,const Complex &c2) { return (c1.real == c2.real && c1.image == c2.image); } double abs(Complex &c1) { return sqrt(c1.real*c1.real+c1.image*c1.image); } #endif
main.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6,-8); const Complex c2(9); 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; }

task4
User.hpp
#ifndef USER_HPP #define USER_HPP #include<bits/stdc++.h> 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() { cout << "Enter email address:"; cin >> email; while(email.find('@') == -1 ) { cout << "Warning:@ should be included.Please try again." << endl; cout << "Enter email address:"; cin >> email; } cout << "email is set successfully..." << endl; } void change_passwd() { string d,e; int cnt=1; cout << "Enter old password:"; cin >> d; while(cnt!=3) { if(d==password) { cout << "Enter new password:"; cin >> e; while(e.length() != 6) { cout << "Warning:length of the password should be six.Please try again." << endl; cout << "Enter new password:"; cin >> e; } password = e; cout << "new passwd is set successfully..." << endl; break; } else { cout << "password input error. Please re-enter again:"; cin >> d; cnt++; continue; } } if(cnt>=3) { cout << "password input error. Please try after a while." << endl; } } void print_info() { cout << left << setw(10) << "name:" << name << endl; cout << left << setw(10) << "passwd:" << "******" << endl; cout << left << setw(10) << "email:" << email << endl; } static void print_n() { cout << "there are " << n << " users." << endl; } private: string name; string password; string email; static int n; }; int User::n=0; #endif
main.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Ronin","982576","982576@qq.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Donald"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }


通过本次实验,我了解了string类的对象声明及其函数find、length等。
本次实验,在进行User.hpp编写时,因为中途曾进行编译,生成了一个.gch文件,导致后续编译时一直报错。
上网查了以后,明白那是gcc为了优化编译速度,如果一个文件里面包含的 .h 比较多的话,
gcc会先将.h头文件预编译成为 .h.gch文件,以便下次编译时能够更快,将会将.h.gch文件自动载入。
但该文件有一个很不好的地方就是,
如果我们修改了文件头的 .h 文件, gcc仍旧会编译原来的 .h.gch 文件, 不会载入新修改的文件头,很容易造成错误。
对于此的解决方案,我选择了直接删除.gch文件(更好的方案是选择全部重新编译)
而百度得知只需要将 Makefile文件 的 clean 目标项 修改为 rm -f *.o *.h.gch , 然后在重新编译就好.....

浙公网安备 33010602011771号