task1.cpp
方式一
t.h
#ifndef T_H #define T_H #include <iostream> #include <string> using namespace std; class T { public: T(int x = 0, int y = 0); T(const T &t); T(T &&t); ~T(); void set_m1(int x); int get_m1() const; int get_m2() const; void display() const; friend void func(); private: int m1, m2; public: static void disply_count(); public: static const string doc; static const int max_count; private: static int count; }; void func(); #endifView Code
t.cpp
#include "t.h" const string T::doc{"a simple class"}; const int T::max_count = 99; int T::count = 0; T::T(int x, int y): m1{x}, m2{y} { ++count; cout << "constructor called.\n"; } T::T(const T &t): m1{t.m1}, m2{t.m2} { ++count; cout << "copy constructor called.\n"; } T::T(T &&t): m1{t.m1}, m2{t.m2} { ++count; cout << "move constructor called.\n"; } T::~T() { --count; cout << "destructor called.\n"; } void T::set_m1(int x) { m1 = x; } int T::get_m1() const { return m1; } int T::get_m2() const { return m2; } void T::display() const { cout << m1 << ", " << m2 << endl; } void T::disply_count() { cout << "T objects: " << count << endl; } void func() { T t1; t1.set_m1(55); t1.m2 = 77; t1.display(); }View Code
main.cpp
#include "t.h" void test() { cout << "T class info: " << T::doc << endl; cout << "T objects max_count: " << T::max_count << endl; T::disply_count(); T t1; t1.display(); t1.set_m1(42); T t2{t1}; t2.display(); T t3{std::move(t1)}; t3.display(); t1.display(); T::disply_count(); } int main() { cout << "============测试类T============" << endl; test(); cout << endl; cout << "============测试友元函数func()============" << endl; func(); }View Code
运行结果
方式二
t.hpp
#pragma once #include <iostream> #include <string> using namespace std; class T { public: T(int x = 0, int y = 0); T(const T &t); T(T &&t); ~T(); void set_m1(int x); int get_m1() const; int get_m2() const; void display() const; friend void func(); private: int m1, m2; public: static void disply_count(); public: static const string doc; static const int max_count; private: static int count; }; const string T::doc{"a simple class"}; const int T::max_count = 99; int T::count = 0; T::T(int x, int y): m1{x}, m2{y} { ++count; cout << "constructor called.\n"; } T::T(const T &t): m1{t.m1}, m2{t.m2} { ++count; cout << "copy constructor called.\n"; } T::T(T &&t): m1{t.m1}, m2{t.m2} { ++count; cout << "move constructor called.\n"; } T::~T() { --count; cout << "destructor called.\n"; } void T::set_m1(int x) { m1 = x; } int T::get_m1() const { return m1; } int T::get_m2() const { return m2; } void T::display() const { cout << m1 << ", " << m2 << endl; } void T::disply_count() { cout << "T objects: " << count << endl; }View Code
main.cpp
#include "t.hpp" void test() { cout << "T class info: " << T::doc << endl; cout << "T objects max_count: " << T::max_count << endl; T::disply_count(); T t1; t1.display(); t1.set_m1(42); T t2{t1}; t2.display(); T t3{std::move(t1)}; t3.display(); t1.display(); T::disply_count(); } int main() { cout << "============测试类T============" << endl; test(); cout << endl; cout << "============测试友元函数func()============" << endl; func(); }View Code
运行结果
task2.cpp
Employee.cpp
#pragma once // Employee类的定义 #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; }; // Employee类的声明 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; } // 雇员提薪加成 // by_percent是提升比例 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"; }View Code
main.cpp
#include "Employee.hpp" #include <iostream> // 测试:Employee类 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); // 提成15% employee2.display_info(); cout << endl << endl; Employee::display_count(); } int main() { test(); }View Code
运行结果
task3.cpp
Complex.cpp
#pragma once #include <iostream> #include <cmath> class Complex{ private: double real, imag; public: Complex(double r=0,double i=0){ real=r; imag=i; }; Complex(const Complex &c){ real=c.real; imag=c.imag; } double get_real() const { return real; } double get_imag() const { return imag; } void add(const Complex &c1){ real += c1.get_real(); imag += c1.get_imag(); } void show(){ std::cout << real; if (imag>0) std::cout << " + " << abs(imag) << 'i'; if (imag<0) std::cout << " - " << fabs(imag) << 'i'; } void show() const{ std::cout << real; if (imag>0) std::cout << "+" << abs(imag) << "i"; if (imag<0) std::cout << "-" << abs(imag) << "i"; } public: friend Complex add(const Complex &c1, const Complex &c2){return Complex(c1.get_real() + c2.get_real(),c1.get_imag() + c2.get_imag());} friend bool is_equal(const Complex &c1, const Complex &c2){ return (c1.real == c2.real) && (c1.imag == c2.imag);} friend double abs(const Complex &c1){return sqrt(c1.real * c1.real + c1.imag * c1.imag);} };View Code
main.cpp
#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(); }View Code
运行结果
task4.cpp
User.hpp
#pragma once #include <iostream> #include <string> using std::string; using std::cout; using std::cin; class User{ public: User(); User(string name0,string passwd0="111111",string email0=""):name{name0},passwd{passwd0},email{email0} {n++; }; void print_info(); void set_email(); void change_passwd(); public: static void print_n(); private: string name; string passwd; string email; private: static int n; }; int User::n=0; void User::set_email(){ using namespace std; cout << "Enter email address: "; string email0; cin>>email0; email=email0; cout << "email is set successfully...\n"; } void User::print_info(){ using namespace std; string s(passwd.size(),'*'); cout << "password: " << s << endl; cout << "name: " << name << endl; cout << "email: " << email << endl; } void User::change_passwd(){ using namespace std; string pwd,newwd; for(int i=0;i<=3;i++) { if(i==0){ cout<<"Enter old password:"; } if(i==3){ cout << "Please try after a while. \n"; break; } else { cout<<"Please re-enter again:"; cin>>pwd; if (pwd!=passwd){ cout<<"password input error.\n"; continue; } else{ cout<<"Enter newpassword:"; cin>>newwd; passwd=newwd; cout << "new passwd is set successfully...\n"; break; } } } } void User::print_n(){ cout << "there are " << n << " users\n"; }View Code
task4.cpp
#include "User.hpp" #include <iostream> // 测试User类 void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }View Code
task5.cpp
accout.hpp
class SavingsAccount { private: int id; double balance; double rate; int lastDate; double accumulation; static double total; void record(int data,double amount); double accumulate(int date)const { return accumulation+balance*(date-lastDate); } public: SavingsAccount(int data,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 date,double amount); void withdraw(int data,double amount); void settle(int date); void show() const; };View Code
#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; }View Code
#include "accout.hpp" #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); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个帐户信息 sa0.show();cout << endl; sa1.show();cout << endl; cout << "Total:" << SavingsAccount::getTotal() << endl; return 0; }View Code
运行结果
标签:const,string,对象,double,void,int,实验,include From: https://www.cnblogs.com/PerfectAllKill/p/17766934.html