实验1 类与对象

实验任务三

Complex.hpp文件源码

#include <iostream>
#include <string>
#include <iomanip> 
#include <cmath>

using namespace std;

class Complex
{
    public:
        Complex();
        Complex(double r);
        Complex(double r,double i);
        Complex(Complex &p);
        double get_real() const;
        double get_imag() const;
        void show() const;
        double add(const Complex &a);
        friend Complex add(const Complex &b,const Complex &c);
        friend bool is_equal(const Complex &d, const Complex &e);
        friend double abs(Complex &f);
        
    private:
        double real;
        double imag;
};


Complex::Complex()
{

}

Complex::Complex(double r)
{
    real=r;
    imag=0;
}

Complex::Complex(double r,double i)
{
    real=r;
    imag=i;
}

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.0)
    cout<<real;
    else
    cout<<real<<"+"<<imag<<"i";
}

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


double Complex::add(const Complex &a)
{
    real=real+a.real;
    imag=imag+a.imag;
}

Complex add(const Complex &b,const Complex &c)
{
    Complex c0;
    c0.real=b.real+c.real;
    c0.imag=b.imag+c.imag;
    return c0;
 } 
 
 bool is_equal(const Complex &d, const Complex &e)
 {
     
     if(d.real==e.real&&d.imag==e.imag)
     return true;
     else
     return false;
 }
 
 double abs(Complex &f)
 {
     Complex c1;
     c1.real=f.real;
     c1.imag=f.imag;
     return sqrt(c1.real*c1.real+c1.imag*c1.imag);
 }

Task3.cpp源代码

#include "Complex.hpp"
#include <iostream>
#include <string>
#include <iomanip> 
#include <cmath>

int main()
{

 
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;
}

实验1 类与对象

 

 实验任务四

User.hpp源代码

#include <iostream>
#include <cstring>

using namespace std;

class User
{
    public:
    User();
    User(string name0);
    User(string name0 , string password0,string email0);
    void set_email();
    void change_passwd();
    void print_info();

    string set_email() const;
    static void print_n();
    private:
        string name;
        string password;
        string email;
        static int n;
    
    
};

int User::n=0;

User::User()
{
    ++n;
}
User::User(string name0)
{
    name=name0;
    password="111111";
    email="\0";
    ++n;
}

User::User(string name0, string password0,string email0)
{
    name=name0;
    password=password0;
    email=email0;
    ++n;
}
void User::print_info() 
{
cout << "name: " << name << endl;
cout << "password: " << "******" << endl;
cout << "email: " << email<<endl;
}

void User::change_passwd() 
{
    cout<<"Enter old password:";
    char a[100];
    int i,j,length,flag=0;
    for (j=0;j<3;j++)
    {
    
    gets(a);
    length=strlen(a);
    for (i=0;i<length;i++)
    {
        if(a[i]!=password[i])
        {
        flag++;
        if(flag<3)
        cout<<"password input error. Please re-enter again:";
        else
        cout<<"password input error. Please try after a while.";
        break;
        }
    }
    if(i>=length)
    break;

}

    if(flag==3)
    exit(0);
    if(i>=length)
    {
        cout<<endl;
        cout<<"Enter new password:";
        password="\0";
        cin>>password;
        cout<<"Password is set successfully..."<<endl;
    }
}

void User::set_email() 
{
    cout<<"enter email address:";
    cin>>email;
    cout<<"email is set successfully..."<<endl;

}

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

task4.cpp源代码

#include"User.hpp"
#include <iostream>
#include <cstring>

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();
}

 

 

实验1 类与对象

 

实验1 类与对象

 

 

 

上一篇:Discuz! X3 全新安装图文教程


下一篇:笔记:C++面向对象高级编程--侯捷