试验任务4
Complex.cpp
#pragma once
#include<iostream>
#include<cmath>
class Complex {
public:
Complex();
Complex(double newreal);
Complex(double newreal, double newimag);
Complex(const Complex& C);
double get_real() const;
double get_imag()const;
void show()const;
void add(const Complex &a);
friend Complex add(const Complex& a, const Complex& b);
friend bool is_equal(Complex& a, const Complex& b);
friend double abs(const Complex& a);
private:
double real;
double imag;
};
Complex::Complex() :real(0), imag(0) {}
Complex::Complex(double newreal) :real(newreal), imag(0) {}
Complex::Complex(double newreal, double newimag) :real(newreal), imag(newimag) {}
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 {
std::cout << real << imag ;
if (imag != 0)
std::cout << "i";
}
void Complex::add(const Complex &a) {
real = real + a.real;
imag = imag + a.imag;
}
Complex add(const Complex& a, const Complex& b) {
Complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
bool is_equal(Complex& a, const Complex& b) {
if (a.real == b.real && a.imag == b.imag)
return true;
else
return false;
}
double abs(const Complex&obj) {
return sqrt(obj.real * obj.real + obj.imag * obj.imag);
}
试验任务4 源码
#include <iostream> #include "Complex.hpp" 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(); }
运行结果
试验任务5User.hpp
#pragma once #include<string> #include<iostream> using namespace std; class User { public: User(string h,string p,string k) { name = h; passwd = p; email = k; n++; }; User(string h) { n++; name = h; passwd = "111111"; email =""; } void set_email(); void change_passwd() ; void print_info(); static void print_n(); static int n; private: string name; string passwd; string email; }; int User::n; void User::print_info() { cout << "name: " << name << endl; cout << "passwd: " <<"******" << endl; cout << "email: " << email << endl; } void User::change_passwd() { string h; int i; cout << "Enter old passworld:"; for (i = 0; i < 3; i++) { cin >> h; if (h == passwd) { cout << "Enter new passworld:"; cin >> passwd; cout << "new passworld is set successfully..."<<endl; break; } else { if(i<=1) cout << "passworld input error.please re-enter again:"; } } if (i == 3) cout << "passworld input error.please try afer a while"<<endl; } void User::set_email() { cout << "Enter email address:"; cin >> email; cout << "email is set successfully..." << endl; } void User::print_n(){ cout << "There are " << n << " users"<<endl; }
试验任务5 源码
#include "User.hpp" #include <iostream> // 测试User类 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("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n() ; } int main() { test(); }
实验总结
对于基本的语法使用还不太熟练
注意const static 的使用方法
标签:real,const,对象,double,imag,试验,Complex,void From: https://www.cnblogs.com/030227rzxx/p/16804647.html