实验二
任务1
方式1
1 #ifndef T_H 2 #define T_H 3 #include <iostream> 4 #include <string> 5 using namespace std; 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 void func(); 28 #endif
1 #include "t.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 ++count; 7 cout << "constructor called.\n"; 8 } 9 T::T(const T &t): m1{t.m1}, m2{t.m2} { 10 ++count; 11 cout << "copy constructor called.\n"; 12 } 13 T::T(T &&t): m1{t.m1}, m2{t.m2} { 14 ++count; 15 cout << "move constructor called.\n"; 16 } 17 T::~T() { 18 --count; 19 cout << "destructor called.\n"; 20 } 21 void T::set_m1(int x) { 22 m1 = x; 23 } 24 int T::get_m1() const { 25 return m1; 26 } 27 int T::get_m2() const { 28 return m2; 29 } 30 void T::display() const { 31 cout << m1 << ", " << m2 << endl; 32 } 33 34 void T::disply_count() { 35 cout << "T objects: " << count << endl; 36 } 37 38 void func() { 39 T t1; 40 t1.set_m1(55); 41 t1.m2 = 77; 42 t1.display(); 43 }
1 #include "t.h" 2 void test() { 3 cout << "T class info: " << T::doc << endl; 4 cout << "T objects max_count: " << T::max_count << endl; 5 T::disply_count(); 6 T t1; 7 t1.display(); 8 t1.set_m1(42); 9 T t2{t1}; 10 t2.display(); 11 T t3{std::move(t1)}; 12 t3.display(); 13 t1.display(); 14 T::disply_count(); 15 } 16 int main() { 17 cout << "============测试类T============" << endl; 18 test(); 19 cout << endl; 20 cout << "============测试友元函数func()============" << endl; 21 func(); 22 }

方式2
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 65 void func() { 66 T t1; 67 t1.set_m1(55); 68 t1.m2 = 77; 69 t1.display(); 70 }
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 }

任务2
#pragma once #include <iostream> #include <string> #include <iomanip> using std::string; using std::cout; using std::endl; using std::setfill; using std::setw; using std::left; using std::right; using std::to_string; struct Date { int year; int month; int day; }; class Employee { public: Employee(); Employee(string name0, double salary0, int y, int m, int d = 1); void set_info(string name0, double salary0, int y, int m, int d = 1); string get_name() const; double get_salary() const; void display_info() const; void update_salary(double s); void update_hire_date(int y, int m, int d); void raise_salary(double by_percent); public: static void display_count(); private: string id; string name; double salary; Date hire_date; public: static const string doc; private: static int count; }; const string Employee::doc {"a simple Employee class"}; int Employee::count = 0; Employee::Employee(): id{ to_string(count+1) } { ++count; } Employee::Employee(string name0, double salary0, int y, int m, int d): id{to_string(count+1)}, name{name0}, salary{salary0}, hire_date{y, m, d} { ++count; } void Employee::set_info(string name0, double salary0, int y, int m, int d) { name = name0; salary = salary0; hire_date.year = y; hire_date.month = m; hire_date.day = d; } string Employee::get_name() const { return name; } double Employee::get_salary() const { return salary; } void Employee::display_info() const { cout << left << setw(15) << "id: " << id << endl; cout << setw(15) << "name: " << name << endl; cout << setw(15) << "salary: " << salary << endl; cout << setw(15) << "hire_date: " << hire_date.year << "-"; cout << std::right << setfill('0') << setw(2) << hire_date.month << "-" << setw(2) << hire_date.day; cout << setfill(' '); } void Employee::update_salary(double s) { salary = s; } void Employee::update_hire_date(int y, int m, int d) { hire_date.year = y; hire_date.month = m; hire_date.day = d; } void Employee::raise_salary(double by_percent) { double raise = salary * by_percent / 100; salary += raise; } void Employee::display_count() { cout << "there are " << count << " employees\n"; }
#include "Employee.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << Employee::doc << endl << endl; Employee employee1; employee1.set_info("Sam", 30000, 2015, 1, 6); employee1.update_hire_date(2019, 6, 30); employee1.update_salary(35000); employee1.display_info(); cout << endl << endl; Employee employee2{"Tony", 20000, 2023, 3, 16}; employee2.raise_salary(15); employee2.display_info(); cout << endl << endl; Employee::display_count(); } int main() { test(); }

