实验1 类与对象
1、实验任务3
- 程序源码
- Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std; class Complex { public: Complex(){ }; Complex(double r,double i); Complex(double r); Complex(const Complex &c); ~Complex(){}; double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(const Complex &c); private: double real; double imag; }; Complex::Complex(double r,double i):real(r),imag(i){} Complex::Complex(double r):real(r),imag(0.0){} Complex::Complex(const Complex &c):real(c.real),imag(c.imag){} double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show() const{ if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } void Complex::add(const Complex &c){ real+=c.real; imag+=c.imag; } Complex add(const Complex &c1,const Complex &c2){ Complex c; c.imag=c1.imag+c2.imag; c.real=c1.real+c2.real; return c; } bool is_equal(const Complex &c1,const Complex &c2){ if (c1.imag==c2.imag) return true; else return false; } double abs(const Complex &c){ return sqrt(c.imag*c.imag+c.real*c.real); } #endif
- task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); const Complex c2(5.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; }
- 运行测试结果
2、实验任务4
- 程序源码
- User.hpp
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <iomanip> #include <string> #include <cstring> using namespace std; class User { public: User(string n); User(string n,string p,string e); ~User(){}; void set_email(); void change_passwd (); void print_info(); static void print_n(); private: string name; string passwd; string email; static int num; }; int User::num=0; User::User(string n):name(n),passwd("111111"),email(" "){ ++num; } User::User(string n,string p,string e):name(n),passwd(p),email(e){ ++num; } void User::set_email(){ string input; cout<<"Enter email address:"; cin>>input; email=input; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { cout<<"Enter old password:"; string oldpsw; cin>>oldpsw; if(strcmp(oldpsw.c_str(),passwd.c_str())==0)//第一次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string sec; cin>>sec; if(strcmp(sec.c_str(),passwd.c_str())==0)//第二次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string thir; cin>>thir; if(strcmp(thir.c_str(),passwd.c_str())==0)//第三次 { string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please try for a while." <<endl; } } } } void User::print_info(){ cout<<"name:\t"<<name<<endl; cout<<"passwd:\t"<<"******"<<endl; cout<<"email:\t"<<email<<endl; } void User::print_n(){ if(num==1){ cout<<"there is 1 user."<<endl; } else{ cout<<"there are "<<num<<" users."<<endl; } } #endif
- task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Meekoon", "666666", "meekoon@nuist.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Author"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
- 运行测试结果
-
- 当修改密码,但是输入旧密码错误时:
当修改密码,输入旧密码正确
3、实验总结
-
- task4.cpp
- 运行测试结果
- task3.cpp
------------恢复内容结束------------
------------恢复内容开始------------
1、实验任务3
- 程序源码
- Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std; class Complex { public: Complex(){ }; Complex(double r,double i); Complex(double r); Complex(const Complex &c); ~Complex(){}; double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(const Complex &c); private: double real; double imag; }; Complex::Complex(double r,double i):real(r),imag(i){} Complex::Complex(double r):real(r),imag(0.0){} Complex::Complex(const Complex &c):real(c.real),imag(c.imag){} double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show() const{ if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } void Complex::add(const Complex &c){ real+=c.real; imag+=c.imag; } Complex add(const Complex &c1,const Complex &c2){ Complex c; c.imag=c1.imag+c2.imag; c.real=c1.real+c2.real; return c; } bool is_equal(const Complex &c1,const Complex &c2){ if (c1.imag==c2.imag) return true; else return false; } double abs(const Complex &c){ return sqrt(c.imag*c.imag+c.real*c.real); } #endif
- task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); const Complex c2(5.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; }
- 运行测试结果
2、实验任务4
- 程序源码
- User.hpp
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <iomanip> #include <string> #include <cstring> using namespace std; class User { public: User(string n); User(string n,string p,string e); ~User(){}; void set_email(); void change_passwd (); void print_info(); static void print_n(); private: string name; string passwd; string email; static int num; }; int User::num=0; User::User(string n):name(n),passwd("111111"),email(" "){ ++num; } User::User(string n,string p,string e):name(n),passwd(p),email(e){ ++num; } void User::set_email(){ string input; cout<<"Enter email address:"; cin>>input; email=input; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { cout<<"Enter old password:"; string oldpsw; cin>>oldpsw; if(strcmp(oldpsw.c_str(),passwd.c_str())==0)//第一次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string sec; cin>>sec; if(strcmp(sec.c_str(),passwd.c_str())==0)//第二次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string thir; cin>>thir; if(strcmp(thir.c_str(),passwd.c_str())==0)//第三次 { string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please try for a while." <<endl; } } } } void User::print_info(){ cout<<"name:\t"<<name<<endl; cout<<"passwd:\t"<<"******"<<endl; cout<<"email:\t"<<email<<endl; } void User::print_n(){ if(num==1){ cout<<"there is 1 user."<<endl; } else{ cout<<"there are "<<num<<" users."<<endl; } } #endif
- task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Meekoon", "666666", "meekoon@nuist.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Author"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
- 运行测试结果
-
- 当修改密码,但是输入旧密码错误时:
当修改密码,输入旧密码正确
3、实验总结
-
- task4.cpp
- 运行测试结果
- task3.cpp
------------恢复内容结束------------
------------恢复内容开始------------
1、实验任务3
- 程序源码
- Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std; class Complex { public: Complex(){ }; Complex(double r,double i); Complex(double r); Complex(const Complex &c); ~Complex(){}; double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(const Complex &c); private: double real; double imag; }; Complex::Complex(double r,double i):real(r),imag(i){} Complex::Complex(double r):real(r),imag(0.0){} Complex::Complex(const Complex &c):real(c.real),imag(c.imag){} double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show() const{ if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } void Complex::add(const Complex &c){ real+=c.real; imag+=c.imag; } Complex add(const Complex &c1,const Complex &c2){ Complex c; c.imag=c1.imag+c2.imag; c.real=c1.real+c2.real; return c; } bool is_equal(const Complex &c1,const Complex &c2){ if (c1.imag==c2.imag) return true; else return false; } double abs(const Complex &c){ return sqrt(c.imag*c.imag+c.real*c.real); } #endif
- task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); const Complex c2(5.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; }
- 运行测试结果
2、实验任务4
- 程序源码
- User.hpp
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <iomanip> #include <string> #include <cstring> using namespace std; class User { public: User(string n); User(string n,string p,string e); ~User(){}; void set_email(); void change_passwd (); void print_info(); static void print_n(); private: string name; string passwd; string email; static int num; }; int User::num=0; User::User(string n):name(n),passwd("111111"),email(" "){ ++num; } User::User(string n,string p,string e):name(n),passwd(p),email(e){ ++num; } void User::set_email(){ string input; cout<<"Enter email address:"; cin>>input; email=input; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { cout<<"Enter old password:"; string oldpsw; cin>>oldpsw; if(strcmp(oldpsw.c_str(),passwd.c_str())==0)//第一次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string sec; cin>>sec; if(strcmp(sec.c_str(),passwd.c_str())==0)//第二次 { cout<<"Enter new password:"; string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please re-enter again:"; string thir; cin>>thir; if(strcmp(thir.c_str(),passwd.c_str())==0)//第三次 { string newpsw; cin>>newpsw; passwd=newpsw; cout<<"new passwd is set successfully..."<<endl; } else { cout<<"password input error. Please try for a while." <<endl; } } } } void User::print_info(){ cout<<"name:\t"<<name<<endl; cout<<"passwd:\t"<<"******"<<endl; cout<<"email:\t"<<email<<endl; } void User::print_n(){ if(num==1){ cout<<"there is 1 user."<<endl; } else{ cout<<"there are "<<num<<" users."<<endl; } } #endif
- task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Meekoon", "666666", "meekoon@nuist.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Author"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
- 运行测试结果
-
-
3、实验总结
- task4.cpp
- 运行测试结果
- task3.cpp
还是需要勤加练习,看书敲代码。
posted on 2021-10-26 11:46 19物联网一班李大伟 阅读(43) 评论(0) 编辑 收藏 举报