首页 > 其他分享 >实验2 类和对象(2)

实验2 类和对象(2)

时间:2022-10-14 18:11:28浏览次数:53  
标签:const cout 对象 void Complex 实验 include imag

实验四

Complex.hpp

#pragma once
#include<iostream>
#include<cmath>
using namespace std;

class Complex
{
public:
    Complex(){
    };
    Complex(double a){
        real = a;
        imag = 0;
    };
    Complex(double b, double c)
    {
        real = b;
        imag = c;
    };
    Complex(const Complex &c3)
    {
        real = c3.real;
        imag = c3.imag;
        
    };
    ~Complex(){};
    double get_real()const{
        return real;
    };
    double get_imag()const{
        return imag;
    };
    void show()const;
    void add(const Complex &c2);
    friend Complex add(const Complex &c1, const Complex &c2);
    friend bool is_equal(const Complex &c1, const Complex &c2);
    friend double abs(Complex &c1);
private:
    double real;
    double imag;
};

void Complex::show()const{
    if(get_imag()>0)
    cout << get_real() << " + " << get_imag() << "i";
    else if(get_imag()<0)
    cout << get_real() << " - " << get_imag()*(-1) << "i";
    else
    cout << get_real();
}

void Complex::add(const Complex &c2){
    real += c2.real;
    imag += c2.imag; 
    
}
Complex add(const Complex &c1, const Complex &c2){
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}
bool is_equal(const Complex &c1, const Complex &c2){
    if(c1.real==c2.real&&c1.imag==c2.imag)
    return true;
    else
    return false;
}
double abs(Complex &c1){
    return(sqrt(c1.real*c1.real+c1.imag*c1.imag));
}

task4.cpp

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

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();
}

运行结果

 实验五

User.hpp

#pragma once
#include<iostream>
#include<string>

using namespace std;

class User {
public:
    User(string na, string pa = "111111", string em = "");
    void set_email();
    void change_passwd();
    void print_info()const;
    void static print_n() {
        cout << "there are " << n << " users" << endl;
    }
private:
    string name, passwd, email;
    static int n;


};
int User::n = 0;
User::User(string na, string pa, string em) :name(na), passwd(pa), email(em) {
    n++;
}

void User::set_email(){
    cout << "Enter email address:" ;
    cin >> email;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() {
    cout << "Enter old password:";
    string c;
    int count = 1;
    cin >> c;
    while (c != passwd && count < 3) {
        cout << "password input error.please re-enter again: ";
        cin >> passwd;
        count++;
        if (count == 3) {
            cout << "password input error.please try after a while." << endl;
        }
    }

    if (count != 3) {
        cout << "enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
}
void User::print_info()const {
    int l = passwd.length();

    cout << "name: " << name << endl;
    cout << "password: ";
    while (l--) {
        cout << "*";
    }cout << '\n';
    cout << "email: " << email << endl;
}

task5.cpp

#include "User.hpp"
#include <iostream>

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();
}

运行结果

 

 

 

标签:const,cout,对象,void,Complex,实验,include,imag
From: https://www.cnblogs.com/DeviIMayCry/p/16789340.html

相关文章

  • 把已经释放的CR的对象包到现有CR中
    我之前写了合并两个没有释放的CR的博客:​​http://blog.sina.com.cn/s/blog_c0978c9b0102vgiy.html​​今天是想把已经释放的CR,包到一个新CR下,合并不能用,但是可以用下面的方......
  • HU_CREATE_GOODS_MOVEMENT报错:对象清单抬头数据中的差异
    对于已经创建HU的物料,调用HU_CREATE_GOODS_MOVEMENT 创建凭证的时候遇到了下面的问题情景是这样:先对ct00工厂的数据进行了bapi调用commit后又对CT20工厂数据进行操作这个......
  • 面向对象
    面向对象:封装、继承、多态打球:面向过程:专注于打球的动作面向对象:有一个人的对象,人的对象中包含了一个打球的动作JAVA有类构成,类由成员属性和成员......
  • 常见内置函数及可迭代对象
    目录重要内置函数常见内置函数可迭代对象迭代器对象for循环的本质异常捕获/处理重要内置函数zipl1=[11,22,33,44,55]l2=['jason','kevin','oscar','jerry',......
  • 实验二 类与对象2
    4.实验任务4不使用C++标准库,自行设计并实现一个简化版复数类ComplexComplex.hpp1#include<iostream>2#include<math.h>3usingnamespacestd;45clas......
  • 10月14日内容总结——内置函数、可迭代对象和迭代器、异常捕获与处理
    目录一、重要内置函数1、zip2、filter3、sorted二、常见内置函数(了解)1、abs2、all3、any4、bytes5、bin、oct、hex、int6、callable7、chr、ord8、dir9、divmod10、enumera......
  • js 数组对象中每一项对象属性比较大小与计算数组对象属性和
    //数组对象排序方法 exportfunctioncompare(property){   returnfunction(a,b){    constvalue1=a[property]    constvalue2......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运......
  • 软件设计实验9
    实验9:桥接模式[实验任务一]:两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。直接放源码:#include<iostream>using......
  • 第二次实验
    实验任务四Complex.hpp源代码1#pragmaonce234#include<iostream>5#include<cmath>6usingnamespacestd;78classComplex9{10public:11......