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

实验二 类和对象

时间:2022-10-18 23:24:55浏览次数:47  
标签:std const cout 对象 Complex 实验 using string

#pragma once

#include <iostream>
#include <iomanip>
#include <cmath>

using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::left;
using std::right;

class Complex {
    public:
        Complex(double a = 0.0,double b = 0.0);
        Complex(const Complex &c);
        
        const double get_real const(){
            return r;
        };
        const double get_imag const(){
            return i;
        };
        void show() const;
        void add(complex c) const;
        
        friend Complex add(const Complex &c1, const Complex &c2);
        friend bool is_equal(const Complex &c1, const Complex &c2);
        friend double abs(const Complex c1);
        
    private:
        double r;
        double i;
        
};

Complex::Complex(double a, double b) : r{a}, i{b} {
}

Complex::Complex(const Complex &c){
    r = c.r;
    i = c.r;
} 

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

void Complex::add(const Complex c) const {
    r = r + c.get_real();
    i = i + c.get_imag();
 } 
 
Complex add(const Complex &c1, const Complex &c2){
    Complex c;
    c.r = c1.r + c2.r;
    c.i = c1.ri+ c2.i; 
}

bool is_equal(const Complex &c1, const Complex &c2){
    if(c1.r == c2.r && c1.i == c2.i )
       return true;
    else
       return false;
} 

double abs(const Complex &c1){
    double s = sqrt(c1.r * c1.r + c1.i * c1.i);
    return s;
} 
#include "实验二task4.complex.hpp"
#include <iostream>

//类测试
void test() {
    using namespace std;
    
    Complex c1(4, -4);
 Complex c2(8,10);
    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();
}

 

 

#pragma once

#include <iostream>
#include <string>

using namespace std;

class User{
    public:
        User(string name0);
        User(string name0, string passwd0, string email0);
        void set_email();
        void change_passwd();
        void print_info();
        static int print_n();
        
    private:
        string name,passwd,email;
};

User::User(string name0) : name{name0}, passwd{"111111"}, email{""} {};

User::User(string name0, string passwd0, string email0) : name{name0}, passwd{passwd0}, email{email0} {};

void User::set_email(){
    cout << "Enter email adress:";
    cin >> email;
    cout << "email sets successfully..." << endl;
} 

void User::change_passwd(){
    cout << "enter old password: ";
    int i=3;
    for(i;i>0;i--)
    {
        string pd;
        cin >> pd;
        if(pd == passwd){
            cout << "Enter new password: ";
            cin >> pd;
            cout << "new password is set successfully...";
            passwd = pd;
            break;
        }
        else
        {
            if(i>1)
              cout << "password input error.Please re-enter again: ";
            else
              cout << "password input error.Please try after a while.";
        }
    }
    cout << endl;
 } 
 
 void User::print_info(){
     int k = passwd.length() ;
     string s1(k,'*');
     cout << "name: " << name << endl;
     cout << "password: " << s1 << endl;
     cout << "email: " << email << endl;
 } 
 
 int User::print_n() {
     static int n=1;
     ++n;
     cout << "There are " << n << " users." << endl;
     return n;
 }
#include "实验二task5.User.hpp"
#include <iostream>

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

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

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

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

int main() {
    test();
}

 

 

 

标签:std,const,cout,对象,Complex,实验,using,string
From: https://www.cnblogs.com/djynb/p/16804581.html

相关文章

  • 实验5:开源控制器实践——POX
    生成拓扑:点击查看代码sudomn--topo=single,3--mac--controller=remote,ip=127.0.0.1,port=6633--switchovsk,protocols=OpenFlow10阅读Hub模块代码,使用tcpdu......
  • 实验5:开源控制器实践——POX
    (一)基本要求h1pingh2、h2和h3的tcpdump抓包结果使用tcpdump验证Hub模块使用tcpdump验证Switch模块L2_learning模块代码流程图(二)进阶要求重新搭建(一......
  • 类和对象(2)
    实验4:Complex.hpp文件源码1#pragmaonce23//Complex类的定义4#include<iostream>56usingnamespacestd;78//Complex类的声明9classComp......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.能......
  • 实验二 类和对象(2)
    实验任务4h.pp1#pragmaonce2#include<iostream>3#include<cmath>4usingstd::cout;5usingstd::endl;67classComplex{8private:9doub......
  • 实验二
      #include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};constcomplex<double>c3{c2};......
  • 实验一
    task1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}task1_1#include<stdio.h>intma......
  • 实验1
    1.#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}2.#include<stdio.h>#include<math.h>intmain......
  • 实验一
    task1——1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>......
  • JavaScript学习--Array数组对象
    定义1.var变量名=newArray(元素列表);如vararr=newArray(1,2,3);2.常用:var变量名=[元素列表];如vararr=[1,2,3];访问arr[索引]=值;如arr[0]=1;ps:数组长度类型均可变 len......