实验1 类与对象

------------恢复内容开始------------

------------恢复内容开始------------

 Complex类的定义

#include<iostream>
#include<math.h>
using namespace std;
//类的定义
class Complex
{
public:
    //构造函数
    Complex(){};
    //构造函数重载
    Complex(double x):real(x),imag(0){}
    Complex(double x,double y):real(x),imag(y){}
    Complex(Complex &c1):real(c1.get_real()),imag(c1.get_imag()){}
    //得到实部
    double  get_real () const {return real;}
    //得到虚步
   double get_imag() const { return imag;}
    //显示实部和虚部
   void show() const
    {
       cout<<real;
        if(imag>0)cout<<'+'<<imag<<'i';
        if(imag<0) cout<<imag<<'i';
        cout<<endl;
    }
     void add(const Complex &c2)
    {
        real+=c2.get_real();
        imag+=c2.get_imag();
    }
    //友员函数
    //虚数相加返回虚数
    friend Complex add(const Complex  &c1,const Complex &c2);
    //判断虚数是否相等
    friend  const bool is_equal( const Complex &c1,const Complex &c2) ;
    //求虚数的模
    friend double abs(Complex c1);
private:
    double real;
    double imag;
};
Complex add(const Complex &c1,const Complex &c2)
{
     Complex c(c1.real+c2.real,c1.imag+c2.imag);
     return c;
 }

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




主函数代码
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(4, -3); 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; }

实验1 类与对象

#ifndef User_hpp
#define User_hpp

#include <iostream>
using namespace std;
#endif /* User_hpp */
class User
{
public:
    User(){};
    User(string initialname,string initialpassword="111111",string initialemail=""):name{initialname},password(initialpassword),email(initialemail){n++;}
    //设置邮箱。提示用户从键盘输入邮箱,邮箱地址不合理会重新输入。
    void set_email();
    //修改密码。修改密码前,要求先输入旧的密码,验证无误后,才允许修改;如果输入旧密码时,连续三次输入错误提示用户稍后再试,暂时退出修改密码程序,密码长度不合理会提示重新输入。
    bool change_passwd();
    //打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。
    void print_info();
    //输出用户总数
    static void print_n();
private:
    string name;      //姓名
    string password;  //密码
    string email;     //email
    static int n;     //人员总数
};
void User::set_email()
{  cout<<"Enter email address:";
    string newemail;
    while(1)
    {
    cin>>newemail;
    int i=0;
    for(;i<newemail.length();i++)
    if(newemail[i]=='@') break;
    if(i<=newemail.length()-2) break;
    if(i==newemail.length())
        cout<<"The eamil address is unreasonable. Please re-enter again:";
    }
    email=newemail;
    cout<<"email is set successfully..."<<endl;
  }
bool User::change_passwd()
{
    cout<<"Enter old password:";
    string word;int n=2;
    cin>>word;
    if(word!=password)
     while(n--)
        {
            cout<<"password input error. Please re-enter again:";
            cin>>word;
            if(word==password) break;
            
        }
    if(word!=password)
    {cout<<"password input is error. Please try after a while"<<endl;
        return 0;
    }
    if(word==password)
    {
        cout<<"Enter new password:";
         while(1)
        { cin>>password;
            if(password.length()>=6&&password.length()<=20) break;
            else cout<<"The password length is unreasonable. Please re-enter again:";
        }
    }
    return 0;
}
void User::print_info()
{
    cout<<"name:  "<<name<<endl<<"password:  "<<"******"<<endl<<"email:  "<<email<<endl;
}
void User::print_n()
{
    cout<<"there are "<<n<<" users."<<endl;
}
 int User::n=0;  //n初始化。

 

#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jack", "92199", "twobeno1@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();
}

实验1 类与对象

 

实验1 类与对象

 

 

------------恢复内容结束------------

------------恢复内容结束------------

上一篇:实验一 类与对象


下一篇:史上最全的 Python 3 类型转换指南