1 #ifndef T_H
2 #define T_H
3
4 #include <iostream>
5 #include <string>
6
7 using namespace std;
8
9 // 类T的声明
10 class T {
11 public:
12 T(int x = 0, int y = 0); //带有默认形值的构造函数
13 T(const T &t); // 复制构造函数
14 T(T &&t); // 移动构造函数
15 ~T(); // 析构函数
16
17 void set_m1(int x); // 设置T类对象的数据成员m1
18 int get_m1() const; // 获取T类对象的数据成员m1
19 int get_m2() const; // 获取T类对象的数据成员m2
20 void display() const; // 显示T类对象的信息
21
22 friend void func(); // 声明func()为T类友元函数
23
24 private:
25 int m1, m2;
26
27 public:
28 static void display_count(); // 类方法,显示当前T类对象数目
29
30 public:
31 static const string doc; // 类属性,用于描述T类
32 static const int max_count; // 类属性,用于描述T类对象的上限
33
34 private:
35 static int count; // 类属性,用于描述当前T类对象数目
36 };
37
38 void func();
39
40 #endif
41 #define T_H
42
43 #include <iostream>
44 #include <string>
45
46 using namespace std;
47
48 // 类T的声明
49 class T {
50 public:
51 T(int x = 0, int y = 0); //带有默认形值的构造函数
52 T(const T &t); // 复制构造函数
53 T(T &&t); // 移动构造函数
54 ~T(); // 析构函数
55
56 void set_m1(int x); // 设置T类对象的数据成员m1
57 int get_m1() const; // 获取T类对象的数据成员m1
58 int get_m2() const; // 获取T类对象的数据成员m2
59 void display() const; // 显示T类对象的信息
60
61 friend void func(); // 声明func()为T类友元函数
62
63 private:
64 int m1, m2;
65
66 public:
67 static void display_count(); // 类方法,显示当前T类对象数目
68
69 public:
70 static const string doc; // 类属性,用于描述T类
71 static const int max_count; // 类属性,用于描述T类对象的上限
72
73 private:
74 static int count; // 类属性,用于描述当前T类对象数目
75 };
76
77 void func();
78
79 #endif
1 #include "t.h"
2
3 // 类的static数据成员: 类外初始化
4 const string T::doc{"a simple class"};
5 const int T::max_count = 99;
6 int T::count = 0;
7
8 // 类T的实现
9 T::T(int x, int y): m1{x}, m2{y} {
10 ++count;
11 cout << "constructor called.\n";
12 }
13
14 T::T(const T &t): m1{t.m1}, m2{t.m2}{
15 ++count;
16 cout << "copy constructor called.\n";
17 }
18
19 T::T(T &&t): m1{t.m1}, m2{t.m2}{
20 ++count;
21 cout << "move constructor called.\n";
22 }
23
24 T::~T(){
25 --count;
26 cout << "destructor called.\n";
27 }
28
29 void T::set_m1(int x) {
30 m1 = x;
31 }
32
33 int T::get_m1() const {
34 return m1;
35 }
36
37 int T::get_m2() const {
38 return m2;
39 }
40
41 void T::display() const {
42 cout << m1 << "," << m2 << endl;
43 }
44
45 // 类方法
46 void T::display_count() {
47 cout << "T objects:" << count << endl;
48 }
49
50 // 友元函数func(): 实现
51 void func(){
52 T t1;
53 t1.set_m1(55);
54 t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问
55 t1.display();
56 }
1 #include "t.h"
2
3 // 测试
4 void test(){
5 cout << "T class info: " << T::doc << endl;
6 cout << "T objects max_count: " << T::max_count << endl;
7 T::display_count();
8
9 T t1;
10 t1.display();
11 t1.set_m1(42);
12
13 T t2{t1};
14 t2.display();
15
16 T t3{std::move(t1)};
17 t3.display();
18 t1.display();
19
20 T::display_count();
21 }
22
23 // 主函数
24 int main(){
25 cout << "============测试类T============" << endl;
26 test();
27 cout << endl;
28
29 cout << "============测试友元函数func()============" << endl;
30 func();
31 }
![]()
1 #pragma once
2
3 //Employee类的定义
4 #include <iostream>
5 #include <string>
6 #include <iomanip>
7
8 using std::string;
9 using std::cout;
10 using std::endl;
11 using std::setfill;
12 using std::setw;
13 using std::left;
14 using std::right;
15 using std::to_string;
16
17 struct Data {
18 int year;
19 int month;
20 int day;
21 };
22
23 //Employee类的声明
24 class Employee
25 {
26 public:
27 Employee();
28 Employee(string name0, double salary0, int y, int m, int d = 1);
29 void set_info(string name0, double salary0, int y, int m, int d = 1);
30 //设置雇员信息
31 string get_name() const; //获取雇员姓名
32 double get_salary() const; //获取雇员薪水
33 void display_info() const; //显示雇员信息
34 void updata_salary(double s) ; //更新雇员薪水
35 void updata_hire_data(int y, int m, int d); //更新雇佣信息
36 void raise_salary(double by_percent); //计算提薪加成
37
38 public:
39 static void display_count(); //类方法,显示雇员总数
40
41 private:
42 string id; //雇员工号
43 string name; //雇员姓名
44 double salary; //雇员薪水
45 Data hire_data; //雇员雇佣日期
46
47 public:
48 static const string doc; //类属性,用于描述类
49
50 private:
51 static int count; //类属性,用于记录雇员总人数
52
53 };
54
55 const string Employee::doc {"a simple Employee class"};
56 int Employee::count = 0;
57
58 //默认构造函数
59 Employee::Employee(): id{ to_string(count+1)} {
60 ++count;
61 }
62
63 //带参数的构造函数
64 Employee::Employee(string name0, double salary0, int y, int m, int d) :
65 id{to_string(count+1)}, name{name0}, salary{salary0},hire_data{y, m, d} {
66 ++count;
67 }
68
69
70 //设置员工信息
71 void Employee::set_info(string name0, double salary0, int y, int m, int d){
72 name = name0;
73 salary = salary0;
74 hire_data.year = y;
75 hire_data.month = m;
76 hire_data.day = d;
77 }
78
79 //获取员工姓名
80 string Employee::get_name() const{
81 return name;
82 }
83
84 //获取员工薪水
85 double Employee::get_salary() const{
86 return salary;
87 }
88
89 //显示雇员信息
90 void Employee::display_info() const{
91 cout << left << setw(15) << "id: " << id <<endl;
92 cout << setw(15) << "name: " << name <<endl;
93 cout << setw(15) << "salary: " << salary <<endl;
94 cout << setw(15) << "hire_data: " << hire_data.year << "-";
95 cout << std::right << setfill('0') << setw(2) << hire_data.month << "-" <<setw(2) << hire_data.day;
96
97 cout << setfill(' '); //恢复到默认空格填充
98 }
99
100 //更新薪水
101 void Employee::updata_salary(double s) {
102 salary = s;
103 }
104
105 //更新雇佣日期
106 void Employee::updata_hire_data(int y, int m, int d) {
107 hire_data.year = y;
108 hire_data.month = m;
109 hire_data.day = d;
110 }
111
112 //雇员提薪加成
113 //by_percent是提升比例
114 void Employee::raise_salary(double by_percent) {
115 double raise = salary * by_percent/100;
116 salary += raise;
117 }
118
119 //类方法
120 //显示雇员总数
121 void Employee::display_count() {
122 cout << "there are" << count << " employee\n";
123 }
1 #include "Employee.hpp"
2 #include <iostream>
3
4 //测试:Employee类
5 void test() {
6 using std::cout;
7 using std::endl;
8
9 cout << Employee::doc << endl << endl;
10
11 Employee employee1;
12 employee1.set_info("Sam", 30000, 2015, 1, 6);
13 employee1.updata_hire_data(2019, 6, 30);
14 employee1.updata_salary(35000);
15 employee1.display_info();
16 cout << endl <<endl;
17
18 Employee employee2{"Tony", 20000, 2023, 3, 16};
19 employee2.raise_salary(15);
20 employee2.display_info();
21 cout << endl << endl;
22
23 Employee::display_count();
24 }
25
26 int main(){
27 test();
28 }
![]()
1 #pragma once
2
3 #include <iostream>
4 #include <cmath>
5
6 class Complex{
7 public:
8 Complex(double r = 0, double i = 0){
9 real = r;
10 imag = i;
11 }
12 Complex(const Complex &c){
13 real = c.real;
14 imag = c.imag;
15 }
16 double get_real() const{
17 return real;
18 }
19 double get_imag() const{
20 return imag;
21 }
22 void add(const Complex &c){
23 real += c.get_real();
24 imag += c.get_imag();
25 }
26 void show(){
27 std::cout << real;
28 if (imag>0) std::cout << "+" << abs(imag) << "i";
29 if (imag<0) std::cout << "-" << abs(imag) << "i";
30 }
31 void show() const{
32 std::cout << real;
33 if (imag>0) std::cout << "+" << abs(imag) << "i";
34 if (imag<0) std::cout << "-" << abs(imag) << "i";
35 }
36 friend Complex add(const Complex &c1, const Complex &c2);
37 friend bool is_equal(const Complex &c1, const Complex &c2);
38 friend double abs(const Complex &c);
39
40 private:
41 double real;
42 double imag;
43 };
44
45 Complex add(const Complex &c1, const Complex &c2){
46 double r, i;
47 r = c1.get_real() + c2.get_real();
48 i = c1.get_imag() + c2.get_imag();
49 Complex c(r, i);
50 return c;
51 }
52 bool is_equal(const Complex &c1, const Complex &c2){
53 bool r, i;
54 r = c1.get_real() == c2.get_real();
55 i = c1.get_imag() == c2.get_imag();
56 return r && i;
57 }
58 double abs(const Complex &c){
59 return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
60 }
1 #include "complex.hpp"
2
3 // 复数类Complex: 测试
4 void test() {
5 using namespace std;
6
7 Complex c1(3, -4);
8 const Complex c2(4.5);
9 Complex c3(c1);
10
11 cout << "c1 = ";
12 c1.show();
13 cout << endl;
14
15 cout << "c2 = ";
16 c2.show();
17 cout << endl;
18 cout << "c2.imag = " << c2.get_imag() << endl;
19
20 cout << "c3 = ";
21 c3.show();
22 cout << endl;
23
24 cout << "abs(c1) = ";
25 cout << abs(c1) << endl;
26
27 cout << boolalpha;
28 cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
29 cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
30
31 Complex c4;
32 c4 = add(c1, c2);
33 cout << "c4 = c1 + c2 = ";
34 c4.show();
35 cout <<endl;
36
37 c1.add(c2);
38 cout << " c1 += c2, " << " c1 = ";
39 c1.show();
40 cout << endl;
41 }
42
43 int main(){
44 test();
45 }
![]()
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 << "passwword 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 cout << "new passwd is set successfully...";
46 }
47 }
48 void User::print_info(){
49 string s(passwd.size(), '*');
50 cout << "name: " << name << endl;
51 cout << "passwd: " << s << endl;
52 cout << "email: " << email << endl;
53 }
54 void User::print_n(){
55 cout << "there are " << n << "users" << endl;
56 }
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 }
![]()
1 #ifndef ACCOUNT_H
2 #define ACCOUNT_H
3 class SavingsAccount { //储蓄账户类
4 private:
5 int id; //账号
6 double balance; //余额
7 double rate; //存款的年利率
8 int lastDate; //上次变更余额的时期
9 double accumulation; //余额按日累加之和
10 static double total; //所有账户的总金额
11 //记录一笔账,date为日期,amount为金额,desc为说明
12 void record(int date, double amount);
13 //获得到指定日期为止的存款金额按日积累值
14 double accumulate(int date) const{
15 return accumulation+balance*(date - lastDate);
16 }
17 public:
18 //构造函数
19 SavingsAccount(int date, int id, double rate);
20 int getId() const {return id;}
21 double getBlance() const {return balance;}
22 double getRate() const {return rate;}
23 static double getTotal() {return total;}
24 void deposit(int date, double amount); //存入现金
25 void withdraw(int date, double amount); //取出现金
26 //结算利息,每年1月1日调用一次该函数
27 void settle(int date);
28 //显示账户信息
29 void show() const;
30 };
31 #endif //_ _ACCOUNT_H_ _
1 #include "account.h"
2 #include <cmath>
3 #include <iostream>
4 using namespace std;
5
6 double SavingsAccount::total = 0;
7 //SavingsAccount类相关成员函数的实现
8 SavingsAccount::SavingsAccount(int date, int id, double rate):id(id), balance(0), rate(rate), lastDate(date), accumulation(0){
9 cout << date << "\t#" << id << "is created" << endl;
10 }
11 void SavingsAccount::record(int date, double amount){
12 accumulation = accumulate(date);
13 lastDate = date;
14 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
15 balance += amount;
16 total += amount;
17 cout << date << "\t#" << id << "\t" << amount << "\t" << balance <<endl;
18 }
19 void SavingsAccount::deposit(int date, double amount) {
20 record(date, amount);
21 }
22 void SavingsAccount::withdraw(int date, double amount){
23 if (amount > getBlance())
24 cout << "Error: not enough money" << endl;
25 else
26 record(date, -amount);
27 }
28 void SavingsAccount::settle(int date){
29 double interest = accumulate(date) * rate/365; //计算年息
30 if (interest != 0)
31 record(date, interest);
32 accumulation = 0;
33 }
34 void SavingsAccount::show() const {
35 cout << "#" << id << "\tBlance:" << balance;
36 }
1 #include "account.h"
2 #include <iostream>
3 using namespace std;
4 int main(){
5 //建立几个账户
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 //开户后第90天到了银行的计息日,结算所有账户的年息
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 }
![]()