首页 > 其他分享 >实验二

实验二

时间:2022-10-19 12:35:15浏览次数:40  
标签:构造函数 cout Complex 实验 User include string

task.4

#include<iostream>
#include<math.h>
using namespace std;
class Complex {
private:
    float real, imag;
public:
    Complex(float r, float i);//带两个参数的构造函数
    Complex();//默认构造函数
    Complex(float r);//带一个参数的构造函数
    Complex(Complex &c);//复制构造函数
    float get_real()const { return real; }
    float get_imag()const { return imag; }
    void show()const {
        cout << real;
        if (imag > 0)cout << "+";
        if(imag!=0)cout << imag << "i";
    }
    void add(const Complex &c) {
        real += c.real;
        imag += c.imag;
    }
    friend Complex add(const Complex &c1,const Complex &c2);//youyuan
    friend bool is_equal(const Complex &c1,const Complex &c2);
    friend float abs(const Complex &c);
};

Complex::Complex(float r, float i) :real(r), imag(i) {}//带两个参数的构造函数的实现
Complex::Complex(float r) : real(r), imag(0) {}//带一个参数的构造函数的实现
Complex::Complex() : real(0), imag(0) {}//默认构造函数的实现
Complex::Complex(Complex &c) {
    real = c.real;
    imag = c.imag;
}//复制构造函数的实现
Complex add(const Complex &c1, const Complex &c2) {
    float real = c1.real + c2.real;
    float imag = c1.imag + c2.imag;
    Complex a(real, imag);
    return a;
}
bool is_equal(const Complex &c1, const Complex &c2) {
    bool a = false;
    if (c1.real == c2.real&&c1.imag == c2.imag)a = true;
    return a;
}
float abs(const Complex &c) {
    return sqrt(c.real*c.real + c.imag*c.imag);
}

task.4.cpp

#include "Complex.hpp"
#include <iostream>
#include <math.h>
// 类测试
void test() {
    using namespace std;
    Complex c1(3, -4);
    const Complex c2(4.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;
}
int main() {
    test();
}

 

task.5

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
class User {
private:
    string name, passwd, email;
    static int n;//静态数据成员
public:
    User(string na, string p, string e);//带3个参数的构造函数
    User(string na);//带一个参数的构造函数
    void change_passwd();
    void set_email();
    void print_info();
    
    static void print_n();//静态成员函数
};
int User::n = 0;//静态初始化
User::User(string na, string p, string e) :name(na), passwd(p), email(e) { n++; }//带三个参数的构造函数
User::User(string na) : name(na), passwd("111111"), email("") { n++; }//带一个参数的构造函数
void User::change_passwd() {
    string ps;
    int t = 2;
    cout << "Enter old password:";//输出Enter old password:
    cin >> ps;//输入旧密码保存到ps
    cout << endl;
    if (ps == passwd) {//判断是否与password一致
        cout << "Enter new password:";
        cin >> ps;
        passwd = ps;
        cout <<"new passwd is set successfully..."<<endl;//若一致,输出Enter new password:,输入新密码保存到ps,再赋值给passwd
    }
    else {
        while (t) {
            cout << "password input error.Please re-enter again:";
            cin >> ps;
            if (ps == passwd) {
                cout << "Enter new password:";
                cin >> ps;
                passwd = ps;
                cout << "new passwd is set successfully..." << endl;//若一致,输出Enter new password:,输入新密码保存到ps,再赋值给passwd
                break;
            }
            cout << endl;
            t--;
       }
        if (t == 0) { cout << "password input error.Please try after a while." << endl; }
    }
}//改密码
void User::set_email() {
    string ne;
    cout << "Enter email address:";
    cin >> ne;
    email = ne;
    cout << endl;
    cout << "email is set successfully..." << endl;
}//设邮箱
void User::print_info() {
    string s1{ sizeof(passwd) , '*' };
    // 构造一个字符串对象
    cout << "name:" << name << endl;
    cout << "passwd:" <<s1<< endl;
    cout << "email:" << email << endl;
}
void User::print_n() {
    cout << "there are " << n << " users." << endl;
}

task.5.cpp

#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl
        << "testing 2......\n\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}

 

标签:构造函数,cout,Complex,实验,User,include,string
From: https://www.cnblogs.com/qazmlp10/p/16805837.html

相关文章

  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基......
  • 实验6:开源控制器实践——RYU
    实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器。2.建立拓扑并连接RYU控制器3.通过Ryu的图形界面查看网络拓扑4.阅读Ryu文档的The......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 实验二 类与对象(2)
     Task4:Complex.hpp 1#pragmaonce23//Complex类的定义4#include<iostream>5#include<string>6#include<math.h>78usingstd::cout;9usin......
  • 实验2 类和对象(2)
    4.实验任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal,imag;public:Complex();~Com......
  • 实验6:开源控制器实践——RYU
    (一)基本要求搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。阅读Ryu文档的TheFirstApplication一节,运行当中的L2Switch,h1......
  • 实验2 类与对象(2)
    2022.10.12OOP实验课实验2类与对象(2)任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal;double......
  • 实验二
    Task4hpp文件如下#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):re......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本......
  • 实验二 类和对象
    实验任务4task4.cpp#include"Complex.hpp"#include<iostream>//类测试voidtest(){usingnamespacestd;Complexc1(1,-8);constComplexc2(6......