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

实验2 类和对象

时间:2022-10-16 15:58:36浏览次数:56  
标签:const string 对象 void passwd Complex 实验 email

实验4

#pragma once

#include<iostream>
#include<cmath>

using namespace std;

class Complex 
{
public:
    Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {}
    Complex(Complex& t) :real(t.real), imag(t.imag) {}
    double get_real() const 
    { 
        return real; 
    }
    double get_imag() const 
    { 
        return imag; 
    }
    void add(const Complex& t) 
    { 
        real += t.real; imag += t.imag; 
    }
    void show() const;

private:
    double real, imag;

private:
    friend Complex add(const Complex& p1,const Complex& p2);
    friend bool is_equal(const Complex& p1, const Complex& p2);
    friend double abs(const Complex& p);
};

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

Complex add(const Complex& t1, const Complex& t2) 
{
    Complex t;
    t.real = t1.real + t2.real;
    t.imag = t1.imag + t2.imag;
    return t;
}
bool is_equal(const Complex& t1, const Complex& t2) 
{
    if (t1.real == t2.real && t1.imag == t2.imag) 
    {
        return true;
    }
    else
    {
        return false;
    }
}
double abs(const Complex& t) {
    double T = sqrt(t.real * t.real + t.imag * t.imag);
    return T;
}
#include"Complex.h"
#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

#pragma once

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class User 
{
public:
    User(string name_0);
    User(string name_0, string passwd_0, string email_0);
    void print_info();
    void change_passwd();
    void set_email();
    static void print_n();
private:
    string name;
    string passwd,password,password_new,passwdNum;
    string email,email_new;

    static int count;
};

int User::count = 0;

User::User(string name_0) :name{ name_0 } {
    passwd = "111111";
    email = "";
    ++count;
}

User::User(string name_0, string passwd_0, string email_0) :
    name{ name_0 }, passwd{ passwd_0 }, email{ email_0 } {
    ++count;
}

void User::print_info() 
{
    int number = size(passwd);
    cout << left << setw(2) << "name: " << name << endl;
    cout << setw(2) << "passwd: ";
    while (number--) { cout << '*'; }
    cout << endl;
    cout << setw(2) << "email: " << email << endl;
}

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

void User::set_email() 
{
    cout << "Enter email address: ";
    cin >> email_new;
    email = email_new;
    cout << "email is set sucessfully..." << endl;
}

void User::print_n() 
{
    cout << "there are " << count << " users" << endl;
}
#include"User.h"
#include<iostream>

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,string,对象,void,passwd,Complex,实验,email
From: https://www.cnblogs.com/lyjlyjlyj/p/16796333.html

相关文章

  • 实验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三、实......
  • 实验二
    任务四Complex.hpp#pragmaonce#include<iostream>#include<cmath>usingstd::cout;usingstd::endl;classComplex{public:Complex(doublex0,doubley......
  • 如何选择数据存储空间?华为云对象存储服务,让你省心又划算​
    上至大企业,下至普通用户,在平日里都习惯将一些重要的文件存储在网络云盘上,这样才便捷日常的使用,随意翻出来查看也省时,畅享移动的数据存储魅力。来自华为云的对象存储服务OBS(O......
  • 实验2
    实验2.4hpp:1#pragmaonce2#include<iostream>3#include<cmath>4usingnamespacestd;5classComplex{6public:7Complex(doublea=0,......
  • 实验2 类与对象(2)
    Task4hpp文件如下#pragmaonce#include<iostream>#include<complex>classComplex{public: Complex(); Complex(doubler,doublei=0.0); Complex(cons......
  • Java基础(七)| 类、对象、封装和构造详解
    ⭐本专栏旨在对JAVA的基础语法及知识点进行全面且详细的讲解,完成从0到1的java学习,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握JAVA编程,同时为后续的框架学习,进阶开......
  • 实验四
    一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 实验一 C语言开发环境使用和编程初体验
    //实验一#include<stdio.h>intmain(){printf("OO\n");printf("<H><H>\n");printf("IIII\n");return0;}//task1_1.c#include<......
  • 实验一
    #include<stdio.h>#include<stdlib.h>intmain(){printf("o\n");printf("<H>\n");printf("II\n");return0;}#include<stdio.h>#include......