实验2 类和对象

实验任务3

complex.hpp
 1 # include<iostream>
 2 # include<cmath>
 3 
 4 class Complex{
 5 public:
 6 Complex(double r = 0, double i = 0){
 7 real = r;
 8 imag = i;
 9 };
10 Complex(const Complex &c){
11 real = c.real;
12 imag = c.imag;
13 };
14 ~Complex(){};
15 
16 double get_real() const {return real;};
17 double get_imag() const {return imag;};
18 
19 void add(const Complex &c);
20 void show() const;
21 
22 friend Complex add(const Complex &c1 , const Complex &c2);
23 friend bool is_equal(const Complex &c1 , const Complex &c2);
24 friend double abs(const Complex &c1);
25 
26 private:
27 double real;
28 double imag;
29 
30 };
31 
32 
33 void Complex::add(const Complex &c){
34 real += c.real;
35 imag += c.imag;
36 }
37 void Complex::show() const {
38 if (real == 0 && imag == 0) { std::cout << 0; }
39 else if (real == 0) { std::cout << imag << "i"; }
40 if(imag>0)
41 {std::cout << real << " + " << imag << "i"; }
42 else if(imag<0)
43 {std::cout << real << "-"<< -imag << "i"; }
44 else if(imag ==0)
45 {std::cout << real ; }
46 }
47 Complex add(const Complex &c1 , const Complex &c2){
48 double resultreal = c1.real + c2.real;
49 double resultimag = c1.imag + c2.imag;
50 return Complex (resultreal,resultimag);
51 }
52 bool is_equal(const Complex &c1 , const Complex &c2){
53 return (c1.real == c2.real)&&(c1.imag == c2.imag);
54 }
55 double abs(const Complex &c){
56 double x = sqrt(c.real*c.real + c.imag*c.imag);
57 return x;
58 }

 1 #include"complex.hpp"
 2 
 3 
 4 void test(){
 5 using namespace std;
 6 Complex c1(3,-4);
 7 const Complex c2(4.5);
 8 Complex c3(c1);
 9 cout << "c1 = ";
10 c1.show();
11 cout << endl;
12 cout << "c2 = ";
13 c2.show();
14 cout << endl;
15 cout << "c2.imag = " << c2.get_imag() << endl;
16 cout << "c3 = ";
17 c3.show();
18 cout << endl;
19 cout << "abs(c1) = ";
20 cout << abs(c1) << endl;
21 cout << boolalpha;
22 cout << "c1 == c3 : " << is_equal(c1,c3) << endl;
23 cout << "c1 == c2 : " << is_equal(c1,c2) << endl;
24 Complex c4;
25 c4 = add(c1,c2);
26 cout << "c4 = c1 + c2 = ";
27 c4.show();
28 cout << endl;
29 c1.add(c2);
30 cout << "c1 += c2, " << "c1 = ";
31 c1.show();
32 cout << endl;
33 }
34 
35 int main(){
36 test();
37 }
main.cpp

 

 

