首页 > 其他分享 >实验二 类与对象2

实验二 类与对象2

时间:2022-10-14 16:58:32浏览次数:49  
标签:const cout 对象 void Complex 实验 using include

4. 实验任务4
不使用C++标准库,自行设计并实现一个简化版复数类Complex

Complex.hpp

 1 #include <iostream>
 2 #include <math.h> 
 3 using namespace std; 
 4 
 5 class Complex {
 6     
 7 public:
 8     Complex(double r = 0, double i = 0): real{r}, imag{i} {}
 9     Complex(const Complex& obj): real{obj.real}, imag{obj.imag} {}
10     
11     double get_real() const {
12         return real;
13     }
14     double get_imag() const {
15         return imag;
16     }
17     void show() const;
18     void add(const Complex& c);
19     
20     friend Complex add(Complex c1, Complex c2);
21     friend bool is_equal(Complex c1, Complex c2);
22     friend int abs(Complex c1);
23     
24 private:
25     double real, imag;
26 };
27 
28 void Complex::show() const {
29     if(imag == 0) {
30         cout << real;
31     }
32     else if(imag < 0) {
33         cout << real << " - " << -1 * imag << "i";
34     } else{
35         cout << real << " + " << imag << "i";
36     }
37 }
38 void Complex::add(const Complex& c) {
39     real += c.real;
40     imag += c.imag;
41 }
42 
43 Complex add(Complex c1, Complex c2) {
44     Complex c3(c1.real + c2.real, c1.imag + c2.imag);
45     return c3;
46 }
47 bool is_equal(Complex c1, Complex c2) {
48     return (c1.real == c2.real && c2.imag == c1.imag);
49 }
50 int abs(Complex c) {
51     return sqrt(c.real * c.real + c.imag * c.imag);
52 }

task4.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 #include <math.h> 
 4 // 类测试 
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(3, -5);
 9     const Complex c2(3.5);
10     Complex c3(c1);
11 
12     cout << "c1 = ";
13     c1.show();
14     cout << endl;
15 
16     cout << "c2 = ";
17     c2.show();
18     cout << endl;
19     cout << "c2.imag = " << c2.get_imag() << endl;
20 
21     cout << "c3 = ";
22     c3.show();
23     cout << endl;
24 
25     cout << "abs(c1) = ";
26     cout << abs(c1) << endl;
27 
28     cout << boolalpha;
29     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
30     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
31 
32     Complex c4;
33     c4 = add(c1, c2);
34     cout << "c4 = c1 + c2 = ";
35     c4.show();
36     cout << endl;
37 
38     c1.add(c2);
39     cout << "c1 += c2, " << "c1 = ";
40     c1.show();
41     cout << endl;
42 }
43 
44 int main() {
45     test();
46 }

 

 

 

5. 实验任务5

User.hpp

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class User {
 7 
 8 public:
 9     User(string n, string p = "111111", string e = "") : 
10         name{ n }, passwd{ p }, email{ e } { 
11             num ++; 
12         }
13 
14     void set_email();
15     void change_passwd();
16     void print_info();
17     static void print_n() {
18         cout << "there are " << num << " users."; 
19     }
20 
21 private: 
22     string name, passwd, email;
23     static int num;
24 };
25 
26 int User::num = 0;
27 
28 void User::set_email() {
29     cout << "Enter email address: " ;
30     string email_;
31     cin >> email_;
32     email = email_;
33     cout << "email is set successfully..." << endl;
34 }
35 
36 void User::change_passwd() {
37     string old_pwd, new_pwd;
38     int count = 1;
39 
40     cout << "Enter old password: ";
41     cin >> old_pwd;
42     
43     while(old_pwd != passwd) {
44         cout << "password input error. Please re-enter again: ";
45         cin >> old_pwd;
46         count++;
47         
48         if(count == 3) {
49             cout << "password input error. Please try after a while. " << endl;
50             return;
51         }
52     }
53     cout << "Enter new password: ";
54     cin >> new_pwd; 
55     passwd = new_pwd;
56     
57     cout << "new passwd is set successfully..." << endl;
58     
59 }
60 
61 void User::print_info() {
62     string pwd(passwd.length(), '*');
63     cout << "name:\t" << name << endl;
64     cout << "passwd:\t" << pwd << endl;
65     cout << "email:\t" << email << endl;    
66 }

task5.cpp

 1 #include "User.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 // 测试User类 
 6 void test() {
 7     using std::cout;
 8     using std::endl;
 9     using std::string;
10     
11     cout << "testing 1......\n";
12     User user1("Jonny", "92197", "[email protected]");
13     user1.print_info();
14 
15     cout << endl
16          << "testing 2......\n\n";
17          
18     User user2("Leonard");
19     user2.change_passwd();
20     user2.set_email();
21     user2.print_info();
22 
23     cout << endl;
24     User::print_n();
25 }
26 
27 int main() {
28     test();
29 }

 

 

 

 

 

 

实验总结 :

hpp文件中 ,不能忘了导入需要的头文件, using 所需要的命名空间中的成员

标签:const,cout,对象,void,Complex,实验,using,include
From: https://www.cnblogs.com/youxiasaigao/p/16792104.html

相关文章

  • 10月14日内容总结——内置函数、可迭代对象和迭代器、异常捕获与处理
    目录一、重要内置函数1、zip2、filter3、sorted二、常见内置函数(了解)1、abs2、all3、any4、bytes5、bin、oct、hex、int6、callable7、chr、ord8、dir9、divmod10、enumera......
  • js 数组对象中每一项对象属性比较大小与计算数组对象属性和
    //数组对象排序方法 exportfunctioncompare(property){   returnfunction(a,b){    constvalue1=a[property]    constvalue2......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运......
  • 软件设计实验9
    实验9:桥接模式[实验任务一]:两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。直接放源码:#include<iostream>using......
  • 第二次实验
    实验任务四Complex.hpp源代码1#pragmaonce234#include<iostream>5#include<cmath>6usingnamespacestd;78classComplex9{10public:11......
  • asp.net core 使用转外部对象为Controller
    应用程序部件 MSDN说明:应用程序部件是对应用资源的抽象化,借助应用程序部件,ASP.NETCore可以发现控制器、视图组件、标记帮助程序、RazorPages、Razor编译源等。核......
  • 实验1
    实验任务1 实验任务2-1 #include<stdio.h>#include<math.h>intmain(){doublex,ans;while(scanf("%lf",&x)!=EOF){ans=pow(x,365);printf("%.2......
  • 实验2 类和对象(2)
    实验任务1程序源码#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};constcomplex<doub......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验......
  • 《MiniPRO H750开发指南》第五十二章 FPU测试(Julia分形)实验
    第五十二章FPU测试(Julia分形)实验​本章我们将学习如何开启STM32H750的硬件FPU,并对比使用硬件FPU和不使用硬件FPU的速度差别,以体现硬件FPU的优势。​本章分为如下几个小节......