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

实验二 类和对象(2)

时间:2022-10-16 18:33:24浏览次数:64  
标签:const 对象 double void Complex 实验 User include

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

#pragma once

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
class Complex{
    public:
    Complex(double r=0,double i=0);
    Complex(const Complex&c);
    double get_real()const;
    double get_imag()const;
    void show()const;
    void add(const Complex& c); 
    friend Complex add(Complex &c1,const Complex &c2);//加const根据task4中第9和33行 
    friend bool is_equal(Complex &c1,const Complex &c2);//加const根据task4中第9和30行 
    friend double abs(Complex&c);
    
    private:
        double real,imag;
};
Complex::Complex(double r,double i):real{r},imag{i}{}

Complex::Complex(const Complex&c):real{c.real},imag{c.imag}{}

double Complex::get_real()const{
    return real;
}

double Complex::get_imag()const{
    return imag;
}

void Complex::show()const{
    if(imag>0)
        cout<<real<<" + "<<imag<<"i";
    else if(imag==0)
            cout<<real;
         else
             cout<<real<<" - "<<-imag<<"i";
}

void Complex::add(const Complex& c){
    real+=c.real;
    imag+=c.imag;
}

Complex add(Complex &c1,const Complex &c2){
    return Complex(c1.real+c2.real,c1.imag+c2.imag);
}

bool is_equal(Complex &c1,const Complex &c2){
    return(c1.real==c2.real&&c1.imag==c2.imag);
}

double abs(Complex&c){
    return sqrt(c.real*c.real+c.imag*c.imag);
}

task 4.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();
}

运行结果:

 

  5. 实验任务5 设计并实现一个用户类User,并在主函数中使用和测试这个类。 User.hpp:  
#include<iostream>
#include<string>
using namespace std;
class User{
public:
    User(string na,string pa="111111",string em="");
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n(); 
    
private:
    string name,passwd,email;
    static int n;
};
    int User::n=0;

User::User(string na,string pa,string em):name{na},passwd{pa},email{em}{
    n++;
}

void User::set_email(){
    string email1;
    cout<<"Enter email address: ";
    cin>>email1;
    email=email1;
    cout<<"email is set successfully…"<<endl; 
}

void User::change_passwd(){
    string old_passwd,new_passwd;
    int count=1;
    cout<<"Enter old password: ";
    cin>>old_passwd; 
    while(count<=3){    
        if(old_passwd!=passwd){
                cout<<"passwd input error.Please re-enter again:";
                cin>>old_passwd;
                count++;
                if(count==3){
                    cout<<"passwd input error.Please try after a while."<<endl;
                    break;
                }
        }
        else{
                cout<<"Enter new password: ";
                cin>>new_passwd;
                passwd=new_passwd;
                cout<<"new passwd is set successfully…"<<endl;
                break;
            }            
    }
}

void User::print_info(){
    string passwd0(passwd.length(),'*');
    cout<<"name:\t"<<name<<endl;
    cout<<"passwd:\t"<<passwd0<<endl;
    cout<<"email:\t"<<email<<endl;
}

void User::print_n(){
    cout<<"there are "<<n<<" users.";
}

task 5.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();
}

 

运行结果:

测试一:

测试二:

 

 

标签:const,对象,double,void,Complex,实验,User,include
From: https://www.cnblogs.com/laver-8/p/16796482.html

相关文章

  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环......
  • Python第六章实验报告
    一.实验内容:《零基础学Python》第六章实例和实战,以及一道作业题二.实验环境:IDLEShell3.9.7三.实验目的和要求:掌握定义和调用函数、变量的作用域、匿名函数、参数传递、......
  • 实验1
    1#include<stdio.h>2intmain()3{4printf("O\n");5printf("<H>\n");6printf("II\n");7return0;8}   1#include<st......
  • 实验2 类与对象(2)
    一、实验内容:实验任务4:1.Complex.hpp:#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublex=0,doubley=0......
  • 实验5:开源控制器实践——POX
    基础要求实验总结本次实验的难度相较于之前的几次实验要有所提升,本次实验中运用POX控制器,这使得我熟悉了这一控制器的安装方法及其基础使用方法,同时对于在前几......
  • 实验2 类和对象
    实验4#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doubler=0.0,doublei=0.0):real(r),i......
  • 实验1
    实验一  #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");return......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实......