实验一 类与对象
Complex.hpp
//Complex.hpp
#ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP #include<iostream> #include<cmath> class Complex { private: double real; double imag; public: Complex() {}; Complex(double a) { real = a; imag = 0; } Complex(double b, double c) { real = b; imag = c; } Complex(const Complex& p) { real = p.real; imag = p.imag; } double get_real()const { return real; } double get_imag()const { return imag; } void show()const { if (imag < 0) { std::cout << real << imag << "i" << std::endl; } else if(imag==0) { std::cout << real << std::endl; } else { std::cout << real << "+"<<imag << "i" << std::endl; } } Complex add(const Complex& c) { Complex t; t.real = real+c.real; t.imag = imag+c.imag; return t; } friend Complex add( Complex ,const Complex ); friend bool is_equal(Complex &c1 ,const Complex &c2 ); friend double abs(Complex &d); }; Complex add(Complex c1, const Complex c2) { Complex t; t.real = c1.real + c2.real; t.imag = c1.imag + c2.imag; return t; } bool is_equal(Complex &c1, const Complex &c2) { if (c1.real == c2.real && c1.imag == c2.imag) { return true; } else { return false; } } double abs(Complex& d) { return sqrt(d.real * d.real + d.imag * d.imag); } #endif
Task3.cpp
//task3.cpp
#include "Complex.hpp" #include<iostream> int main() { 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)="; std::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=c1.add(c2); cout << "c1+=c2," << "c1="; c1.show(); cout << endl; }

Task3.cpp
//task3.cpp
//另一组数据处理
#include "Complex.hpp" #include<iostream> int main() { using namespace std; Complex c1(6, 8); const Complex c2(3); 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)="; std::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=c1.add(c2); cout << "c1+=c2," << "c1="; c1.show(); cout << endl; }

User.hpp
//User.hpp
#ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP #include<iostream> #include<string> using namespace std; class User { private: string name; string password; string email; static int n; public: User(string name0) :name{ name0 }, password{"111111"}, email{""} { ++n; } User(string name0, string password0, string email0):name{name0},password{password0},email{email0} { ++n; } void set_email() { cout << "请输入你的邮箱地址:"; cin >> email; cout << "邮箱设置成功..." << endl; } void change_passwd() { string oldpassword; int i = 1; cout << "请输入旧密码:"; cin >> oldpassword; while (oldpassword != password && i <3) { cout << "密码错误,请重试:"; cin >> oldpassword; i++; } if (oldpassword != password && i == 3) { cout << "输入错误,请稍后重试." << endl; } if (oldpassword == password) { cout << "输入成功,请修改你的密码:"; cin >> password; cout << "已成功设置新密码." << endl; } } void print_info() { cout << "用户名:" << name << endl; cout << "密码:" << "******" << endl; cout << "联系邮箱:" << email << endl; } static void print_n(); }; int User::n = 0; void User::print_n() { cout << "这里一共有"<<n<<"名用户" << endl; } #endif
Task4.cpp
//task4.cpp #include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }


浙公网安备 33010602011771号