zoukankan      html  css  js  c++  java
  • 实验1 类与对象

    task3

    Complex.hpp源码

    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    class Complex
    {
        public:
        Complex(float newReal,float newImag);
        Complex();
        Complex(float newReal);
        Complex(const Complex &p);
        float get_real()const;
        float get_imag()const;
        void show() const;
        void add(Complex p);
        friend Complex add(Complex p1,Complex p2);
        friend bool is_equal(Complex p1,Complex p2);
        friend float abs(Complex &p);
    
        private:
        float real,imag;
    };
    
    Complex::Complex(float newReal,float newImag):real(newReal),imag(newImag){}
    Complex::Complex():real(0),imag(0){}
    Complex::Complex(float newReal):real(newReal),imag(0){}
    Complex::Complex(const Complex &p){
        real=p.real;
        imag=p.imag;
    }
    float Complex::get_real()const{
        return real;
    }
    float Complex::get_imag()const{
        return imag;
    }
    void Complex::show()const{
        cout<<real;
        if(imag<0){
            cout<<" - "<<-imag<<"i";
        }
        else if(imag>0){
            cout<<"+"<<imag<<"i";
        }
        
    }
    void Complex::add(Complex p){
        real=real+p.real;
        imag=imag+p.imag;
    }
    Complex add(Complex p1,Complex p2){
        Complex c;
        c.real=p1.real+p2.real;
        c.imag=p1.imag+p2.imag;
        return c;
    }
    bool is_equal(Complex p1,Complex p2){
        if(p1.real==p2.real && p1.imag==p2.imag) return true;
        else return false;
    }
    float abs(Complex &p){
        float temp1,temp2;
        temp1=p.real*p.real+p.imag*p.imag;
        temp2=sqrt(temp1);
        return temp2;
    }

    task3.cpp源码

    #include "Complex.hpp"
    #include <iostream>
    
    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;
    }

    task3.cpp对应结果截图

     新实验数据源码

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

    User.hpp源码

    #include<iostream>
    #include<string.h>
    
    using namespace std;
    
    
    class User{
        public:
        User(string newName);
        User(string newName ,string newPassWord, string newEmail);
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
        private:
        string name;
        string passwd;
        string email;
        static int count;
    };
    int User::count=0;
    
    User::User(string newName):name(newName){
        ++count;
        passwd="111111";
        email="";
    }
    User::User(string newName ,string newPassWord, string newEmail):name(newName),passwd(newPassWord),email(newEmail){
        ++count;
    }
    
    void User::set_email(){
        cout<<"Enter email address:";
        cin>>email;
        cout<<"email is set successfully..."<<endl;
        
    }
    
    void User::change_passwd(){
        int newwd,i;
        string testwd;
        cout<<"Enter old password:";
        cin>>testwd;
        for(i=0;i<2;){
        if(testwd==passwd){
            cout<<"Enter new passwd:";
            cin>>newwd;
            passwd=newwd;
            cout<<"new passwd is successfully..."<<endl;
            break;
        }else{
            cout << "password input error.Please re-enter again:";
            cin>>testwd;
            i++;
        }
        if(i==2) cout<<"password input error. Please try after a while."<<endl;
        
    }
    }
    void User::print_info(){
        cout<<"name:"<<name<<endl;
        cout<<"passwd: ******"<<endl;
        cout<<"email:"<<email<<endl;
    }
    
    void User::print_n(){
        if(count==1) cout<<"there is 1 user";
        else cout<<"there are " <<count<<" users";
    }

    task4.cpp源码

    #include "User.hpp"
    #include <iostream>
    
    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();
    }

    task4.cpp对应结果截图

     新实验数据源码

    int main()
    {
        using namespace std;
    
        cout << "testing 1......" << endl;
        User user1("陈超凡", "020220", "nmsl@hotmail.com");
        user1.print_info();
    
        cout << endl
             << "testing 2......" << endl
             << endl;
        User user2("朱辰逸");
        user2.change_passwd();
        user2.set_email();
        user2.print_info();
    
       

    新实验数据实验结果截图

    实验总结:

    1.注意const的用法和他对引用&的影响

    2.注意接口的定义和实现是否满足对应实验要求

  • 相关阅读:
    酒店预订管理系统
    毕业论文管理系统
    酒店预订管理系统
    闪屏+引导页
    android编程测试
    测试用例
    ER图
    软件工程作业
    毕业论文管理系统
    酒店管理系统
  • 原文地址:https://www.cnblogs.com/algebra/p/15437180.html
Copyright © 2011-2022 走看看