【实验一】类与对象

文章目录

1、任务一

不使用C++标准库,自行设计并实现一个复数类Complex,使其满足如下要求:

  • 数据成员
    用来表示复数的实部real和虚部imag,实部、虚部,均为小数形式。
  • 函数成员
    • 构造函数
      支持以下方式定义复数对象:
      Complex c1; // 不带参数
      Complex c2(3); // 只有一个参数,相当于3 + 0i
      Complex c3(3, 4); // 两个参数,相当于3 + 4i
      Complex c4(c3); // 用c3构造c4
      
    • 成员函数
      • get_real() 返回复数实部
      • get_imag() 返回复数虚部
      • show() 用于输出复数。要求以 3 + 4i , 3 - 4i 这样的形式输出
      • add() 用于把一个复数加到自身,比如 c1.add(c2) ,相当于 c1 += c2
  • 友元函数
    • add() 用于实现两个复数相加,返回复数。比如 c3 = add(c1, c2);
    • is_equal() 用于判断两个复数是否相等,返回true/false。比如 is_equal(c1, c2)
    • abs() 用于对复数进行取模运算
      将类Complex的定义及友元函数实现,单独保存在文件Complex.hpp中。
      使用ComplexTest.cpp中的代码,测试类Complex的各项接口是否正确。

Complex.hpp

#include <iostream>
#include <cmath>

class Complex
{
private:
    double a;
    double b;

public:
    Complex();
    Complex(double a);
    Complex(double a, double b);
    Complex(const Complex &complex_temp);
    ~Complex();

    double get_real() const;
    double get_imag() const;
    void show() const ;
    void add(Complex complex_temp);
    Complex add(const Complex complex_temp) const;
    bool equal(const Complex complex_temp) const;
    double abs(const Complex complex1) const;

    friend Complex add(const Complex complex1, const Complex complex2);
    friend bool is_equal(const Complex complex1, const Complex complex2);
    friend double abs(const Complex complex_temp);
};

Complex::Complex()
{
}
Complex::Complex(double a) : a{a}
{
}
Complex::Complex(double a, double b) : a{a}, b{b}
{
}
Complex::Complex(const Complex &complex_temp) : a{complex_temp.a}, b{complex_temp.b}
{
}

Complex::~Complex()
{
}

double Complex::get_real() const
{
    return a;
}

double Complex::get_imag() const
{
    return b;
}

void Complex::show() const
{
    std::cout << a << " + " << b << "i";
}

void Complex::add(Complex complex_temp)
{
    a += complex_temp.a;
    b += complex_temp.b;
}

Complex Complex::add(const Complex complex_temp) const
{
    return Complex(a + complex_temp.get_real(), b + complex_temp.get_imag());
}

bool Complex::equal(const Complex complex_temp) const
{
    if(a == complex_temp.get_real() && b == complex_temp.get_imag()){
        return true;
    }
    return false;
}

double Complex::abs(const Complex complex_temp) const
{
    return sqrt(pow(complex_temp.get_imag(), 2) + pow(complex_temp.get_real(), 2));
}


Complex add(const Complex complex1, const Complex complex2);
bool is_equal(const Complex complex1, const Complex complex2);
double abs(const Complex complex_temp);

Complex add(const Complex complex1, const Complex complex2){
    complex1.add(complex2);
    return complex1;
}
bool is_equal(const Complex complex1, const Complex complex2){
    return complex1.equal(complex2);
}
double abs(const Complex complex_temp){
    return(complex_temp.abs(complex_temp));
}

ComplexTest.cpp

#include "Complex.hpp"
#include <iostream>
#include <cmath>

using namespace std;


int main(){
    using namespace std;
    Complex c1(5, -4);
    const Complex c2(5.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;
    system("pause");
    return 0;
}

运行结果:
【实验一】类与对象

2、任务二

设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:

  • 数据成员
    每个用户有用户名(name)、密码(passwd)、联系邮箱(email)三个属性。
    还有一个类属性,用于记录用户总数n。
  • 函数成员
    • 构造函数
    User u1("Mary"); // 密码和邮箱,使用默认设置
    User u2("John", "112233", "xyz@gmail.com")
    
    如果定义用户对象时未设置密码和邮箱,密码默认为6个1,联系邮箱默认为空串。
    • 成员函数
      • set_email()
        设置邮箱。提示用户从键盘输入邮箱。
      • change_passwd()
        修改密码。修改密码前,要求先输入旧的密码,验证无误后,才允许修改;如果输入旧
        密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
      • print_info()
        打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。

User类的定义和实现,保存在文件User.hpp中:

#include <iostream>
#include <string>

using namespace std;

class User
{
private:
    string name;
    string passwd;
    string email;
    static int count;

public:
    User();
    User(string name);
    User(string name, string passwd, string email);
    ~User();

    static void print_count();

    void set_email();
    void change_passwd();
    void print_info();
};

int User::count = 0;
void User::print_count(){
    cout << "\nthere are " << count << "users\n";
}

User::User()
{
    this->name = "无";
    this->passwd = "666666";
    this->email = "666666@qq.com";
    this->count++;
}
User::User(string name) : name{name}
{
    this->passwd = "666666";
    this->email = "666666@qq.com";
    this->count++;
}
User::User(string name, string passwd, string email) : name{name}, passwd{passwd}, email{email}
{

    this->count++;
}

User::~User()
{

    this->count++;
}

void User::set_email()
{
    string email_temp;
    string passwd_temp;
    cout << "\nDear " << name << "~" << "\nCheck password: ";
    cin >> passwd_temp;
    while (passwd_temp != passwd)
    {
        cout << "password input error. Please re-enter again: ";
        cin >> passwd_temp;
    }
    cout << "Enter email address: ";
    cin >> email_temp;
    this->email = email_temp;
    cout << "email is set successfully...\n";
}

void User::change_passwd()
{
    string passwd_temp;
    cout << "\nDear " << name << "~" << "\nEnter old password: ";
    cin >> passwd_temp;
    while (passwd_temp != passwd)
    {
        cout << "password input error. Please re-enter again: ";
        cin >> passwd_temp;
    }
    cout << "Enter new passwd: ";
    cin >> passwd_temp;
    this->passwd = passwd_temp;
    cout << "passwd changed successfully\n";
}

void User::print_info()
{
    cout << "\n用户名:" << name << "\n密码:******" << "\n邮箱:" << email << endl;
}

User类的使用和测试,保存在文件UserTest.cpp中:

#include "Complex.hpp"
#include <iostream>
#include <cmath>

using namespace std;


int main(){
    using namespace std;
    Complex c1(5, -4);
    const Complex c2(5.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;
    system("pause");
    return 0;
}

运行结果:
【实验一】类与对象

实验总结

const的用法,以及构造函数、初始化列表、友元函数。
加深对面向对象中封装的理解

上一篇:实验一 类与对象


下一篇:《python数字货币量化交易》学习笔记 第四部分别有洞天篇 4.1 择时策略实盘与优化