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

实验二,类与对象

时间:2022-10-12 22:22:06浏览次数:46  
标签:real const cout 对象 imag Complex 实验 include

task.4

Complex.hpp

#include<iostream>
#include<math.h>

using namespace std;

class Complex{

public:
        
    Complex (float r = 0, float i = 0) : real{r}, imag{i} {}
    Complex (const Complex &obj) : real{obj.real} , imag{obj.imag} {}
    
    float get_real() const { return real; }
    float get_imag() const { return imag; }
    
    void show() const {
        if(imag > 0)
            cout << real<< " + " << imag << "i"<<endl;
         if(imag < 0)
            cout <<real<< " - " << -imag << "i"<<endl;
            if(imag==0)
            cout << real<<endl;
    }
    
    void add(const Complex &obj){
        real += obj.get_real();
        imag += obj.get_imag();
    }

    friend Complex add(const Complex &a, const Complex &b){
        return Complex(a.real + b.real, a.imag + b.imag);
    }

    friend bool is_equal(const Complex &a, const Complex &b){
       if(a.get_imag()==b.get_imag()&&a.get_real()==b.get_real())return true;
       else return false;
    }

    
    friend float abs(const Complex &obj){
        return sqrt(obj.real * obj.real + obj.imag * obj.imag);
    }

private:
    float real, imag;
};

 

 task4.cpp

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

void test(){
    using namespace std;
    
    Complex c1(1, 4);
    const Complex c2(2.34);
    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();
}

测试结果1

测试结果二

 

 

 

 

 

task.5

User.hpp

#include<iostream>
#include<string>

using namespace std;

class User{
    public:
        User(string n,string p="111111", string e="");
        void set_email(){
        cout<<"Enter email address:"<<endl;    
        cin>>email;
         cout << "email is set successfully..." << endl;
        }
        void change_passwd();
        void print_info()const;
        void static print_n(){
            cout<<"there are "<<n<<" users"<<endl;
        }
                private:
            string name,passwd,email;
            static int n;
    
    
}; 

User::User(string n1,string p,string e):name(n1),passwd(p),email(e){
n++;
}
int User::n=0;
void User::change_passwd(){
    cout<<"Enter old password:";
    string c;
    int count=1;
    cin>>c;
      while (c!=passwd&&count<3) {
        cout << "password input error.please re-enter again: ";
        cin >> passwd;
        count++;
        if (count == 3) {
            cout << "password input error.please try after a while." << endl;
        }
    } 
    
    if (count!=3) {
        cout << "enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
}
    void User::print_info()const{
        int l=passwd.length();

        cout<<"name: "<<name<<endl;
        cout<<"password: ";
        while(l--){cout<<"*";
        }cout<<'\n';
        cout<<"email: "<<email<<endl;
    }

    

task5.cpp

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


void test() {
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("lucy", "123456", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";
         
    User user2("Alice");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main() {
    test();
}

测试结果1

测试结果二

测试结果三

实验总结

 const用法不太熟悉

语句还不够精炼

 

标签:real,const,cout,对象,imag,Complex,实验,include
From: https://www.cnblogs.com/akumanpower/p/16786294.html

相关文章

  • 实验4:开源控制器实践——OpenDaylight
    利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。进阶实验:获......
  • 实验任务1
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");......
  • 实验5 开源控制器实践——POX
    实验5开源控制器实践——POX基础部分HubSwitchL2_learning模块代码流程图进阶部分自定义一个POX模块SendFlowInSingle3frompox.coreimportcoreimport......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践--OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 实验1
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");return0;}......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 实验1
    任务一#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");return0;}  #include......
  • Microsoft Visual Studio 2010 Service Pack 1 安装失败:系统无法找到指定的对象
    MicrosoftVisualStudio2010ServicePack1安装失败:系统无法找到指定的对象vs2010学习版安装错误在此计算机中仅有部分MicrosoftVisualStudio2010产品已升级到Ser......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......