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

    实验结论

    实验任务3

    #ifndef COMPLEX_HPP
    #define COMPLEX_HPP
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    class Complex{
        public:
            Complex(double a=0,double b=0){
                real=a;
                imag=b;
            }
            ~Complex(){}
            Complex(const Complex  &c){
                real=c.real;
                imag=c.imag;
            }
            double get_real()const;
            double get_imag()const;
            void show()const;
            void add(const Complex &c);
        
            friend Complex add(const Complex &c1,const Complex &c2);
            friend bool is_equal(const Complex &c1,const Complex &c2);
            friend double abs(Complex &c);
            
        private:
            double real,imag;
    };
    
    double Complex::get_real() const{
        return real;
    }
    double Complex::get_imag() const{
        return imag;
    }
    void Complex::show() const{
        using namespace std;
        if (imag>0)
            cout <<real<<"+"<<imag<<"i";
        else if (imag<0)
            cout <<real<<imag<<"i";
        else 
            cout <<real;
    }
    void Complex::add(const Complex &c){
        real+=c.get_real();
        imag+=c.get_imag(); 
    } 
    
    Complex add(const Complex &c1,const Complex &c2){
        Complex c;
        c.real=c1.real+c2.real;
        c.imag=c1.imag+c2.imag;
        return c;
    } 
    bool is_equal(const Complex &c1,const Complex &c2){
        if(c1.real==c2.real && c1.imag==c2.imag){
            return true;
        }else 
        return false;
    }
    double abs(Complex &c){
        return sqrt(c.real*c.real+c.imag*c.imag);
    }
    
    #endif
    #include "Complex.hpp"
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        Complex c1(6, -5);
        const Complex c2(3.6);
        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;
    }

    实验任务4

    #ifndef USER_HPP
    #define USER_HPP
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    class User{
        public:
            User(string name0,string passwd0="111111",string email0=""):name(name0),passwd(passwd0),email(email0){
                ++n;
            }
            void set_email();
            void change_passwd();
            void print_info();
            static void    print_n();
        private:
            string name;
            string passwd;
            string email;
            static int n;
    
        
    };
    int User::n=0;
    void User::set_email(){
        cout<<"Enter email address: ";
        cin>>email;
        cout<<"email is set successfully..."<<endl;
    }
    
    void User::change_passwd(){
        cout<<"Enter old password: ";
        string passwd1;
        string passwd2;
        int i=1;
        for(i=1;i<=3;i++){
            cin>>passwd1;
            if(passwd1!=passwd){
                if(i<=2){
                    
                    cout<<"password input error. Please re-enter again: ";
                    
                }
                if(i==3){
                    
                    cout<<"password input error. Pleasa try after a while."<<endl;
                    
                }
            }
            else{
                cout<<"Enter new passwd: ";
                cin>>passwd2;
                passwd=passwd2;
                cout<<"new passwd is set successfully..."<<endl;
                break;
            }
        }
    }
    void User::print_info(){
        cout<<"name:   "<<name<<endl;
        cout<<"passwd: ******"<<endl;
        cout<<"email: "<<email<<endl;
    }
    void User::print_n(){
        cout<<"there are "<<n<<" users.";
    }
    
    #endif
    #include "User.hpp"
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        cout << "testing 1......" << endl;
        User user1("Curry", "942030", "stc@nbastar.com");
        user1.print_info();
    
        cout << endl
             << "testing 2......" << endl
             << endl;
        User user2("James");
        user2.change_passwd();
        user2.set_email();
        user2.print_info();
    
        User::print_n();
    }

     

     

     

  • 相关阅读:
    SQL游标操作每隔5分钟时间段数据统计信息
    win64位操作系统下安装pl/sql developer 并登录连接到oracle12c
    分科目统计每科前三名的学生
    merge源表数据移植到目标表新表数据中
    sqlserver表分区
    用SqlBulkCopy批量插入数据到SqlServer数据库表中
    SQL server插入数据后,如何获取自增长字段的值?
    Java创建线程的三种方式
    Java用户线程和守护线程
    Java虚拟机详解
  • 原文地址:https://www.cnblogs.com/qiansen/p/15430429.html
Copyright © 2011-2022 走看看