实验一 类与对象
实验任务3 Complex类的自我实现
简要说明:不使用C++标准库,自行设计并实现一个复数类Complex,并满足简单的使用需求。
程序由 Complex.hpp 与 task3.cpp 两部分构成,分别完成复数类定义及实现,主程序展示的功能。
Complex.hpp代码如下:
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
class Complex {
public:
Complex(double r = 0, double i = 0) : real(r), image(i) {}
Complex(const Complex &c) : real(c.real), image(c.image) {}
double get_real();
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;
double image;
};
double Complex::get_real() { return real; }
double Complex::get_imag() const { return image; }
void Complex::show() const {
if(real == 0)
cout << image << "i" << endl;
if(image == 0)
cout << real << endl;
if(image < 0)
cout << real << " - " << -image << "i" << endl;
}
void Complex::add(const Complex &c) {
real += c.real;
image += c.image;
}
Complex add(const Complex& c1, const Complex& c2) {
Complex r;
r.real = c1.real + c2.real;
r.image = c1.image + c2.image;
return r;
}
bool is_equal(const Complex& c1, const Complex& c2) {
if (c1.real == c2.real && c1.image == c2.image) {
return true;
}
else {
return false;
}
}
double abs(const Complex& c) {
double ret = sqrt(c.real * c.real + c.image * c.image);
return ret;
}
#endif // !COMPLEX_HPP
主程序 task3.cpp代码如下:
#include "Complex.hpp"
#include <iostream>
int main()
{
using namespace std;
Complex c1(1, -2);
const Complex c2(6.7);
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类的设计与实现
简要说明:设计并实现一个用户类User, 满足基础的创建用户,修改密码与邮箱的功能,并进行简单的合法校验。
程序由 User.hpp 与 task4.cpp 两部分构成,分别完成用户类定义及实现,主程序展示的功能。
User.hpp 代码如下:
#ifndef USER_HPP
#define USER_HPP
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
class User {
public:
User(string name, string pwd="111111", string email="") :
name(name), password(pwd), email(email)
{
cnt++;
}
void set_email();
void change_passwd();
void print_info();
static void print_n();
private:
string name;
string password;
string email;
static int cnt;
};
int User::cnt = 0;
void User::set_email() {
string newemail;
cout << "Enter email address: ";
cin >> newemail;
while (newemail.find('@') == -1) {
cout << "Wrong! There must be an '@' in your email address!\nPlease re_enter again: ";
cin >> newemail;
}
email = newemail;
cout << "email is set sucessfully..." << endl;
}
void User::change_passwd() {
string oldpwd;
string newpwd;
int t = 0;
cout << "Enter old password: ";
cin >> oldpwd;
while (true) {
if (oldpwd == password) {
cout << "Enter new password: ";
cin >> newpwd;
while (newpwd.length() != 6) {
cout << "The password can only contain 6 characters!\nPlease re_enter again: ";
cin >> newpwd;
}
password = newpwd;
cout << "new password is set successfully..." << endl;
break;
}
else {
if (t >= 2) {
cout << "Password input error. Please try after a while." << endl;
break;
}
cout << "Password input error. Please re_enter again: ";
cin >> oldpwd;
t++;
}
}
}
void User::print_info() {
cout << "name:\t" << name << endl;
cout << "passwd:\t" << "*****" << endl;
cout << "email:\t" << email << endl;
}
void User::print_n() {
cout << "there are " << cnt << " users." << endl;
}
#endif
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号