实验任务1
方式1
main.cpp
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::disply_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::disply_count(); 21 } 22 23 // 主函数 24 int main() { 25 cout << "============测试类T============" << endl; 26 test(); 27 cout << endl; 28 29 cout << "============测试友元函数func()============" << endl; 30 func(); 31 }View Code
t.cpp
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::disply_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 }View Code
t.h
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 disply_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 #endifView Code
运行测试结果
方式2
t.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 // 类T的声明 9 class T { 10 public: 11 T(int x = 0, int y = 0); // 带有默认形值的构造函数 12 T(const T &t); // 复制构造函数 13 T(T &&t); // 移动构造函数 14 ~T(); // 析构函数 15 16 void set_m1(int x); // 设置T类对象的数据成员m1 17 int get_m1() const; // 获取T类对象的数据成员m1 18 int get_m2() const; // 获取T类对象的数据成员m2 19 void display() const; // 显示T类对象的信息 20 21 friend void func(); // 声明func()为T类友元函数 22 23 private: 24 int m1, m2; 25 26 public: 27 static void disply_count(); // 类方法,显示当前T类对象数目 28 29 public: 30 static const string doc; // 类属性,用于描述T类 31 static const int max_count; // 类属性,用于描述T类对象的上限 32 33 private: 34 static int count; // 类属性,用于描述当前T类对象数目 35 }; 36 37 // 类的static数据成员:类外初始化 38 const string T::doc{"a simple class"}; 39 const int T::max_count = 99; 40 int T::count = 0; 41 42 // 类T的实现 43 T::T(int x, int y): m1{x}, m2{y} { 44 ++count; 45 cout << "constructor called.\n"; 46 } 47 48 T::T(const T &t): m1{t.m1}, m2{t.m2} { 49 ++count; 50 cout << "copy constructor called.\n"; 51 } 52 53 T::T(T &&t): m1{t.m1}, m2{t.m2} { 54 ++count; 55 cout << "move constructor called.\n"; 56 } 57 58 T::~T() { 59 --count; 60 cout << "destructor called.\n"; 61 } 62 63 void T::set_m1(int x) { 64 m1 = x; 65 } 66 67 int T::get_m1() const { 68 return m1; 69 } 70 71 int T::get_m2() const { 72 return m2; 73 } 74 75 void T::display() const { 76 cout << m1 << ", " << m2 << endl; 77 } 78 79 // 类方法 80 void T::disply_count() { 81 cout << "T objects: " << count << endl; 82 } 83 84 // 函数func():实现 85 void func() { 86 T t1; 87 t1.set_m1(55); 88 t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问 89 t1.display(); 90 }View Code
main.cpp
1 #include "t.hpp" 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::disply_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::disply_count(); 21 } 22 23 // 主函数 24 int main() { 25 cout << "============测试类T============" << endl; 26 test(); 27 cout << endl; 28 29 cout << "============测试友元函数func()============" << endl; 30 func(); 31 }View Code
运行测试结果
实验任务2
employee.hpp
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 Date { 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 update_salary(double s); // 更新雇员薪水 35 void update_hire_date(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 Date hire_date; // 雇员雇佣日期 46 47 public: 48 static const string doc; // 类属性,用于描述类 49 50 private: 51 static int count; // 类属性,用于记录雇员总人数 52 53 }; 54 const string Employee::doc {"a simple Employee class"}; 55 int Employee::count = 0; 56 57 // 默认构造函数 58 Employee::Employee(): id{ to_string(count+1) } { 59 ++count; 60 } 61 62 // 带参数的构造函数 63 Employee::Employee(string name0, double salary0, int y, int m, int d): 64 id{to_string(count+1)}, name{name0}, salary{salary0}, 65 hire_date{y, m, d} { 66 ++count; 67 } 68 69 // 设置员工信息 70 void Employee::set_info(string name0, double salary0, int y, int m, int d) 71 { 72 name = name0; 73 salary = salary0; 74 hire_date.year = y; 75 hire_date.month = m; 76 hire_date.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_date: " << hire_date.year << "-"; 95 cout << std::right << setfill('0') << setw(2) << hire_date.month << "-"<< setw(2) << hire_date.day; 96 97 cout << setfill(' '); // 恢复到默认空格填充 98 } 99 100 // 更新薪水 101 void Employee::update_salary(double s) { 102 salary = s; 103 } 104 105 // 更新雇佣日期 106 void Employee::update_hire_date(int y, int m, int d) { 107 hire_date.year = y; 108 hire_date.month = m; 109 hire_date.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 << " employees\n"; 123 }View Code
main.cpp
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.update_hire_date(2019, 6, 30); 14 employee1.update_salary(35000); 15 employee1.display_info(); 16 cout << endl << endl; 17 18 Employee employee2{"Tony", 20000, 2023, 3, 16}; 19 employee2.raise_salary(15); // 提成15% 20 employee2.display_info(); 21 cout << endl << endl; 22 23 Employee::display_count(); 24 25 } 26 27 int main() { 28 test(); 29 }View Code
运行测试结果
实验任务3
t.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cmath> 5 6 using namespace std; 7 // 复数类Complex:定义 8 // 待补足 9 // ××× 10 class Complex{ 11 private: 12 double real, imag; 13 public: 14 Complex(double real = 0, double imag = 0); 15 Complex(const Complex &c); 16 ~Complex(); 17 18 double get_real() const; 19 double get_imag() const; 20 void show() const; 21 void add(const Complex &c); 22 23 friend Complex add(const Complex &c1, const Complex &c2); 24 friend bool is_equal(const Complex &c1, const Complex &c2); 25 friend double abs(const Complex &c); 26 }; 27 28 Complex::Complex(double real, double imag): real{real}, imag{imag} { 29 } 30 31 Complex::Complex(const Complex &c): real{c.real}, imag{c.imag} { 32 } 33 34 Complex::~Complex() { 35 } 36 37 double Complex::get_real() const { 38 return real; 39 } 40 41 double Complex::get_imag() const { 42 return imag; 43 } 44 45 void Complex::show() const { 46 if(imag > 0) 47 cout << real << "+" << imag << "i" << endl; 48 if(imag < 0) 49 cout << real << imag << "i" << endl; 50 if(imag == 0) 51 cout << real << endl; 52 } 53 54 void Complex::add(const Complex &c) { 55 real += c.real; 56 imag += c.imag; 57 } 58 59 Complex add(const Complex &c1, const Complex &c2) { 60 Complex c; 61 c.real = c1.real + c2.real; 62 c.imag = c1.imag + c2.imag; 63 return c; 64 } 65 66 bool is_equal(const Complex &c1, const Complex &c2) { 67 if(c1.real == c2.real && c1.imag == c2.imag) 68 return true; 69 else 70 return false; 71 } 72 73 double abs(const Complex &c) { 74 return sqrt(c.real * c.real + c.imag * c.imag); 75 }View Code
main.cpp
1 #include "t.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 }View Code
运行测试结果
实验任务4
user.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 7 using namespace std; 8 9 class User 10 { 11 public: 12 User(string name, string password = "111111", string email = " "); 13 ~User(); 14 15 void set_email(); 16 void change_passwd(); 17 void print_info(); 18 19 20 private: 21 string name; 22 string password; 23 string email; 24 25 private: 26 static int n; 27 28 public: 29 static void print_n(); 30 }; 31 32 User::User(string name, string password, string email): name{name}, password{password}, email{email} { 33 ++n; 34 } 35 36 User::~User() { 37 --n; 38 } 39 40 void User::set_email() { 41 string email1; 42 cout << "Enter email address: "; 43 cin >> email1; 44 email = email1; 45 cout << "email is set successfully..." << endl; 46 } 47 48 void User::change_passwd() { 49 string password1, password2; 50 int count = 0; 51 cout << "Enter old password: "; 52 cin >> password1; 53 while(password1 != password) 54 { 55 count++; 56 cout << "password input error. Please re-enter again: "; 57 cin >> password1; 58 if(count >= 2) 59 { 60 cout << "password input error. Please try after a while." << endl; 61 break; 62 } 63 } 64 if(password1 == password) 65 { 66 cout << "Enter new password: "; 67 cin >> password2; 68 password = password2; 69 cout << "new password is set successfully..." << endl; 70 } 71 } 72 73 void User::print_info() { 74 string s (password.size(), '*'); 75 cout << "name: " << name << endl; 76 cout << "passwd: " << s << endl; 77 cout << "email: " << email << endl; 78 } 79 80 int User::n = 0; 81 82 void User::print_n() { 83 cout << "there are " << n << " users " << endl; 84 }View Code
task4.cpp
1 #include "User.hpp" 2 #include <iostream> 3 4 // 测试User类 5 void test() { 6 using std::cout; 7 using std::endl; 8 9 cout << "testing 1......\n"; 10 User user1("Jonny", "92197", "[email protected]"); 11 user1.print_info(); 12 13 cout << endl << "testing 2......\n\n"; 14 15 User user2("Leonard"); 16 user2.print_info(); 17 user2.change_passwd(); 18 user2.set_email(); 19 user2.print_info(); 20 cout << endl; 21 User::print_n(); 22 } 23 24 int main(){ 25 test(); 26 }View Code
运行测试结果
测试1
测试2
实验任务5
5_11.cpp
1 #include "account.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() { 7 SavingsAccount sa0(1, 21325302, 0.015); 8 SavingsAccount sa1(1, 58320212, 0.015); 9 10 sa0.deposit(5, 5000); 11 sa1.deposit(25, 10000); 12 sa0.deposit(45, 5500); 13 sa1.withdraw(60, 4000); 14 sa0.settle(90); 15 sa1.settle(90); 16 sa0.show(); cout << endl; 17 sa1.show(); cout << endl; 18 cout << "Total: " << SavingsAccount::getTotal() << endl; 19 return 0; 20 21 }View Code
account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 4 class SavingsAccount { 5 private: 6 int id; 7 double balance; 8 double rate; 9 int lastDate; 10 double accumulation; 11 static double total; 12 13 void record(int date, double amount); 14 15 double accumulate(int data) const { 16 return accumulation + balance * (data - lastDate); 17 } 18 19 public: 20 SavingsAccount(int date, int id, double rate); 21 int getId() const {return id;} 22 double getBalance() const {return balance;} 23 double getRate() const {return rate;} 24 static double getTotal() {return total;} 25 void deposit(int date, double amount); 26 void withdraw(int date, double amount); 27 void settle(int date); 28 void show() const; 29 }; 30 31 #endifView Code
account.cpp
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 5 using namespace std; 6 7 double SavingsAccount::total = 0; 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 12 void SavingsAccount::record(int date, double amount) { 13 accumulation = accumulate(date); 14 lastDate = date; 15 amount = floor(amount * 100 + 0.5) / 100; 16 balance += amount; 17 total += amount; 18 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 19 } 20 21 void SavingsAccount::deposit(int date, double amount) { 22 record(date, amount); 23 } 24 25 void SavingsAccount::withdraw(int date, double amount) { 26 if(amount > getBalance()) 27 cout << "Error: not enough money" << endl; 28 else 29 record(date, -amount); 30 } 31 32 void SavingsAccount::settle(int date) { 33 double interest = accumulate(date) * rate / 365; 34 if(interest != 0) 35 record(date, interest); 36 accumulation = 0; 37 } 38 39 void SavingsAccount::show() const { 40 cout << "#" << id << "\tBalance: " << balance; 41 }View Code
运行测试结果
标签:const,string,对象,double,void,编程,int,Complex,实验 From: https://www.cnblogs.com/rssn/p/17767015.html