实验2 类和对象

实验任务1

t.h

 1 #ifndef T_H
 2 #define T_H
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 class T
 7 {
 8 public:
 9     T(int x=0,int y=0);
10     T(const T&t);
11     T(T &&t);
12     ~T();
13     void set_m1(int x);
14     int get_m1() const;
15     int get_m2() const;
16     void display() const;
17     friend void func();
18 private:
19     int m1,m2;
20 public:
21     static void disply_count();
22     static const string doc;
23     static const int max_count;
24 private:
25     static int count;
26 };
27 void func();
28 #endif
View Code

t.cpp

 1 #include "t1.h"
 2 const string T::doc{"a simple class"};
 3 const int T::max_count=99;
 4 int T::count=0;
 5 T::T(int x,int y):m1{x},m2{y}
 6 {
 7     ++count;
 8     cout<<"constructor called \n";
 9 }
10 T::T(const T &t):m1{t.m1},m2{t.m2}
11 {
12     ++count;
13     cout<<"copy construcyor called \n";
14 }
15 T::T(T &&t):m1{t.m1},m2{t.m2}
16 {
17     ++count;
18     cout<<"move constructor called \n";
19 }
20 T::~T()
21 {
22     --count;
23     cout<<"destructor called \n";
24 }
25 void T::set_m1(int x)
26 {
27     m1=x;
28 }
29 int T::get_m1() const
30 {
31     return m1;
32 
33 }
34 int T::get_m2() const
35 {
36     return m2;
37 }
38 void T::display() const
39 {
40     cout<<m1<<","<<m2<<endl;
41 }
42 void T::disply_count()
43 {
44     cout<<"T objects:"<<count<<endl;
45 }
46 void func()
47 {
48     T t1;
49     t1.set_m1(55);
50     t1.m2=77;
51     t1.display();
52 }
View Code

main.cpp

 1 #include "t1.h"
 2 void test()
 3 {
 4     cout << "T class info: " << T::doc << endl;
 5     cout << "T objects max_count: " << T::max_count << endl;
 6     T::disply_count();
 7     T t1;
 8     t1.display();
 9     t1.set_m1(42);
10     T t2{t1};
11     t2.display();
12     T t3{std::move(t1)};
13     t3.display();
14     t1.display();
15     T::disply_count();
16 }
17 int main()
18 {
19     cout<<"===========T=================="<<endl;
20     test();
21     cout<<endl;
22     cout<<"===================func()==============="<<endl;
23     func();
24 }
View Code

 

 

实验任务2

employee.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <string>
 4 #include <iomanip>
 5 using std::string;
 6 using std::cout;
 7 using std::endl;
 8 using std::setfill;
 9 using std::setw;
10 using std::left;
11 using std::right;
12 using std::to_string;
13 struct Date
14 {
15     int year;
16     int month;
17     int day;
18 };
19 class Employee
20 {
21 public:
22     Employee();
23     Employee(string name0, double salary0, int y, int m, int d = 1);
24     void set_info(string name0, double salary0, int y, int m, int d = 1);
25     string get_name() const;
26     double get_salary() const;
27     void display_info() const;
28     void update_salary(double s);
29     void update_hire_date(int y, int m, int d);
30     void raise_salary(double by_percent);
31 public:
32     static void display_count();
33     private:
34     string id;
35     string name;
36     double salary;
37     Date hire_date;
38 public:
39     static const string doc;
40 private:
41     static int count;
42 };
43 const string Employee::doc {"a simple Employee class"};
44 int Employee::count = 0;
45 Employee::Employee(): id{ to_string(count+1) }
46 {
47     ++count;
48 }
49 Employee::Employee(string name0, double salary0, int y, int m, int d):id{to_string(count+1)}, name{name0}, salary{salary0},
50 hire_date{y, m, d}
51 {
52     ++count;
53 }
54 void Employee::set_info(string name0, double salary0, int y, int m, int d)
55 {
56     name = name0;
57     salary = salary0;
58     hire_date.year = y;
59     hire_date.month = m;
60     hire_date.day = d;
61 }
62 string Employee::get_name() const
63 {
64     return name;
65 }
66 double Employee::get_salary() const
67 {
68     return salary;
69 }
70     void Employee::display_info() const {
71     cout << left << setw(15) << "id: " << id << endl;
72     cout << setw(15) << "name: " << name << endl;
73     cout << setw(15) << "salary: " << salary << endl;
74     cout << setw(15) << "hire_date: " << hire_date.year << "-";
75     cout << std::right << setfill('0') << setw(2) << hire_date.month << "-"<< setw(2) << hire_date.day;
76     cout << setfill(' ');
77 }
78 void Employee::update_salary(double s)
79 {
80     salary = s;
81 }
82 void Employee::update_hire_date(int y, int m, int d)
83 {
84     hire_date.year = y;
85     hire_date.month = m;
86     hire_date.day = d;
87 }
88 void Employee::raise_salary(double by_percent)
89 {
90     double raise = salary * by_percent / 100;
91     salary += raise;
92 }
93 void Employee::display_count()
94 {
95     cout << "there are " << count << " employees\n";
96 }
View Code

