task1
t.hpp
1 #pragma once 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 class T { 7 public: 8 T(int x = 0, int y = 0); 9 T(const T &t); 10 T(T &&t); 11 ~T(); 12 void set_m1(int x); 13 int get_m1() const; 14 int get_m2() const; 15 void display() const; 16 friend void func(); 17 private: 18 int m1, m2; 19 public: 20 static void disply_count(); 21 public: 22 static const string doc; 23 static const int max_count; 24 private: 25 static int count; 26 }; 27 28 const string T::doc{"a simple class"}; 29 const int T::max_count = 99; 30 int T::count = 0; 31 32 T::T(int x, int y): m1{x}, m2{y} { 33 ++count; 34 cout << "constructor called.\n"; 35 } 36 T::T(const T &t): m1{t.m1}, m2{t.m2} { 37 ++count; 38 cout << "copy constructor called.\n"; 39 } 40 T::T(T &&t): m1{t.m1}, m2{t.m2} { 41 ++count; 42 cout << "move constructor called.\n"; 43 } 44 T::~T() { 45 --count; 46 cout << "destructor called.\n"; 47 } 48 void T::set_m1(int x) { 49 m1 = x; 50 } 51 int T::get_m1() const { 52 return m1; 53 } 54 int T::get_m2() const { 55 return m2; 56 } 57 void T::display() const { 58 cout << m1 << ", " << m2 << endl; 59 } 60 61 void T::disply_count() { 62 cout << "T objects: " << count << endl; 63 } 64 void func() { 65 T t1; 66 t1.set_m1(55); 67 t1.m2 = 77; 68 t1.display(); 69 }View Code
main.cpp
1 #include "t.hpp" 2 3 void test() { 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 18 int main() { 19 cout << "============测试类T============" << endl; 20 test(); 21 cout << endl; 22 cout << "============测试友元函数func()============" << endl; 23 func(); 24 }View Code
运行结果截图
task2
employee.hpp1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <iomanip> 6 using std::string; 7 using std::cout; 8 using std::endl; 9 using std::setfill; 10 using std::setw; 11 using std::left; 12 using std::right; 13 using std::to_string; 14 struct Date { 15 int year; 16 int month; 17 int day; 18 }; 19 20 class Employee 21 { 22 public: 23 Employee(); 24 Employee(string name0, double salary0, int y, int m, int d = 1); 25 void set_info(string name0, double salary0, int y, int m, int d = 1); 26 27 string get_name() const; 28 double get_salary() const; 29 void display_info() const; 30 void update_salary(double s); 31 void update_hire_date(int y, int m, int d); 32 void raise_salary(double by_percent); 33 public: 34 static void display_count(); 35 private: 36 string id; 37 string name; 38 double salary; 39 Date hire_date; 40 public: 41 static const string doc; 42 private: 43 static int count; 44 }; 45 const string Employee::doc {"a simple Employee class"}; 46 int Employee::count = 0; 47 48 Employee::Employee(): id{ to_string(count+1) } { 49 ++count; 50 } 51 52 Employee::Employee(string name0, double salary0, int y, int m, int d): 53 id{to_string(count+1)}, name{name0}, salary{salary0}, 54 hire_date{y, m, d} { 55 ++count; 56 } 57 58 void Employee::set_info(string name0, double salary0, int y, int m, int d) 59 { 60 name = name0; 61 salary = salary0; 62 hire_date.year = y; 63 hire_date.month = m; 64 hire_date.day = d; 65 } 66 67 string Employee::get_name() const { 68 return name; 69 } 70 71 double Employee::get_salary() const { 72 return salary; 73 } 74 75 void Employee::display_info() const { 76 cout << left << setw(15) << "id: " << id << endl; 77 cout << setw(15) << "name: " << name << endl; 78 cout << setw(15) << "salary: " << salary << endl; 79 cout << setw(15) << "hire_date: " << hire_date.year << "-"; 80 cout << std::right << setfill('0') << setw(2) << hire_date.month << "-" 81 << setw(2) << hire_date.day; 82 cout << setfill(' '); 83 } 84 85 void Employee::update_salary(double s) { 86 salary = s; 87 } 88 89 void Employee::update_hire_date(int y, int m, int d) { 90 hire_date.year = y; 91 hire_date.month = m; 92 hire_date.day = d; 93 } 94 95 void Employee::raise_salary(double by_percent) { 96 double raise = salary * by_percent / 100; 97 salary += raise; 98 } 99 100 void Employee::display_count() { 101 cout << "there are " << count << " employees\n"; 102 }View Code
main.cpp
1 #include "Employee.hpp" 2 #include <iostream> 3 4 void test() { 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 test(); 22 }View Code
运行结果截图
task3
Complex.hpp
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include<iostream> 5 #include<cmath> 6 7 using namespace std; 8 9 class Complex { 10 private: 11 double real; 12 double imag; 13 public: 14 Complex(); 15 Complex(double real0); 16 Complex(double real0,double imag0); 17 Complex(const Complex &obj); 18 19 double get_real()const; 20 double get_imag()const; 21 void show()const; 22 void add(const Complex &obj); 23 24 friend Complex add(Complex obj1,Complex obj2); 25 friend bool is_equal(const Complex &obj1,const Complex &obj2); 26 friend double abs(const Complex &obj); 27 }; 28 29 Complex::Complex() { 30 real=imag=0; 31 } 32 Complex::Complex(double real0) { 33 real=real0; 34 imag=0; 35 } 36 Complex::Complex(double real0,double imag0) { 37 real=real0; 38 imag=imag0; 39 } 40 Complex::Complex(const Complex &obj) { 41 real=obj.real; 42 imag=obj.imag; 43 } 44 45 double Complex::get_real()const { 46 return real; 47 } 48 double Complex::get_imag()const { 49 return imag; 50 } 51 void Complex::show()const { 52 if(imag!=0) 53 cout<<real<<imag<<"i"; 54 else 55 cout<<real; 56 } 57 void Complex::add(const Complex &obj) { 58 real=real+obj.real; 59 imag=imag+obj.imag; 60 } 61 62 Complex add(Complex obj1,Complex obj2) { 63 obj1.real=obj1.real+obj2.real; 64 obj1.imag=obj1.imag+obj2.imag; 65 return obj1; 66 } 67 bool is_equal(const Complex &obj1,const Complex &obj2) { 68 if(obj1.real==obj2.real&&obj1.imag==obj2.imag) return true; 69 else return false; 70 } 71 double abs(const Complex &obj) { 72 return sqrt(pow(obj.real,2)+pow(obj.imag,2)); 73 } 74 75 #endifView Code
Complex.cpp
1 #include"Complex.hpp" 2 #include<iostream> 3 int main() { 4 using namespace std; 5 6 Complex c1(3, -4); 7 const Complex c2(4.5); 8 Complex c3(c1); 9 10 cout << "c1 = "; 11 c1.show(); 12 cout << endl; 13 14 cout << "c2 = "; 15 c2.show(); 16 cout << endl; 17 cout << "c2.imag = " << c2.get_imag() << endl; 18 19 cout << "c3 = "; 20 c3.show(); 21 cout << endl; 22 23 cout << "abs(c1) = "; 24 cout << abs(c1) << endl; 25 26 cout << boolalpha; 27 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 28 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 29 30 Complex c4; 31 c4 = add(c1, c2); 32 cout << "c4 = c1 + c2 = "; 33 c4.show(); 34 cout << endl; 35 36 c1.add(c2); 37 cout << "c1 += c2, " << "c1 = "; 38 c1.show(); 39 cout << endl; 40 }View Code
运行结果截图
task4 User.hpp
1 #ifndef USER_HPP 2 #define USER_HPP 3 4 #include<iostream> 5 #include<string> 6 7 using namespace std; 8 9 class User { 10 private: 11 string name; 12 string passwd; 13 string email; 14 static int n; 15 public: 16 User(string name0); 17 User(string name0,string passwd0,string email0); 18 void change_passwd(); 19 void set_email(); 20 void print_info(); 21 static void print_n(); 22 }; 23 24 int User::n=0; 25 26 User::User(string name0) { 27 name=name0; 28 passwd="12345"; 29 email=""; 30 n++; 31 } 32 User::User(string name0,string passwd0,string email0) { 33 name=name0; 34 passwd=passwd0; 35 email=email0; 36 n++; 37 } 38 39 void User::set_email() { 40 cout<<"Enter email address: "; 41 cin>>email; 42 cout<<"email is set successfully..."<<endl; 43 } 44 void User::change_passwd() { 45 cout<<"Enter old password: "; 46 string s; 47 cin>>s; 48 int i; 49 for(i=1; i<=2; i++) { 50 if(s==passwd) break; 51 cout<<"password input error.Please re-enter again: "; 52 cin>>s; 53 } 54 if(s!=passwd) cout<<"password input error. Please try after a while."<<endl; 55 else { 56 cout<<"Enter new passwd: "; 57 cin>>s; 58 passwd=s; 59 cout<<"new passwd is set successfully..."<<endl; 60 } 61 } 62 63 void User::print_info() { 64 cout<<"name: "<<name<<endl; 65 cout<<"passwd: "<<"******"<<endl; 66 cout<<"email: "<<email<<endl; 67 } 68 69 void User::print_n() { 70 cout<<"there are "<<n<<" users."<<endl; 71 } 72 73 #endifView Code
task4.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", "[email protected]"); 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
运行结果截图
task5 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 void record(int data,double amount); 13 double accumulate(int date)const 14 { 15 return accumulation+balance*(date-lastDate); 16 } 17 public: 18 SavingsAccount(int data,int id,double rate); 19 int getId()const{return id;} 20 double getBalance()const{return balance;} 21 double getRate()const{return rate;} 22 static double getTotal(){return total;} 23 void deposit(int date,double amount); 24 void withdraw(int data,double amount); 25 void settle(int date); 26 void show() const; 27 }; 28 #endifView 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 { 9 cout<<date<<"\t#"<<id<<"is created"<<endl; 10 } 11 void SavingsAccount::record(int date,double amount) 12 { 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 void SavingsAccount::deposit(int date,double amount) 21 { 22 record(date,amount); 23 } 24 void SavingsAccount::withdraw(int date,double amount) 25 { 26 if(amount>getBalance()) 27 cout<<"Error:not enough money"<<endl; 28 else 29 record(date,-amount); 30 } 31 void SavingsAccount::settle(int data) 32 { 33 double interest=accumulate(data)*rate/365; 34 if(interest!=0) 35 record(data,interest); 36 accumulation=0; 37 } 38 void SavingsAccount::show() const 39 { 40 cout<<"#"<<id<<"\tBalance:"<<balance; 41 }View Code
5_11.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 }View Code
运行结果截图
标签:const,string,对象,double,void,编程,int,Complex,实验 From: https://www.cnblogs.com/chenxiaolong202083290491/p/17780251.html