实验一类与对象

2021/10/21 6:11:06

本文主要是介绍实验一类与对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Complex.h

#pragma once
#ifndef CIRCLE_H
#define  CIRCLE_H
class Complex
{
public:
    Complex();
    Complex(double a, double b);
    Complex(double a);
    Complex(Complex& c);
    double get_real() { return real; };
    double get_imag() { return imag; };
    double get_real() const { return real; };
    double get_imag() const { return imag; };
    void show();
    void show() const;
    void add(const Complex& d);
    friend Complex add(Complex& c1, const Complex& c2);
    friend bool is_equal(Complex& c3, Complex& c4);
    friend bool is_equal(Complex& c3, const Complex& c4);
    friend double abs(Complex& c5);
private:
    double real, imag;
};
#endif

Complex.cpp

#include "Complex.h"
#include<iostream>
#include<math.h>
using namespace std;
Complex::Complex(double a) :real(a), imag(0) {}
Complex::Complex(double a, double b) : real(a), imag(b) {}
Complex::Complex() : real(0), imag(0) {}
Complex::Complex(Complex& c)
{
    real = c.real;
    imag = c.imag;
}
void Complex::show()
{
    if (imag > 0)
        cout << real << '+' << imag << 'i';
    else if (imag < 0)
        cout << real << imag << 'i';
    else { cout << real; }
}
void Complex::show() const
{
    if (imag > 0)
        cout << real << '+' << imag << 'i';
    else if (imag < 0)
        cout << real << imag << 'i';
    else { cout << real; }
}
void Complex::add(const Complex& d)
{
    real = real + d.real;
    imag = imag + d.imag;
}
Complex add(Complex& c1, const Complex& c2)
{
    Complex cc;
    cc.real = c1.real + c2.real;
    cc.imag = c1.imag + c2.imag;
    return cc;
}
bool is_equal(Complex& c3, Complex& c4)
{
    if ((c3.real == c4.real) && (c3.imag == c4.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool is_equal(Complex& c3, const Complex& c4)
{
    if ((c3.real == c4.real) && (c3.imag == c4.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}
double abs(Complex& c5)
{
    return sqrt(c5.real * c5.real + c5.imag * c5.imag);
}

task.3

#include <iostream>
#include<math.h>
#include"Complex.h"
using namespace std;
int main()
{
    Complex c1(1, 2);
    const Complex c2(3.4);
    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;
}

 User.h

#pragma once
#include<iostream>
using namespace std;
class User
{
public:
    User(string a, string b="111111", string c="") :name(a), passwd(b), email(c) { n++; }
     void set_email();
     void change_passwd();
     void printf_info();
       static void printf_n() { cout << "threre are" << n <<" "<< "users" << endl; }
private:
    string name, passwd, email;
    static int n;
};

User.cpp

#include<iostream>
#include<string>
#include"User.h"
using namespace std;
int User::n = 0;
void User::set_email()
{
    string s;
    cout << "Enter email address:";
    cin >> s;
    email = s;
    cout << "email is set successfully..."<<endl;
}
void User::change_passwd()
{
    int n = 3;
    cout << "Enter old password:";
    while (n!=0)
    {
        n = n - 1;
        string s;
        cin >> s;
        cout << endl;
        if ((s != passwd)&&n!=0)
        {
            cout << "password input error,please re-enter again:" ;
        }
        if ((s != passwd) && n == 0)
        {
            cout << "password input error,please try after a while";
        }
        if (s == passwd)
        {
            cout << "Enter new passwd:";
            string t;
            cin >> t;
            cout << endl;
            passwd = t;
            cout << "new passwd is set successfully..."<<endl;
            break;
        }
    }
    cout << endl;
}
void User::printf_info()
{
    cout << "name" << ":" << name << endl; 
    cout << "passwd" << ":" << "******" << endl;
    cout << "email" << ":" << email << endl;
}

task.4

#include <iostream>
#include "User.h"
using namespace std;
int main()
{
    cout << "testing 1......" << endl;
    User user1("chovy", "4396", "ggg@hotmail.com");
    user1.printf_info();
    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("jjeonard");
    user2.change_passwd();
    user2.set_email();
    user2.printf_info();

    User::printf_n();
}

 

 



这篇关于实验一类与对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程