实验任务4

 1 #pragma once  
 2 #include <iostream>  
 3 #include <string>  
 4 using namespace std;  
 5   
 6 class User{  
 7 public:  
 8     User(std::string name, std::string passwd = "111111", std::string email = " "):name{name}, passwd{passwd}, email{email} {n++;}  
 9     void set_email();  
10     void change_passwd();  
11     void print_info();  
12     static void print_n();  
13   
14 private:  
15     string name;  
16     string passwd;  
17     string email;  
18     static int n;  
19 };  
20   
21 int User::n = 0;  
22   
23 void User::set_email(){  
24     cout << "Enter email address: ";  
25     cin >> email;  
26     cout << "email is set successfully..." << endl;  
27 }  
28 void User::change_passwd() {  
29     string p, a;  
30     cout << "Enter old password: ";  
31     cin >> p;  
32     int i = 0;  
33     while (p != passwd){  
34         i++;  
35         cout << "password input error. Please re-enter again: ";  
36         cin >> p;  
37         if (i>=2){  
38             cout << "password input error. Please try after a while" << endl;  
39             break;  
40         }  
41     }  
42     if (p == passwd){  
43         cout << "Enter new passwd: ";  
44         cin >> a;  
45         passwd = a;  // Add this line to update the password.  
46         cout << "new passwd is set successfully..." << endl;  
47     } else {  
48         cout << "The entered old password is incorrect." << endl;  
49     }    
50 }  
51 void User::print_info(){  
52     string s(passwd.size(), '*'); // This line is not necessary. You can remove it.   
53     // It seems like you are trying to mask the password with '*'. However, the variable 's' is not used anywhere.   
54     // You should directly use 'passwd' instead of 's'.   
55     cout << "name: " << name << endl;  
56     cout << "passwd: " << passwd << endl; // Use 'passwd' instead of 's'.   
57     cout << "email: " << email << endl;   
58 }   
59 void User::print_n(){   
60     cout << "there are " << n << "users" << endl;   
61 }
user.hpp
 1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 //测试IUser类
 5 void test(){
 6     using std::cout;
 7     using std::endl;
 8 
 9     cout << "testing 1......\n";
10     User user1("Jonny", "92197", "xyz@hotmail.com");
11     user1.print_info();
12 
13     cout << endl
14          << "testing 2......\n\n";
15 
16     User user2("Leonard");
17     user2.change_passwd();
18     user2.set_email();
19     user2.print_info();
20 
21     cout << endl;
22     User::print_n();
23 }
24 
25 int main(){
26     test();
27 }
main.cpp

 

 

实验任务5

 1 class SavingsAccount {
 2 private:
 3     int id;
 4     double balance;
 5     double rate;
 6     int lastDate;
 7     double accumulation;
 8     static double total;
 9     
10     void record(int date, double amount);
11     double accumulate(int date) const {
12         return accumulation+balance*(date-lastDate);
13     }
14 public:
15     SavingsAccount(int date, int id, double rate);
16     int getId() const {return id;}
17     double getBalance() const {return balance;}
18     double getRate() const {return rate;}
19     static double getTotal() {return total;}
20     void deposit(int data, double amount);
21     void withdraw(int data, double amount);
22     void settle(int data);
23     void show() const; 
24 };
account.h
 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total=0;
 7 SavingsAccount::SavingsAccount(int date, int id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 8     cout << date << "\t#" << id << "is created" << endl;
 9 }
10 void SavingsAccount::record(int date, double amount) {
11     accumulation = accumulate(date);
12     lastDate = date;
13     amount = floor(amount*100+0.5)/100;
14     balance += amount;
15     total += amount;
16     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 
17 }
18 void SavingsAccount::deposit(int date, double amount) {
19     record(date, amount);
20 }
21 void SavingsAccount::withdraw(int date, double amount) {
22     if(amount>getBalance())
23       cout << "Error: not enough money" << endl;
24     else
25       record(date, -amount);
26 }
27 void SavingsAccount::settle(int date) {
28     double interest = accumulate(date) * rate/365;
29     if(interest != 0)
30       record(date, interest);
31     accumulation = 0;
32 }
33 void SavingsAccount::show() const {
34     cout << "#" << id << "\tBalance: " << balance;
35 }
account.cpp
 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main() {
 6     SavingsAccount sa0(1, 21325302, 0.015);
 7     SavingsAccount sa1(1, 58320212, 0.015);
 8     
 9     sa0.deposit(5,5000);
10     sa1.deposit(25,10000);
11     sa0.deposit(45,5500);
12     sa1.withdraw(60,4000);
13     
14     sa0.settle(90);
15     sa1.settle(90);
16     
17     sa0.show(); cout << endl;
18     sa1.show(); cout << endl;
19     cout << "Total: " << SavingsAccount::getTotal() << endl;
20     return 0;
21 }
5_11.cpp

 

 

posted @ 2023-10-23 00:44  方艺博  阅读(28)  评论(0)    收藏  举报