• 实验1 类与对象


    实验任务三

    Complex.hpp

     1 #include<iostream>
     2 #include<cmath>
     3 using namespace std;
     4 class Complex{
     5     private:
     6         double real, imag;
     7     public:
     8         Complex(double r = 0, double i = 0):real(r), imag(i){}
     9         Complex(const Complex & c){real = c.real; imag = c.imag;}
    10         double get_real()const{return real;}
    11         double get_imag()const{return imag;}
    12         void show()const;
    13         void add(const Complex &c);
    14         friend Complex add(const Complex &c1, const Complex &c2);
    15         friend bool is_equal(const Complex &c1, const Complex &c2);
    16         friend double abs(const Complex &c);
    17 }; 
    18 
    19 void Complex::show()const{
    20     if(imag > 0)
    21         cout << real << " + " <<imag << "i";
    22     else if(imag < 0)
    23         cout << real <<" - " << -1*imag << "i";
    24     else
    25         cout << real;
    26 }
    27 void Complex::add(const Complex &c){
    28     real += c.real;
    29     imag += c.imag;
    30 }
    31 Complex add(const Complex &c1, const Complex &c2){
    32     Complex c;
    33     c.real = c1.real + c2.real;
    34     c.imag = c1.imag + c2.imag;
    35     return c;
    36 } 
    37 bool is_equal(const Complex &c1, const Complex &c2){
    38     if(c1.real==c2.real && c1.imag==c2.imag)
    39         return true;
    40     return false;
    41 }
    42 double abs(const Complex &c){
    43     double s = sqrt(c.real * c.real + c.imag * c.imag);
    44     return s;
    45 }

    task3.cpp

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

    User.hpp

     1 #include<iostream>
     2 #include<cstring> 
     3 using namespace std;
     4 
     5 class User{
     6     private:
     7         string name;
     8         string passwd;
     9         string email;
    10         static int n;
    11     public:
    12         User(string name0, string passwd0 = "111111", string email0 = ""):
    13             name(name0), passwd(passwd0), email(email0){ ++n;}
    14         void set_email();
    15         void change_passwd();
    16         void print_info()const;
    17         static void print_n();
    18 };
    19 int User::n = 0;
    20 
    21 void User::set_email(){
    22     cout << "Enter email address:";
    23     cin >> email;
    24     cout << "email is set successfully..." << endl;
    25 }
    26 
    27 void User::change_passwd(){
    28     string p;
    29     cout << "Enter old password: ";
    30     for(int i=0; i<3; i++){
    31         cin >> p;
    32         if(p==passwd){
    33             cout << "Enter new password: ";
    34             cin >> passwd;
    35             cout << "new password is set successfully..." << endl;
    36             break;
    37         }
    38         else{
    39             cout << "password input error.";
    40             if(i==0||i==1)
    41                 cout << " Please re-enter again: ";
    42             else
    43                 cout << "Please try after a while." << endl;
    44         }
    45     }    
    46 }
    47 
    48 void User::print_info()const{
    49     cout << "name: " << name << endl;
    50     cout << "password: ******" << endl;
    51     cout << "email: " << email << endl;
    52     
    53 }
    54 
    55 void User::print_n(){
    56     if(n == 1)
    57         cout << "there is " << n << " user." << endl;
    58     else
    59         cout << "there are " << n << " users." << endl;
    60 }

    task4.cpp

     1 #include "User.hpp"
     2 #include <iostream>
     3 
     4 int main()
     5 {
     6     using namespace std;
     7 
     8     cout << "testing 1......" << endl;
     9     User user1("Jonny", "92197", "xyz@hotmail.com");
    10     user1.print_info();
    11 
    12     cout << endl
    13          << "testing 2......" << endl
    14          << endl;
    15     User user2("Leonard");
    16     user2.change_passwd();
    17     user2.set_email();
    18     user2.print_info();
    19 
    20     User::print_n();
    21 }

    实验结果

     

     

     

  • 相关阅读:
    Salesforce 大量数据部署的最佳实践
    Salesforce 数据备份和恢复小知识
    Salesforce 导入导出数据简介
    GDPR 和个人信息保护的小知识
    代码审查要点简介
    Salesforce 外部对象
    Python scipy.sparse.csr_matrix()[csc_matrix()]
    Deep Compression
    Pascal voc api
    Python内置模块-xml模块
  • 原文地址:https://www.cnblogs.com/wjxing/p/15456231.html
Copyright © 2020-2023  润新知