main.cpp

 1 #include "Employee.h"
 2 #include <iostream>
 3 void test()
 4 {
 5     using std::cout;
 6     using std::endl;
 7     cout << Employee::doc << endl << endl;
 8     Employee employee1;
 9     employee1.set_info("Sam", 30000, 2015, 1, 6);
10     employee1.update_hire_date(2019, 6, 30);
11     employee1.update_salary(35000);
12     employee1.display_info();
13     cout << endl << endl;
14     Employee employee2{"Tony", 20000, 2023, 3, 16};
15     employee2.raise_salary(15);
16     employee2.display_info();
17     cout << endl << endl;
18     Employee::display_count();
19 }
20 int main()
21 {
22     test();
23 }
View Code

 

 

实验任务3

complex.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <cmath>
 4 using namespace std;
 5 class Complex
 6 {
 7 public:
 8     Complex(double t = 0, double p = 0) :t(t), p(p) {}
 9     Complex(const Complex& a) ;
10     double get_real() { return t; }
11     double get_imag() const { return p; }
12     void show() const;
13     void add(const Complex &z);
14     ~Complex(){};
15     friend Complex add(const Complex &k,const Complex &l);
16     friend bool is_equal(const Complex &asd,const Complex &zxc);
17     friend double abs(const Complex &a);
18 
19 private:
20     double t, p;
21 };
View Code

complex.cpp

 1 #include "complex.hpp"
 2 Complex::Complex(const Complex& a)
 3 {
 4     t = a.t;
 5     p = a.p;
 6 }
 7 void Complex::show() const
 8 {
 9     if (p > 0)
10         cout << t << "+" << p << "i" ;
11     else if (p < 0)
12         cout << t << p << "i" ;
13     else
14         cout << t ;
15 }
16 void Complex::add(const Complex &z)
17 {
18     t = t + z.t;
19     p = p + z.p;
20 }
21 Complex add(const Complex &k,const Complex &l)
22 {
23     Complex s;
24     s.t=k.t+l.t;s.p=k.p+l.p;
25     return (s);
26 }
27 bool is_equal(const Complex &asd,const Complex &zxc)
28 {
29     if (asd.t == zxc.t && asd.p == zxc.p)
30         return (true);
31     else
32         return (false);
33 }
34 double abs(const Complex &a)
35 {
36     return(sqrt(a.t * a.t + a.p * a.p));
37 }
View Code

main.cpp

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

 

 

实验任务4

user.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 
 9 class User {
10 public:
11     User(string name,string passwd = "111111",string email = " "): a{name}, b{passwd}, c{email} {t++;}
12     ~User() = default;    
13  
14     void set_email();
15     void change_passwd();
16     void print_info();
17     static void print_n();
18  
19 private:
20     string a,b,c;
21     static int t;
22     
23 };
24  
25 int User::t = 0;
26 
27 void User::set_email()
28 {
29     cout << "Enter email address: " ;
30     cin >> c ;
31     cout << "email is set successfully..." << endl;
32 }
33  
34 void User::change_passwd()
35 {
36     cout << "Enter old password: ";
37     string temp;
38     int i = 3;
39     while(i)
40     {
41         cin >> temp;
42         if(temp == b)
43         {   
44             cout << "Enter new passwd: ";
45             cin >> b;  
46             cout << "new passwd is set successfully..." << endl;
47             break;
48         }
49         else 
50         {
51             i--;
52             if(i != 0)
53                 cout << "password input error. Please re-enter again: " ;
54         }
55         if(i == 0)
56         {
57             cout << "password input error. Please try after a while." << endl;
58         }
59     }
60  
61 }
62 
63 void User::print_info()
64 {   
65     string pp(b.size(), '*');
66     cout << "name:   " << a << endl;
67     cout << "passwd: " << pp << endl;
68     cout << "email:  " << c << endl;
69 }
70  
71 void User::print_n()
72 {
73     cout << "there are " << t << " users." << endl;
74 }
View Code

main.cpp

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

 

 

实验任务5

account.h

 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 };
View Code

account.cpp

 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 }
View Code

5_11.cpp

 1 #include "account.cpp"
 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 }
View Code

 

posted @ 2023-10-23 01:00  春满城  阅读(26)  评论(0)    收藏  举报