2022.10.12 OOP实验课
实验2 类与对象(2)
任务4
Complex.hpp
#pragma once
#include<iostream>
#include<math.h>
class Complex
{
private:
double real;
double imag;
public:
//构造函数
Complex();
//~Complex();
Complex(double real0, double imag0);
Complex(const Complex& obj);
Complex(double real0);
//成员函数
double get_real() const {return real;};
double get_imag() const {return imag;};
void show() const;
void add(const Complex obj);
//友元函数
friend Complex add(Complex c1, Complex c2);
friend bool is_equal(Complex c1, Complex c2);
friend double abs(Complex obj);
};
//构造函数
Complex::Complex():real{0.0},imag{0.0}
{
}
Complex::Complex(double real0, double imag0):real{real0}, imag{imag0}
{
}
Complex::Complex(const Complex& obj): real{obj.real}, imag{obj.imag}
{
}
Complex::Complex(double real0):real{real0},imag{0.0}
{
}
//成员函数
void Complex::show() const{
if(imag == 0){
std::cout << real;
}
else if(imag>0){
std::cout << real << "+" << fabs(imag) << "i";
}
else if(imag<0){
std::cout << real << "-" << fabs(imag) <<"i";
}
}
void Complex::add(const Complex obj){
real += obj.real;
imag += obj.imag;
}
//友元函数
Complex add(Complex c1, Complex c2){
return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
bool is_equal(Complex c1, Complex c2){
if(c1.real == c2.real && c1.imag == c2.imag){
return true;
}
else{
return false;
}
}
double abs(Complex obj){
return sqrt(pow(obj.real,2) + pow(obj.imag,2));
}
task4.cpp
#include "Complex.hpp"
#include <iostream>
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();
}
测试结果:
其他数据测试结果:
任务5
User.hpp
#pragma once
#include <iostream>
#include <string>
using namespace std;
class User{
private:
//数据成员
string name, passwd, email;
public:
//函数成员
//构造函数
User(string name0);
User(string name0, string passwd, string email);
//成员函数
void set_email();
void change_passwd();
void print_info();
static void print_n();
static int n;
};
int User::n = 0;
//构造函数
User::User(string name0): name{name0}, passwd{"111111"}, email{""}{n++;}
User::User(string name0, string passwd0, string email0): name{name0}, passwd{passwd0}, email{email0}{n++;}
//成员函数
void User::set_email(){
cout <<"Enter email address: ";
cin >> email;
cout << "email is set sucessfully..." << endl;
}
void User::change_passwd(){
string testpasswd;
cout << "Enter old password: ";
cin >> testpasswd;//输入测试密码
for(int i=1; i<3 && testpasswd != passwd; i++){
//一共三次机会
cout << "password input error. Please re-enter again: ";
cin >> testpasswd;
}
if(testpasswd != passwd){
//连续输错三次
cout << "passworf input error. Please try after a while.\n";
}else if(testpasswd == passwd){
cout << "Enter new passwd: ";
cin >> passwd;
cout << "new password is set successfully...";
}
}
void User::print_info(){
cout << "name: " << name << endl;//打印名字
int num = passwd.length();
cout << "passwd: ";
for( int i=0; i < num ; i++){
cout << "*";
}
cout << endl;//打印密码
cout << "email: " << email << endl;//打印邮箱
}
void User::print_n(){
cout << "there are " << n << " users. "<< endl;
}
task5.cpp
#include "User.hpp"
#include <iostream>
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("Lenard");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main(){
test();
}
测试结果: