实验二 类和对象(2)

一.实验任务4

1.源代码:

Complex.hpp:

#pragma once
#include<cmath>
#include<iostream>
using namespace std;

class Complex{
public:
    Complex(double m=0,double n=0){real=m;imag=n;}
    Complex(const Complex& c1);
    ~Complex()=default;
    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(Complex const &c1);
    friend Complex add(Complex const&c1,Complex const&c2);
    friend bool is_equal(Complex const&c1,Complex const&c2);
    friend double abs(Complex const&c1);
    
private:
    double real;
    double imag;
    
};

Complex::Complex(const Complex&c1){
    real=c1.real;imag=c1.imag;
}

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

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

void Complex::show() const{
if(imag==0)
cout<<real;
else if(imag<0)
cout<<real<<"-"<<abs(imag)<<"i";
else cout<<real<<"+"<<imag<<"i";

}

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

Complex add(Complex const&c1,Complex const&c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c1.imag;
    return c3;
}
bool is_equal(Complex const&c1,Complex const&c2){
if(c1.real==c2.real&&c1.imag==c2.imag) return true;
else return false;
}

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

2.task4.cpp:

#include "complex.cpp"
#include <iostream>

// 类测试
void test() {
    using namespace std;

    Complex c1(3, -4);
    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;
}

int main() {
    test();
}

2.运行调试:

更换数据后:

 

二.实验任务5

1.源代码:

User.hpp:

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

using namespace std;
class User{
public:
User(string name0,string passwd0="111111",string email0="");
void set_email();
void change_passwd();
void print_info();static int print_n();
private:
string name;
string passwd;
string email;
};
User::User(string name0,string passwd0,string email0)
{
    name=name0;passwd=passwd0;email=email0;
    
}

void User::set_email(){
cout<<"Enter email adress:";
cin>>email;
cout<<"succeed"<<endl;
}

void User::change_passwd(){
string passwd1;int flag=1,num=1;
cout<<"Please enter your old passwd:";
while(flag){
cin>>passwd1;
if(passwd1==passwd){
    cout<<"Please enter your new passwd:";
    cin>>passwd;
    cout<<"succeed"<<endl;flag=0;}
else if(num<3) {
cout<<"please enter your old passwd again:";
flag=1;num++;}
else {
cout<<"Plaese try again later"<<endl;
flag=0;
}}}

void User::print_info(){
int i;
cout<<"name:"<<name<<endl;
cout<<"passwd:";
for(i=1;i<=passwd.length();i++)
{
cout<<"*";
}
cout<<"email:"<<email;
;
}
int User::print_n(){
static int x = 1;
    x ++;
    cout << "there are " << x << " users." << endl;
    return x;
}

 

task5.cpp:

#include "User.hpp"
#include <iostream>
// 测试User类
void test()
{
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";

    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main()
{
    test();
}

2.运行调试:

 

 

posted @ 2022-10-18 14:17  小帅不爱摆烂  阅读(20)  评论(0编辑  收藏  举报