实验一 类与对象
1、实验任务1-2
略
2、试验任务3
实验流程: 不使用C++标准库,自行设计并实现一个复数类Complex,使用task3.cpp中的代码,测试类Complex的各项接口是否正确。
task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(12, -12); const Complex c2(123); 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; }
Complex.cpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include<iostream> #include<cstring> #include<cmath> using namespace std; //Complex类的定义 class Complex{ friend Complex add(const Complex& a,const Complex& b); friend bool is_equal(const Complex& a,const Complex& b); friend double abs(const Complex& a); public: Complex(double a=0,double b=0):real(a),imag(b){} Complex(const Complex& p):real(p.get_real()),imag(p.get_imag()){} double get_real() const{ return real; } double get_imag() const{ return imag; } void show() const{ cout<<real; if(imag!=0) cout<<(imag<0?" - ":" + ")<<abs(imag)<<"i"; cout<<endl; } void add(const Complex& a){ this->real+=a.get_real(); this->imag+=a.get_imag(); } private: double real; double imag; }; Complex add(const Complex& a,const Complex& b){ double m_real=a.real+b.real; double m_imag=a.imag+b.imag; return Complex(m_real,m_imag); } bool is_equal(const Complex& a,const Complex& b){ if(a.real==b.real&&a.imag==b.imag) return true; else return false; } double abs(const Complex& a){ return sqrt(a.real*a.real+a.imag*a.imag); } #endif
输出结果如下:

3、试验任务4
实验流程:设计并实现一个用户类User,并在主函数中使用和测试这个类。
task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("GGG", "12345", "abc@abc.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("AAA"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); cout << endl << "testing 3......" << endl << endl; user2.change_passwd(); }
User.hpp
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<cstring> using namespace std; //User类的定义 class User{ public: User(string m_name,string m_passwd="111111",string m_email=""):name(m_name),passwd(m_passwd),email(m_email){n++;} static void print_n(){ cout<<"there are "<<n<<" users"<<endl; } void set_email() { cout<<"Enter email address:"; string m_email; cin>>m_email; email = m_email; cout<<"email is set successfully..."<<endl; } void change_passwd(){ string m_passwd; cout<<"Enter old passwd:"; cin>>m_passwd; if(m_passwd==passwd){ set_passwd(); return; } cout<<"passwd input error."; int cnt=0; while(cnt<2){ cout<<"Please re-enter again:"; cin>>m_passwd; if(m_passwd==passwd){ set_passwd(); return; } cout<<"passwd input error."; cnt++; } cout<<"Please try after a while."<<endl; } void print_info(){ cout<<"email:\t"<<name<<endl; cout<<"passwd:\t******"<<endl; cout<<"email:\t"<<email<<endl; } private: string name,passwd,email; static int n; void set_passwd(){ string new_passwd; cout<<"Enter new passwd:"; cin>>new_passwd; passwd=new_passwd; cout<<"new passwd is set successfully..."<<endl; } }; int User::n=0; #endif
输出结果如下:


浙公网安备 33010602011771号