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

实验二 类和对象

时间:2022-10-18 14:24:41浏览次数:26  
标签:const cout 对象 void Complex 实验 using include

实验四

.cpp

#include "Complex.hpp"
#include <iostream>
void test() {
    using namespace std;

    Complex c1(7, -8);
    const Complex c2(7.8);
    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();
}
.hpp
#pragma once #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double a = 0, double b = 0) :real{ a }, imag{ b } {}; Complex(const Complex& p) :real{ p.real }, imag{ p.imag } {}; void show()const; double get_imag()const; double get_real()const; void add(const Complex& p); friend bool is_equal(const Complex& a, const Complex& b); friend double abs(const Complex& p); friend Complex add(const Complex& a, const Complex& b); private: double real;double imag; }; void Complex::show()const { if (real == 0 && imag == 0) { cout << 0; } else if (real == 0 && imag != 0) { cout << imag << "i"; } else if (real != 0 && imag == 0) { cout << real; } else if (real != 0 && imag != 0) { if (imag < 0) { cout << real << imag << "i"; } else { cout << real << "+" << imag << "i"; } } }; double Complex::get_imag()const { return imag; } double Complex::get_real()const { return real; } double abs(const Complex& p) { return sqrt(p.get_real() * p.get_real() + p.get_imag() * p.get_imag()); } Complex add(const Complex& a, const Complex& b) { Complex c(a.get_real() + b.get_real(), a.get_imag() + b.get_imag()); return c; } void Complex::add(const Complex& p) { real += p.get_real(); imag += p.get_imag(); } bool is_equal(const Complex& a, const Complex& b) { bool i; if (a.get_real() == b.get_real() && a.get_imag() == b.get_imag()) { i = 1; } else { i = 0; } return i; }

实验五

.cpp

#include "User.hpp"
#include <iostream>
#include <string>
using namespace std;
void test() {
    using std::cout;
    using std::endl;
    using std::string;

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

.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();
    void change_passwd();
    void print_info();
    static void print_n() 
    {
        cout << "there are " << num << " users.";
    }
private:
    string name, passwd, email;
    static int num;
};
int User::num = 0;
void User::set_email() 
{
    cout << "Enter email address: " ;
    string email_;
    cin >> email_;
    email = email_;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() 
{
    string o, n;
    int count = 1;
    cout << "Enter old password: ";
    cin >> o;
    while(o!= passwd) 
    {
        cout << "password input error. Please re-enter again: ";
        cin >> o;
        count++;
        if(count == 3)
         {
            cout << "password input error. Please try after a while. " << endl;
            return;
        }
    }
    cout << "Enter new password: ";
    cin >> n;
    passwd = n;
    cout << "new passwd is set successfully..." << endl;
}
void User::print_info() 
{
    string password(passwd.length(), '*');
    cout << "name:\t" << name << endl;
    cout << "passwd:\t" << password << endl;
    cout << "email:\t" << email << endl;
}
View Code

 

标签:const,cout,对象,void,Complex,实验,using,include
From: https://www.cnblogs.com/shun16/p/16802409.html

相关文章

  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验6: 开源控制器实践——RYU
    开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境U......
  • JavaScript的日期对象的使用Date
    1<script>2letdate=newDate();//返回的是当前的时间3console.log(date.getFullYear());//获取的当前年份4console.log(date.getMonth()+1);......
  • 面向对象
    面向对象编程面向对象指以属性和行为的观点去分析现实中的事务在面向对象时只分析该对象是否具备该属性和行为类名当由多个单词构成首字母必须大写当成员变量由多个单......
  • 实验一
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;} #include<stdio.h>intmain(){printf("......
  • 面向对象语言的三大特性:封装,继承,多态
    封装:可以将事物的属性和行为抽象出来,封装在一个类中。继承:子类可以从基类上继承其(全部或部分)属性和函数。多态:多态是指一个接口,对应多种实现。C++的多态性具体体现在编译......
  • [答疑]商品的规格是不是应该建模为值对象
    阿华2018-11-2821:59咨询下各位,商品的规格是不是应该建模为值对象?这样对他们的增删不会影响到其他地方。比如一个酒品有200ml,500ml两种规格,管理员后来改成了500ml和700ml,......
  • [答疑]多个对象(红圈)在EA中怎么画出来的
    lihongwei(62***407)14:39:02多个对象(红圈)在EA中怎么画出来的?潘加宇(3504847)16:13:55这个画不出来,如果要表示这个是多个,右击对象,Advance→Multiplicity......
  • 《MiniPRO H750开发指南》第五十六章 串口IAP实验
    第五十六章串口IAP实验​IAP,即在应用编程,通俗地说法就是“程序升级”。产品阶段设计完成后,在脱离实验室的调试环境下,如果想对产品做功能升级或BUG修复会十分麻烦,如果硬件支......
  • 《MiniPRO H750开发指南》第五十七章 USB读卡器(Slave)实验
    第五十七章USB读卡器(Slave)实验​STM32H750系列芯片都自带了USBOTGFS和USBOTGHS(HS需要外扩高速PHY芯片实现,速度可达480Mbps),支持USBHost和USBDevice,MiniPROSTM32H75......