实验一类与对象

task3

Complex.cpp

#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include<bits/stdc++.h>
using namespace std; 
class Complex
{
    public:
        Complex();
        Complex (double num);
        Complex(double a,double b);
        Complex(const Complex &a);
        Complex(int num);
        Complex(int a,int b);
        double get_real ()const;
        double get_imag ()const;
        void show ()const;
        void add(Complex a);
        friend Complex add(Complex c1,Complex c2);
        friend bool is_equal(const Complex c1,const Complex c2);
        friend double abs(Complex a);
    private:
        double real;
        double imag;
};
Complex::Complex():real(0),imag(0){}
Complex::Complex(const Complex &a){
    real=a.real;
    imag=a.imag;
}
Complex::Complex(int num){
    real=num;
    imag=0;
}
Complex::Complex(int a,int b):real(a),imag(b){};
Complex add(Complex c1,Complex c2){
    c1.real+=c2.real;
    c1.imag+=c2.imag;
    return c1;
}
void Complex::add(Complex a){
    real+=a.get_real();
    imag+=a.get_imag();
}
bool is_equal(Complex c1,Complex c2){
    return c1.real==c2.real&&c1.imag==c2.imag?1:0;
}
double abs(Complex a){
    return sqrt(a.real*a.real+a.imag*a.imag);
}
Complex::Complex(double a){
    real=a;
    imag=0;
}
Complex::Complex(double a,double b):real(a),imag(b){} 
double Complex::get_real()const{
            return real;
    }
double Complex::get_imag()const{
            return imag;
    }
void Complex::show()const{
    if(imag>0){
        cout<<real<<"+"<<imag<<"i";
    }
    else if(imag==0){
        cout<<real;
    }
    else if(imag<0){
        cout<<real<<imag<<"i";
        }
    }
#endif

main.cpp

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

 

 

------------------------------------------------------------------------------

task4

Use.cpp

#ifndef USER_HPP
#define USER_HPP
#include<bits/stdc++.h>
using namespace std;
class User
{
    public:
        User(string a);
        User(string name,string passwd,string email);
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
    private:
        string name;
        string passwd;
        string email;
        static int n;
};
User::User(string a):name(a),passwd("111111"),email(){n++;}
User::User(string name,string passwd,string email){
    this->name=name;
    this->passwd=passwd;
    this->email=email;
    n++;
}
void User::set_email(){
    cout<<"Enter email address:";
    cin>>this->email;
    cout<<"Email is set successfully..."<<endl;
}
void User::change_passwd(){
    cout<<"Enter old passwd:";
    string a;
    cin>>a;
    if(a==this->passwd){
        cout<<"Enter new passwd:";
        cin>>this->passwd;
        cout<<"new passwd is set successfully..."<<endl;
    }
    else if(a!=this->passwd){
        int i;
        for(i=2;i>0;i--){
            cout<<"password input error.Please re-enter again:";
            cin>>a;
            if(a==this->passwd){
                cout<<"Enter new passwd:";
                cin>>this->passwd;
                cout<<"new passwd is set successfully..."<<endl;
                break;
            }
        }
        if(i==0){
                cout<<"password input error.Please try after a while."<<endl;
            }
        
    }
}
void User::print_info(){
    cout<<"name:"<<this->name<<endl;
    cout<<"passwd:"<<"******"<<endl;
    cout<<"email:"<<this->email<<endl;
    
}
int User::n=0;
void User::print_n(){
    if(n>=2){
        cout<<"there are "<<n<<" users"<<endl;
    }
    else if(n==1){
        cout<<"there is 1 user"<<endl;
    }
}
#endif

main.cpp

#include "User.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Jonny", "92197", "asdfgh@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();
}

 

 

 

posted @ 2021-10-24 15:09  黄金派大星  阅读(27)  评论(3编辑  收藏  举报