实验二 类与对象

实验任务4:

task4.hpp:

#pragma once
#include<iostream>
#include<math.h>
using namespace std;

class Complex {
public:
	Complex() :real{ 0 }, imag{ 0 } {}
	~Complex() {}
	Complex(double real) : real{ real }, imag{ 0 } {}
	Complex(double real, double imag) : real{ real }, imag{ imag }{}
	Complex(const Complex& c0) :real{ c0.real }, imag{ c0.imag }{}

	double get_real()const { return real; }
	double get_imag()const { return imag; }
	void  show() const;
	void add(Complex c0);

	friend Complex add(Complex c1, Complex c2);
	friend bool is_equal(Complex x, Complex rx);
	friend double abs(Complex c0);

private:
	double real, imag;

};

Complex add(Complex c1, Complex c2) {
	Complex result;
	result.real = c1.real + c2.real;
	result.imag = c1.imag + c2.imag;

	return result;
}

bool is_equal(Complex x, Complex rx) {
	if (x.real == rx.real && x.imag == rx.imag)
		return true;
	else
		return false;
}

double abs(Complex c) {
	double result = (double)sqrt((double)pow(c.real, 2) + (double)pow(c.imag, 2));
	return result;
}

void  Complex::show() const {

	if (real != 0) {
		cout << real;
	}

	if (imag != 0 && imag > 0) {
		cout << " + " << imag << 'i';
	}
	else if (imag != 0 && imag < 0) {
		cout << " " << imag << 'i';
	}
}

void Complex::add(Complex c0) {
	real += c0.real;
	imag += c0.imag;
}

  task4.cpp:

#include<iostream>
#include<math.h>
#include"myComplex.h";
using namespace std;

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:

task.hpp:

#pragma once
#include<iostream>
#include<string>

using namespace std;
class User {
public:
	User();

	User(string name) :name{ name }, password{ "111111" }, email{ NULL }{ n++; }
	User(string name, string psssword) :name{ name }, password{ password }, email{ NULL }{ n++; }
	User(string name, string password, string email) :name{ name }, password{ password }, email{ email }{ n++; }

	void set_email();
	void change_password();
	void print_info();

	static void print_n();
	static int n;
private:
	string name, password, email;
};

int User::n;

void User::set_email() {
	cout << "Enter email address:";
	cin >> email;
	cout << "email is set successful..." << endl;
}

void User::change_password() {

	string pass;
	cout << "Enter old password:";
	cin >> pass;

	for (int i = 0; i < 3; i++) {
		if (pass == password) {
			cout << "Enter new password:";
			cin >> pass;
               password = pass; cout << "new password is set successfully..." << endl; break; } else { if (i < 2) { cout << "password input error.Please re-enter again:"; cin >> pass; } else { cout << "password input error.Please try after a while."; } cout << endl; } } } void User::print_info() { cout << "name:" << name << endl; int count = password.length(); cout << "password:"; for (int i = 0; i < count; i++) { cout << "*"; } cout << endl; cout << "email:" << email << endl; } void User::print_n() { cout << "there are " << n << " users."; }

  task5.cpp:

#include<iostream>
#include<string>
#include"User.h"

void test() {
	using std::cout;
	using std::endl;
	
	cout << "testing 1......\n";
	User user1("Jonny", "92197", "xyz@hotmail.com");
	user1.print_info();

	cout << endl << "testing 2......\n\n";
	User user2("Leonard");
	user2.change_password();
	user2.set_email();
	user2.print_info();

	cout << endl;
	User::print_n();
}

int main() {
	test();
}

  运行测试截图:

 

 

 

posted @ 2022-10-13 23:45  彭鑫宇  阅读(19)  评论(0编辑  收藏  举报