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

实验2 类和对象(2)

时间:2022-10-17 20:47:03浏览次数:40  
标签:const cout 对象 void float Complex 实验 include

实验任务4

#include "Complex.hpp"
#include <iostream>
void test() {
    using namespace std;
    Complex c1(4,-3);
    const Complex c2(6.6);
    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<cmath>
using namespace std;
class Complex {
public:
    Complex(float r = 0, float i = 0) :real{r},imag(i){}
    Complex(const Complex& c);
    float get_real() const { return real; }
    float get_imag()const { return imag; }
    void show()const;
    void add (const Complex& c);
    friend Complex add(const Complex& c1, const Complex& c2);
    friend bool is_equal(const Complex& c1, const Complex& c2);
    friend float abs(Complex& c);
private:
    float real, imag;
};
void Complex::show()const
{
    if (imag > 0)
        cout << real << "+" << imag << "i" << endl;
    else if (imag == 0)
        cout << real << endl;
    else
        cout << real << imag << "i" << endl;
}
Complex::Complex(const Complex& c)
{
    real = c.real, imag = c.imag;
}
void Complex::add(const Complex& c)
{
    real += c.real, imag += c.imag;
}
Complex add(const Complex& c1, const Complex& c2)
{
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
bool is_equal(const Complex& c1, const Complex& c2)
{
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else
        return false;
}
float abs(Complex& c)
{
    return sqrt(c.real * c.real + c.imag * c.imag);
}

实验任务5

#include "User.hpp"
#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();
}
#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class User 
{
public:
    User(string n, string p = "111111", string e = "") :name{ n }, passwd{ p }, email{ e } { count++; }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout<<"there are "<<count<<" users."<<endl; }
private:
    static int count;
    string name, passwd, email;
};
int User::count = 0;
void User::set_email()
{
    cout << "Enter email address:" << endl;
    cin >> email;
}
void User::change_passwd()
{
    int i;
    string passwd1;
    cout << "Enter old passwd:" << endl;
    for (i = 0; i < 3; i++)
    {
        cin >> passwd1;
        if (passwd1 == passwd)
        {
            cout << "Enter new passwd:" << endl;
            cin >> passwd1;
            passwd = passwd1;
            cout << "new password is set successfully..." << endl;
            break;
        }
        else
        {
            if (i == 2)
                cout << "password input error.Please try after a while" << endl;
            else
                cout << "password input error.Please re-enter again:" << endl;
        }   
    } 
}
void User::print_info()
{
    int len = passwd.length();
    string s1( len,'*' );
    cout << "name:" <<setw(8)<< name << endl;
    cout << "passwd:" << setw(8) << s1<< endl;
    cout << "email:" << setw(8) << email << endl;
}

测试1截图:

测试2截图:

实验总结:
1.解决了上一次实验存在的问题,即“const”常量使用的原因和方法。

2.可以较熟练的利用“static”对某一类事物共同属性的存储和使用,掌握了数据共享的工具—“friend”即友元函数和友元类的使用方法。

3.拓展了对C++标准库的使用范围,包括字符串“string”类的一些定义和函数的使用以及格式化输出的方法。

标签:const,cout,对象,void,float,Complex,实验,include
From: https://www.cnblogs.com/zlaym/p/16800568.html

相关文章

  • VCL窗体和控件是如何实例化成对象运行的?
    VCL类库的窗体及每个控件都是一个类(比如TForm1、TButton),可以用类来创建一个对象,这个对象就可以运行了。C++Builder设计期新建的窗体及放置在窗体上的控件,IDE会自动创建窗......
  • 异常处理与生成器对象
    异常常见类型异常类型有很多种异常类型就是我们一段代码在运行时遇到bug终止运行.返回给我们一段报错信息,其中就有异常类型。SyntaxError语法错误NameError名字错误......
  • 异常处理、生成式对象
    1.异常常见类型SyntaxError:语法错误NameError:名字错误,一般由于变量名未定义造成IndexError:索引错误,列表的索引值超过了范围KeyError:字典键错误,字典的键找不到Inde......
  • [2022.10.17]面向对象之类与对象
    面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)技术方法有两种调用方式1.通过创建主函数的对象来调用方法2.通过把“static”修饰符把方法可以直接调用函......
  • win10启动ubuntu报错 参考的对象类型不支持尝试的操作
    问题:在Windows10系统下启动Ubuntu20.04连接WSL2,显示参考的对象类型不支持尝试解决方案:1.临时有效通过管理员身份打开WindowsPowerShell打开之后输入以下命令重置Wins......
  • 实验1C语言开发环境使用和数据类型,运算符,表达式
    #include<stdio.h>#include<stdlib.h>intmain(){printf("0\n");printf("<H>\n");printf("II\n");printf("0\n");printf("<H>\n");pr......
  • 实验二 类与对象
    实验任务一#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};//支持两个参数//使用时,在语法层面......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网......
  • 深入理解python面向对象编程(python基础语法004)
    ......
  • 异常处理、生成器对象、生成器表达式
    异常处理、生成器对象、生成器表达式目录异常处理、生成器对象、生成器表达式一、异常常见类型二、异常处理语法结构三、异常处理的补充四、异常处理的实战应用五、生成器......