实验二:类和对象

实验任务4

Complex.hpp

#pragma once
#include<iostream>
#include<cmath>

using std::cout;
using std::endl;

class Complex{
    public:
        Complex(double x=0.0,double y=0.0):real{x},imag{y}{};
        Complex(const Complex&obj):real{obj.real},imag{obj.imag}{};
        double get_real() const;
        double get_imag() const;
        void show() const;
        void add(const Complex&obj);
        friend Complex add(const Complex&x,const Complex&y);
        friend bool is_equal(const Complex&x,const Complex&y);
        friend double abs(const Complex&obj);
    private:
        double real;
        double imag; 
};

double Complex::get_real() const{
return real;
}

double Complex::get_imag() const{
return imag;
}

void Complex::show() const{
if(imag==0)
cout<<real;
if(imag>0)
cout<<real<<" + "<<imag<<"i";
if(imag<0)
cout<<real<<imag<<"i";
}

void Complex::add(const Complex&obj){
real+=obj.real;
imag+=obj.imag;
}

Complex add(const Complex&x,const Complex&y){
    Complex obj;
    obj.real=x.real+y.real;
    obj.imag=x.imag+y.imag;
    return obj;
}

bool is_equal(const Complex&x,const Complex&y){
    if(x.real==y.real&&x.imag==y.imag)
    return true;
    else
    return false;
}

double abs(const Complex&obj){
    return sqrt(obj.real*obj.real+obj.imag*obj.imag);
}

task4.cpp

#include"Complex.h"
#include<iostream>
void test(){
    using namespace std;
    Complex c1(10,-8);
    const Complex c2(9.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();
}

 

 

 实验任务5

User.hpp

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

using namespace std;

class User{
public:
User(string x,string p="111111",string e="");
void set_email();
void change_password();
void print_info();
static void print_n();
private:
string name;
string password;
string email;
private:
static int n;
};
int User::n=0;

User::User(string x,string p,string e):name{x},password{p},email{e}{
++n;
}

void User::print_info(){
cout<<left<<setw(10)<<"name: "<<name<<endl;
cout<<left<<setw(10)<<"password: ";
for(int i=0;i<password.length();i++)
cout<<"*";
cout<<endl;
cout<<left<<setw(10)<<"email: "<<email<<endl;
}

static void User::print_n(){
cout<<"there are "<<User::n<<" users.";
}

void User::set_email(){
cout<<"Enter email address: ";
string x;
cin>>x;
email=x;
cout<<"email is set successfully..."<<endl;
}

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

task5.cpp

#include"User.h"
#include<iostream>
void test(){
    using std::cout;
    using std::endl;
    
    cout<<"testing 1......\n";
    User user1("Chaihao","520666","wanglai@qq.com");
    user1.print_info();
    
    cout<<endl
    <<"testing 2......\n\n";
    
    User user2("liyuan");
    user2.change_password();
    user2.set_email();
    user2.print_info();
    
    cout<<endl;
    User::print_n();
} 
int main(){
    test();
}

 

 

 

posted @ 2022-10-15 18:01  熊二想吃蜂蜜  阅读(44)  评论(0)    收藏  举报