首页 > 其他分享 >类和对象(2)

类和对象(2)

时间:2022-10-18 22:57:25浏览次数:42  
标签:const cout 对象 double void Complex User

实验4:

Complex.hpp文件源码

 1 #pragma once
 2 
 3 // Complex类的定义
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 // Complex类的声明
 9 class Complex {
10 public:
11     Complex(double r = 0.0, double i = 0.0);
12     Complex(const Complex& obj);
13     double get_real()const { return real; }
14     double get_imag()const { return imag; }
15     void show() const;
16     void add(Complex c);
17 
18 private:
19     double real;
20     double imag;
21 
22 public:
23     friend Complex add(Complex c1, Complex c2);
24     friend bool is_equal(Complex c1, Complex c2) ;
25     friend double abs(Complex c);
26 };
27 
28 // 函数定义
29 Complex::Complex(double r, double i) {
30     real = r;
31     imag = i;
32 }
33 
34 Complex::Complex(const Complex& obj) : real{ obj.real }, imag{ obj.imag }{
35 }
36 
37 void Complex::show()const {
38     if (imag < 0)
39         cout << real << " - " << -imag << "i";
40     else {
41         if (imag == 0)
42             cout << real;
43         else
44             cout << real << "+" << imag << "i";
45     }
46 }
47 
48 void Complex::add(Complex c) {
49     real += c.get_real();
50     imag += c.get_imag();
51 }
52 
53 Complex add(Complex c1, Complex c2) {
54     Complex c;
55     c.real = c1.real + c2.real;
56     c.imag = c1.imag + c2.imag;
57     return c;
58 }
59 
60 bool is_equal(Complex c1, Complex c2) {
61     if (c1.real == c2.real && c1.imag == c2.imag)
62         return right;
63     else
64         return false;
65 }
66 
67 double abs(Complex c) {
68     return sqrt(c.real * c.real + c.imag * c.imag);
69 }

task.cpp文件源码

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 // 类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(3, -4);
 9     const Complex c2(4.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 }

task4运行截图

 

 

 

task5:

User.hpp文件源码

 1 #pragma once
 2 #include <iostream>
 3 
 4 // User类的定义
 5 
 6 using namespace std;
 7 
 8 // User类的声明
 9 class User {
10 public: 
11     User(string name0, string passwd0 = "111111", string email0 = " ") :name { name0 },
12     passwd{ passwd0 }, email{ email0 } { n++; }
13     ~User() { n--; }
14 
15     void set_email();
16     void change_passwd();
17     void print_info();
18     static void print_n();   // 静态成员函数
19 
20 private:
21     string name, passwd, email;
22     static int n;       // 静态数据成员声明,用于记录用户的个数
23 };
24 
25 int User::n = 0;    // 静态数据成员定义和初始化,使用类名限定
26 
27 // 函数定义
28 void User::set_email() {
29     cout << "Enter email address: ";
30     cin >> email;
31     cout << "email is set successfully...\n";
32 }
33 
34 void User::change_passwd() {
35     int i = 1;
36     cout << "Enter old passwd: ";
37     while (i <= 3) {
38         string s1;
39         cin >> s1;
40         if (s1 == passwd) {
41             cout << "Enter new passwd: ";
42             cin >> s1;
43             passwd = s1;
44             break;
45         }
46         else {
47             cout << "passwd input error. ";
48             if (i == 3)
49                 cout << "Please try after a while." << endl;
50             else
51                 cout << "Please re-enter again: ";
52             i++;
53         }
54     }
55 }
56 
57 void User::print_info() {
58     int len = passwd.size();
59     string s2( len, '*' );
60     // passwd.assign(passwd.size(), '*');     // 重新赋值
61     cout << "name:   " << name << endl;
62     cout << "passwd: " << s2 << endl;
63     cout << "email:  " << email << endl;
64 }
65 
66 void User::print_n() {
67     cout << "there are " << n << " users.";
68     cout << endl;
69 }

task5.cpp文件源码

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

task5运行截图

 

 

 

 

 

实验总结:

1、在Complex.hpp的创建过程中,直观感受到了const关键字对于成员函数的限制性,以及在调用参数时产生的一些报错,对于const的理解与运用更加熟练。

2、在User.hpp的创建过程中,主要是对于在密码加密输出那几行代码琢磨了很久,对于string类的相关函数的使用还是不够熟练。

3、task5的选做部分,emmm,还没有太清楚的思路和想法,后续再看。

标签:const,cout,对象,double,void,Complex,User
From: https://www.cnblogs.com/xiao-shi/p/16804515.html

相关文章

  • 实验二 类和对象(2)
    实验任务4h.pp1#pragmaonce2#include<iostream>3#include<cmath>4usingstd::cout;5usingstd::endl;67classComplex{8private:9doub......
  • JavaScript学习--Array数组对象
    定义1.var变量名=newArray(元素列表);如vararr=newArray(1,2,3);2.常用:var变量名=[元素列表];如vararr=[1,2,3];访问arr[索引]=值;如arr[0]=1;ps:数组长度类型均可变 len......
  • Python类对象的创建和使用
    通过前面章节的学习,我们已经学会如何定义一个类,但要想使用它,必须创建该类的对象。创建类对象的过程,又称为类的实例化。Python类的实例化对已定义好的类进行实例化,其......
  • 域对象共享数据
    1、使用ServletAPI向request域对象共享数据@RequestMapping("/testServletAPI")publicStringtestServletAPI(HttpServletRequestrequest){request.setAttribute("te......
  • 如何选择数据存储空间?华为云对象存储服务,让你省心又划算
     上至大企业,下至普通用户,在平日里都习惯将一些重要的文件存储在网络云盘上,这样才便捷日常的使用,随意翻出来查看也省时,畅享移动的数据存储魅力。来自华为云的对象存储服务......
  • 数组转对象
    使用object.assign()方法将数组转换为对象assign()方法可以迭代地从一个或多个对象读取属性到目标对象。它返回目标对象。参考下面的代码。constarray=['foo','b......
  • 对象深拷贝的五种方法
    对象深拷贝的五种方法JSON暴力转化varobj={name:'123'}varobj2=JSON.parse(JSON.stringify(obj这种简单粗暴的方式有局限性,当值为undefined、function、symbol......
  • (面向对象)已知定义人的类Person,请完成:1.定义学生的类Stu并继承人的类Person;2.重写构造
    样例输入张三男李四女19 样例输出姓名:张三,性别:男李四女19姓名:王五,性别:?,年龄:29解题代码#coding=gbk#定义人的类classPerson(object):#继承o......
  • TS 自定义类型-修改使对象部分属性必填
    工作中常常用API的入参是非必填的,而实例的属性因为有默认值而一定存在的情况,举个例子:typeTestOptions={num?:numberstr?:strhookFn?:()=>string}c......
  • 实验二 类和对象(二)
    1#include<iostream>2#include<complex>3intmain()4{5usingnamespacestd;6complex<double>c1{3,4},c2{4.5};7constcomplex<double>......