实验3:
头文件“Complex.hpp”
#include <iostream>
using namespace std;
#include <cmath>
class Complex
{
public:
Complex(double a = 0, double b = 0) : real(a), imag(b) {}
Complex(const Complex& c) : real(c.real), imag(c.imag) {}
~Complex() = default;
double get_real() const { return real; }
double get_imag() const { return imag; }
void show() const { cout << real << imag << 'i'; }
void add(Complex c) { real += c.real; imag += c.imag; }
friend Complex add(Complex a, Complex b);
friend bool is_equal(Complex a, Complex b);
friend double abs(Complex c);
private:
double real;
double imag;
};
Complex add(Complex a, Complex b)
{
Complex c(0, 0);
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
bool is_equal(Complex a, Complex b)
{
if (a.real == b.real && a.imag == b.imag)
return true;
else
return false;
}
double abs(Complex c)
{
return static_cast<double>(sqrt(c.real * c.real + c.imag * c.imag));
}
task3.cpp代码:
#include <iostream>
using namespace std;
#include "Complex.hpp"
int main()
{
using namespace std;
Complex c1(2, 6);
const Complex c2(2.6);
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;
}

实验4:
头文件“User.hpp”
#include <iostream>
using namespace std;
#include <string>
class User
{
public:
User(string a, string b = "111111", string c = "") :name(a), passwd(b), email(c) { n++; }
~User() = default;
void set_email()
{
cout << "Enter email address: ";
cin >> email;
cout << "email is set successfully...\n";
}
void change_passwd()
{
string temp;
cout << "Enter old password: ";
cin >> temp;
if (temp == passwd)
{
cout << "Enter new password: ";
cin >> passwd;
cout << "new password is set successfully...\n";
}
else
{
int i = 1;
while (temp != passwd && i <= 2)
{
cout << "password input error. Please re-enter again:";
cin >> temp;
i++;
}
if (temp == passwd)
{
cout << "Enter new password: ";
cin >> passwd;
cout << "new password is set successfully...\n";
}
else
cout << "password input error. Please try after a while.\n";
}
}
void print_info()
{
cout << "name:\t" << name << endl;
cout << "passwd:\t" << "******" << endl;
cout << "email:\t" << email << endl;
}
static void print_n()
{
cout << n << endl;
}
private:
string name, passwd, email;
static int n;
};
int User::n = 0;
task4.cpp
#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();
}


实验总结:
在这次实验中,我深刻意识到类以及类内函数的调用的问题的细节,在几个地方一直出现问题,并不断尝试改进,包括翻阅书籍,直到最后的成功实现。最大的问题是刚开始没有意识到定义的函数重复不明,导致一直报错。
浙公网安备 33010602011771号