任务3
#pragma once #include<iostream> #include<cmath> class Complex{ public : Complex(double r=0,double i=0){ real=r;imag=i ;} Complex(const Complex &x){ real=x.real; imag=x.imag; } double get_real() const{ return real; } double get_imag() const{ return imag; } void show() const{ using namespace std; if(real==0&&imag==0){cout<<0;} else if(real==0){cout<<imag<<"i";} else if(imag==0){cout<<real;} else{ if(imag>0){ cout<<real<<"+"<<imag<<"i"; } else if(imag<0){ cout<<real<<"-"<<-imag<<"i"; } } } void add( const Complex &x) {real+=x.real; imag+=x.imag;} friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(const Complex &c1); private: double real; double imag; }; Complex add(const Complex &c1,const Complex &c2){ double real=c1.real+c2.real; double imag=c1.imag+c2.imag; return Complex(real,imag); } bool is_equal(const Complex &c1,const Complex&c2){ if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; } double abs(const Complex &c1){ return sqrt(c1.real*c1.real+c1.imag*c1.imag); }
#include <iostream> #include <cmath> #include"Complex.cpp" void test() { using namespace std; Complex c1(3, -4); const Complex c2(4.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main() { test(); }

任务4
#pragma once #include<iostream> #include<string> using namespace std; class User { public: User(string name,string passwd = "111111",string email = " "): N{name}, P{passwd}, E{email} {n++;} ~User() = default; void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string N,P,E; static int n; }; int User::n = 0; void User::set_email() { cout << "Enter email address: " ; cin >> E ; cout << "email is set successfully..." << endl; } void User::change_passwd() { cout << "Enter old password: "; string temp; int i = 3; while(i) { cin >> temp; if(temp == P) { cout << "Enter new passwd: "; cin >> P; cout << "new passwd is set successfully..." << endl; break; } else { i--; if(i != 0) cout << "password input error. Please re-enter again: " ; } if(i == 0) { cout << "password input error. Please try after a while." << endl; } } } void User::print_info() { string pp(P.size(), '*'); cout << "name: " << N << endl; cout << "passwd: " << pp << endl; cout << "email: " << E << endl; } void User::print_n() { cout << "there are " << n << " users." << endl; }
#include "User.hpp" #include <iostream> void test(){ using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("jaychou"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main(){ test(); }


任务5
class SavingsAccount { private: int id; double balance; double rate; int lastDate; double accumulation; static double total; void record(int date, double amount); double accumulate(int date) const { return accumulation+balance*(date-lastDate); } public: SavingsAccount(int date, int id, double rate); int getId() const {return id;} double getBalance() const {return balance;} double getRate() const {return rate;} static double getTotal() {return total;} void deposit(int data, double amount); void withdraw(int data, double amount); void settle(int data); void show() const; };
#include"accout.hpp" #include<cmath> #include<iostream> using namespace std; double SavingsAccount::total=0; SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0) { cout<<date<<"\t#"<<id<<"is created"<<endl; } void SavingsAccount::record(int date,double amount) { accumulation=accumulate(date); lastDate=date; amount=floor(amount*100+0.5)/100; balance+=amount; total+=amount; cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; } void SavingsAccount::deposit(int date,double amount) { record(date,amount); } void SavingsAccount::withdraw(int date,double amount) { if(amount>getBalance()) cout<<"Error:not enough money"<<endl; else record(date,-amount); } void SavingsAccount::settle(int data) { double interest=accumulate(data)*rate/365; if(interest!=0) record(data,interest); accumulation=0; } void SavingsAccount::show() const { cout<<"#"<<id<<"\tBalance:"<<balance; }
#include "account.h" #include <iostream> using namespace std; int main() { SavingsAccount sa0(1, 21325302, 0.015); SavingsAccount sa1(1, 58320212, 0.015); sa0.deposit(5, 5000) ; sa1.deposit(25, 10000); sa0.deposit(45, 5500); sa1.withdraw(60, 4000); sa0.settle(90); sa1.settle(90); sa0.show();cout << endl; sa1.show();cout << endl; cout << "Total:" << SavingsAccount::getTotal() << endl; return 0; }


浙公网安备 33010602011771号