其他分享
首页 > 其他分享> > 实验一:类与对象

实验一:类与对象

作者:互联网

实验三

<task3.cpp>

#include "Complex.hpp"
#include <iostream>
int main()
{
    using namespace std;
    Complex c1(6, -8);
    const Complex c2(3.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;
}

<Complex.hpp>

# ifndef COMPLEX_HPP
# define COMPLEX_HPP
# include<iostream>
//复数类
class Complex {
private:
    double real;
    double imag;
public:
    Complex() {}
    Complex(double a) :real(a), imag(0) {}
    Complex(double a, double b) :real(a), imag(b) {}
    Complex(const Complex& p) :real(p.get_real()), imag(p.get_imag()) {}        
    ~Complex() {}
    double get_real()const;
    double get_imag()const;
    void show()const;
    Complex add(const Complex& p);
    friend Complex add(const Complex& p,const Complex &q);
    friend bool is_equal(const Complex& p, const Complex& q);
    friend double abs(const Complex& p);
};
//返回实部
double Complex::get_real()const
{    
    return real;
}
//返回虚部
double Complex::get_imag()const
{
    return imag;
}
//打印复数
void Complex::show()const                                                                                                                                                                                                                                                                                                                                
{
    if (imag > 0)
        std::cout << real << "+" << imag << "i" << std::endl;
    else if (imag < 0)
        std::cout << real << imag << "i" << std::endl;
    else
        std::cout << real << std::endl;
}
//一个复数加到自身
Complex Complex::add(const Complex& p)
{
    real += p.get_real();
    imag += p.get_imag();
    return(real, imag);
}
//两个复数加法,值返回
Complex add(const Complex& p, const Complex& q)
{
    return Complex(p.real+ q.real, + p.imag+q.imag);
}
//判断两个复数相等
bool is_equal(const Complex &p, const Complex &q)
{
    if (p.real == q.real && p.imag == q.imag)
        return true;
    else
        return false;
}
//取模运算
double abs(const Complex &p)
{
    double a = p.real;
    double b = p.imag;
    return sqrt(a * a + b * b);
}
#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();
}

<User.hpp>

# ifndef USER_HPP
# define USER_HPP
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//用户类
class User {
private:
    string name;
    string password;
    string email;
    static int count;
public:
    User(string a) :name(a), password("111111"), email{ "\0" } {count++; }
    User(string a, string b, string c) :name(a), password(b), email(c) { count++; }
    ~User() {}
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};
//用户人数初始为0
int User::count = 0;
//设置邮箱
void User::set_email()
{
    cout << "Enter email address: ";
    string a;
    while (1)
    {
        cin >> a;
        if (a.find('@')!=string::npos && a.find(".com")!=string::npos)//判断是否创建正确
        {
            cout << "email is set successfully." << endl;
            email = a;
            break;
        }
        else
            cout << "email is error,try again: " ;
    }
}
//修改密码
void User::change_passwd()
{
    int n = 3;
    cout << "Enter old password: ";
    while (n--)
    {
        string a;
        cin >> a;
        //验证密码
        if (a == password)//密码正确
        {
            cout << "Right,enter new password: ";
            while (1)
            {
                cin >> a;
                if (a.length() < 7)//密码小于7位
                {
                    password = a;
                    cout << "password is changed successfully"<<endl;
                    break;
                }
                else
                    cout << "new password input error.Please re-enter again: ";
            }
            break;
        }
        else//密码错误
            if (n != 0)
                cout << "password input error.Please re-enter again: ";
            else
                cout << "password input error.Please try after a while" << endl;
    }

}
//打印个人信息
void User::print_info()
{
    cout << left << setw(10) << "name:" << name << endl << setw(10) << "password:" << "******" << endl <<
        setw(10) << "email:" << email << endl;
}
//打印人数
void User::print_n()
{
    if (count > 1)
        cout << "there are " << count << " users.";
    else if (count == 1)
        cout << "there is 1 user.";
    else
        cout << "none";
}
#endif

运行结果1:

 

 运行结果2:

 

 

 

 

标签:const,string,对象,double,imag,Complex,实验,cout
来源: https://www.cnblogs.com/kidx2/p/15443173.html