1. 程式人生 > 其它 >C++實驗一 類與物件

C++實驗一 類與物件

實驗三:

#include "Complex.hpp"
#include <iostream>
int main()
{
    using namespace std;

    Complex c1(12,-5);
    const Complex c2(5.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; }
#ifndef COMPLEX_HPP
#define COMPLEX_HPP #include <iostream> #include <iomanip> #include<math.h> #include <string> using namespace std; class Complex { public: Complex():real(0),imag(0) { }; Complex(double r,double i):real(r),imag(i) { }; Complex(double r):real(r),imag(0) { }; Complex(const Complex &c):real (c.real),imag(c.imag) { }; ~Complex()=default; double get_real() const { return real; } double get_imag() const { return imag; } void show() const { if(imag>0) cout<<real<<"+"<<imag<<"i"; else if (imag == 0) cout << real; else cout<<real<<imag<<"i"; } void add(const Complex c) { this->real=this->real+c.real; this->imag=this->imag+c.imag; } friend Complex add(const Complex c1,const Complex c2) { Complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c; } friend bool is_equal(const Complex c1,const Complex c2) { if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; } friend double abs(const Complex c) { return sqrt(c.real*c.real+c.imag*c.imag); } private: double real,imag; }; #endif

實驗四

#include "User.hpp"
#include <iostream>
int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("hyh", "20208329", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("huangyh");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}
#ifndef USER_HPP
#define USER_HPP
#include <iostream>
#include <iomanip>
#include<math.h>
#include <string>
using namespace std;
class User{
    public:
        User(string a,string b,string c):name(a),password(b),email(c){
            n++;
        };
        User(string a):name(a),password("111111"),email(""){
            n++;
        };
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
    private:
        string name;
        string password;
        string email;
        static int n;
};
int User::n=0;

void User::set_email(){
    string s;
    cout<<"Enter email addres: ";
    cin>>s;
    email=s;
    cout<<"email is set successfully"<<endl;


}
void User::change_passwd(){
    string h,newh;
    int i=1;
    cout<<"Enter old password: ";
    cin>>h;
    while(h!=password){
        cout<<"password input error. Please re-enter again: ";
        cin>>h;
        i++;
        if(i>=3){
            cout<<"Please try after a while"<<endl;
            break;
        }
    }
    if(h==password)
    {
        cout<<"Enter new password: ";
        cin>>newh;
        password=newh;
        cout<<"new passwd is set sucessfully..."<<endl;
    }
}
void User::print_info(){
    cout<<"name: "<<name<<endl
    <<"password: "<<"******"<<endl
    <<"email: "<<email<<endl;
}
void User::print_n(){
    cout<<"there are "<<n<<" users"<<endl;
}
#endif

實驗總結:通過本次實驗,我對c++類有了更深入的瞭解,熟悉了類的靜態資料成員和函式成員,並學會了在一個工程資料夾下多檔案完成程式碼