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

实验2 类和对象(2)

时间:2022-10-16 12:44:09浏览次数:55  
标签:const cout 对象 void Complex 实验 include imag

实验任务4:

程序源码:

complex.hpp  

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

 

task4.cpp 源码:

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

运行截图:

 

 

 

实验任务5:

user.hpp程序源码

 1 #pragma once
 2 #include <iostream>
 3 #include <string.h>
 4 #include <iomanip>
 5 using namespace std;
 6 
 7 class User {
 8 public:
 9     User(string name, string password = "111111", string email = "") : name(name),
10         password(password), email(email) {
11         n++;
12     }
13     void set_email();
14     void change_passwd();
15     void print_info();
16     void print_n();
17 private:
18     string name, password, email;
19     static int n;
20 };
21 void User::set_email() {
22     cout << "ENTER YOUR EMAIL" << ":";
23     cin >> email;
24 
25 }
26 void User::change_passwd() {
27     string old_passwd;
28     cout << "ENTER FORMER PASSWD:";
29     cin >> old_passwd; 
30     int i = 2;
31     if (old_passwd != password) {
32         
33         while (i) {
34             
35                 cout << "ENTER AGAIN:";
36                 cin >> old_passwd;
37                 if (old_passwd != password)
38                     i--;
39                 else
40                     break;
41             
42         }
43     }
44     if (i > 0)
45     {
46         cout << "ENTER NEW_PASSWD:";
47         cin >> password;
48     
49     }
50     
51     
52 
53 }
54 void User::print_info() {
55     cout << "name:" << name << endl;
56     cout << "password:" << setfill('*') << setw(password.size())<< "" << endl;
57     cout << "email:" << email << endl;
58 
59 }
60 void User::print_n() {
61     cout << "total" << n << endl;
62 
63 }
64 int User::n = 0;

 

task5.hpp程序源码

 

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

程序运行截图

 

标签:const,cout,对象,void,Complex,实验,include,imag
From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16795985.html

相关文章

  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1、能够独立完成OpenDaylight控制器的安装配置;2、能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、......
  • Java类和对象小结
    类与对象类是一个模板:抽象的。对象是一个具体的实例:具体的方法定义、调用对应的引用引用类型:基本类型(8大基本类型)对象是通过引用来操作的属性:字段Fie......
  • 实验5:开源控制器实践——POX
    实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络应用......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验4:开源控制器实践——OpenDaylight
    一、基础要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s......
  • 实验1 C语言开发环境使用和编程初体验
    #include<stdio.h>intmain(){printf("OO\n");printf("<H><H>\n");printf("IIII\n");return0;}#include<stdio.h>#include<mat......
  • 软件设计实验11
    实验11:装饰模式[实验任务一]:手机功能的升级用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人;而JarPhone除了声音还能振动;更高级......
  • 实验二——类与对象(2)
    task2:array<int,5>x3<x1>;语句在devc的环境下可能会报错,改成array<int,5>x3(x1)可正确编译。task3:to_string的功能是将数字常量转换为字符串,还有就是要注意,静态变量类外......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......