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

实验二 类和对象(2)

时间:2022-10-17 23:14:19浏览次数:42  
标签:cout 对象 void passwd Complex 实验 User include

task4:

Complex.hpp:

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

task4.cpp:

#include "Complex.hpp"
#include <iostream>

// 绫绘祴璇?
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();
}

测试截图:

 

 更换样例测试截图:

 

 

task5:

User.hpp:

#pragma once

#include<iostream>
#include<string>

using namespace std;

class User{
    public:
        
        User(string n,string p="111111",string e=""):name{n},passwd{p},email{e}{ n+=1;}
        
        void set_email(){
            cout<<"Enter email address:";
            cin>>email;
            cout<<"email is set successfully..."<<endl;
        };
        
        void change_passwd(){
            
            string testpasswd;
            cout<<"Enter old password:";
            cin>>testpasswd;
            
            for(int times=1;times<3&&testpasswd!=passwd;times++){
                cout<<"password input error.Please re-enter again:";
                cin>>testpasswd;
            }
            if(testpasswd!=passwd){
                cout<<"password input error.Please try after a while";
            }else{
                cout<<"Enter new passwd:";
                cin>>passwd;
                cout<<"new password is set successfully..";
            }
        };
        void print_info(){
            cout<<"name:      "<<name<<endl;
            cout<<"password:";
            for(int i=1;passwd[i];i++){
                cout<<"*";
            }
            cout<<endl;
            cout<<"email:   "<<email<<endl;
        };
        static void print_n(){
            cout<<"there are"<<n<<"users"<<endl;
        }
        
    private:
        string name,passwd,email;
        static int n;
    
};
    int User::n=0;

task5.cpp:

#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();
}

User.hpp:

#pragma once

#include<iostream>
#include<string>

using namespace std;

class User{
    public:
        
        User(string n,string p="111111",string e=""):name{n},passwd{p},email{e}{num++;}
        
        void set_email(){
            cout<<"Enter email address:";
            cin>>email;
            cout<<"email is set successfully..."<<endl;
        };
        
        void change_passwd(){
            
            string testpasswd;
            cout<<"Enter old password:";
            cin>>testpasswd;
            
            for(int times=1;times<3&&testpasswd!=passwd;times++){
                cout<<"password input error.Please re-enter again:";
                cin>>testpasswd;
            }
if(testpasswd!=passwd){ cout<<"password input error.Please try after a while"<<endl; }else{ cout<<"Enter new passwd:"; cin>>passwd; cout<<"new password is set successfully.."; } };
void print_info(){ cout<<"name: "<<name<<endl; cout<<"password:"; for(int i=1;passwd[i];i++){ cout<<"*"; } cout<<endl; cout<<"email: "<<email<<endl; };
static void print_n(){ cout<<"there are "<<num<<" users."<<endl; } private:
string name,passwd,email; static int num; }; int User::num=0;

测试1截图:

 

 测试2截图:

 

标签:cout,对象,void,passwd,Complex,实验,User,include
From: https://www.cnblogs.com/MaskerQwQ/p/16800942.html

相关文章

  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。建立拓扑并连接Ryu控制器,浏览......
  • 实验6:开源控制器实践——RYU
    实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本要求搭建下图所示SDN拓扑,协......
  • 实验二
    task1:#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1={3,4},c2={4.5};constcomplex<double>c3{c2};c......
  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Deskto......
  • 【计算机网络实验】NAT配置实验
    【实训目的】掌握内部网络设计过程和私有IP地址使用。验证端口地址转换工作机制。掌握路由器地址转换配置过程。验证私有地址与公有地址之间的转换过程。验证IP分组和TCP报......
  • 【计算机网络实验】单区域OSPF配置实验
    【实训目的】掌握路由器OSPF配置过程验证OSPF创建动态路由项过程验证OSPF聚合网络地址过程【实训环境】eNSP模拟软件【实验原理】配置过程分为两部分:完成所有路由器接口IP地......
  • 【计算机网络实验】虚拟局域网组建
    【实训目的】(1)掌握基于端口的vlan划分方法(2)熟悉端口的基本参数应用(3)掌握Vlan成员的添加和删除方法【实训环境】eNSP模拟软件【实验原理】虚拟局域......
  • 【个人实验报告】博客网站
    目录​​项目名称​​​​个人实验目标​​​​完成情况​​​​项目主要内容​​​​1.任务完成情况:​​​​2.项目目标实现情况:​​​​3.项目经验总结:​​​​4.存在的不......
  • 【计算机网络实验】多区域OSPF配置实验
    【实训目的】掌握划分网络区域的方法和布置掌握路由器多区域OSPF配置过程进一步验证OSPF工作机制验证OSPF聚合网络地址过程【实训环境】eNSP模拟软件【实验原理】配置过程分......
  • 【计算机网络实验】BGP配置实验
    【实训目的】验证分层路由机制掌握互联网自治系统划分方法进一步验证BGP工作机制验证自治系统之间的连通性。【实训环境】eNSP模拟软件【实验原理】BGP路由协议关键命令如下......