实验二 类和对象
实验任务1.task1
#include<iostream> #include<complex> using namespace std; int main() { complex<double>c1{ 3,4 }, c2{ 4.5 }; const complex<double>c3{ c2 }; cout << "c1=" << c1 << endl; cout << "c2=" << c2 << endl; cout << "c3=" << c3 << endl; cout << "c3.real=" << c3.real() << endl; cout << "c3.imag=" << c3.imag() << endl; cout << "c1+c2=" << c1 + c2 << endl; cout << "c1-c2=" << c1-c2 << endl; cout << "abs(c1)=" << abs(c1) << endl; cout << boolalpha; cout << "c1==c2:" <<( c1==c2) << endl; cout<< "c3==c2:" << (c3==c2) << endl; complex<double>c4 = 2; cout << "c4=" << c4 << endl; c4 += c1; cout << "c4=" << c4 << endl; return 0;
}
实验任务2 task2
#include<iostream> #include<array> using namespace std; template<typename T> void output1(const T& obj) { for (auto i : obj) std::cout << i << ","; std::cout << "\b\b\n"; } template<typename T> void output2(const T & obj) { for (auto p=obj.cbegin();p!=obj.cend(); ++p) std::cout << *p << ","; std::cout << "\b\b\n"; } int main() { array<int, 5>x1; cout<<"x1.size()="<<x1.size()<<endl; x1.fill(42); x1.at(0) = 999; x1.at(4) = -999; x1[1] = 777; cout << "x1:"; output1(x1); cout << "x1:"; output2(x1); array<double, 10>x2{ 1.5,2.2 }; cout << "2x.size()" << x2.size() << endl; cout << "x2:"; output1(x2); array<int, 5>x3{ x1 }; cout << boolalpha << (x1 == x3) << endl; x3.fill(22); cout << "x3:"; output1(x3); swap(x1, x3); cout << "x1:"; output1(x1); cout << "x3:"; output1(x3); return 0; }

实验任务三.
#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(2017, 6, 30);
employee1.update_salary(35000);
employee1.display_info();
cout << endl << endl;
Employee employee2{"Tony", 20000, 2020, 3, 16};
employee2.raise_salary(15); // 提成15%
employee2.display_info();
cout << endl << endl;
Employee::display_count();
}
int main() {
test();
}

实验四
#include <iostream> #include<cmath> #include <math.h> using namespace std; class Complex { public: Complex(double r= 0, double i = 0) { re = r; im = i; double abs = sqrt(re * re + im * im); } Complex(const Complex& x) { re = x.re; im = x.im; } void add(Complex com) { re = re + com.re; im = im + com.im; cout << re << im << "i" << endl; } void show() { if (im != 0) cout << re << im << "i" << endl; else cout << re << endl; } double get_imag()const { return im; } void abs() { double b = sqrt(re * re + im * im); cout << b << endl; } friend Complex add(Complex x1, Complex x2) { Complex x; x.im = x1.im + x2.im; x.re = x1.re + x2.re; return x; } friend bool is_equal(const Complex& x1, const Complex& x2); friend double abs(Complex& x1); private:double re, im; }; bool is_equal(const Complex& x1, const Complex& x2) { if (x1.re == x2.re && x1.im == x2.im) return true; else return false; } double abs(Complex& x1) { return (sqrt(x1.re * x1.re + x1.im * x1.im)); }

实验五:
#include<iostream> #include<string> using namespace std; class User { public: User(string name, string passwd = "111111", string email = "") :n{ name }, p{ passwd }, e{ email }{count++; } ~User() = default; void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string n, p, e; static int count; }; int User::count = 0; void User::set_email() { cout << "enter email adress :"; cin >> e; cout << "email is set successfully" << endl; } void User::change_passwd() { cout << "enter old password:"; string pd; int i = 3; while (i) { cin >> pd; if (pd == p) { cout << "new passwd is set successfully" << endl; break; } else { i--; if (i != 0) cout << "password error,enter again:"; } if (i == 0) { cout << "password lock,try later" << endl; } } } void User::print_info() { cout << "name : " << n << endl; cout << "passwd: " << "******" << endl; cout << "email: " << e << endl; } void User::print_n() { cout << "there are" << count << "users." << endl; }



浙公网安备 33010602011771号