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

实验二 类与对象(2)

时间:2022-10-17 21:11:16浏览次数:45  
标签:const string 对象 void int Complex 实验 Employee

实验任务三:

Employee.hpp:

#pragma once
#include<iostream>
#include<string>
#include<iomanip>

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

struct Date {
    int year;
    int month;
    int day;
};
class Employee
{
public:
    Employee();
    Employee(string name0, double salary0, int y, int m, int d = 1);
    void set_info(string name0, double salary0, int y, int m, int d = 1);
    string get_name() const;
    double get_salary() const;
    void display_info() const;
    void update_salary(double s);
    void update_hire_date(int y, int m, int d);
    void raise_salary(double by_percent);
public:
    static void display_count();
private:
    string id;
    string name;
    double salary;
    Date hire_date;
public:
    static const string doc;
private:
    static int count;
};
const string Employee::doc{ "a simpe Employee class" };
int Employee::count = 0;
Employee::Employee() :id{ to_string(count + 1) } { ++count; }
Employee::Employee(string name0, double salary0, int y, int m, int d) : id{ to_string(count + 1) }, name{ name0 }, salary{ salary0 }, hire_date{y,m,d}
{++count;
}
void Employee::set_info(string name0, double salary0, int y, int m, int d)
{
    name = name0;
    salary = salary0;
    hire_date.year = y;
    hire_date.month = m;
    hire_date.day = d;
}
string Employee::get_name()const {
    return name;
}
double Employee::get_salary() const {
    return salary;
}
void Employee::display_info()const {
    cout << left << setw(15) << "id:" << id << endl;
    cout << setw(15) << "name:" << name << endl;
    cout << setw(15) << "salary:" << salary << endl;
    cout << setw(15) << "hire_date:" << hire_date.year << "-";
    cout << right << setfill('0') << setw(2) << hire_date.month << "-" << setw(2) << hire_date.day;
    cout << setfill(' ');
}
void Employee::update_salary(double s) {
    salary = s;
}
void Employee::update_hire_date(int y, int m, int d) {
    hire_date.year = y;
    hire_date.month = m;
    hire_date.day = d;
}
void Employee::raise_salary(double by_percent) {
    double raise = salary * by_percent / 100;
    salary += raise;
}
void Employee::display_count() {
    cout << "there are" << count << "employees\n";
}

task3.cpp:

#include "Employee.hpp"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << Employee::doc << endl << endl;
    Employee employee1;
    employee1.set_info("Sam", 30000, 2015, 1, 6);
    employee1.update_hire_date(2017, 6, 30);
    employee1.update_salary(35000);
    employee1.display_info();
    cout << endl << endl;
    Employee employee2{ "Tony", 20000, 2020, 3, 16 };
    employee2.raise_salary(15); // Ìá³É15%
    employee2.display_info();
    cout << endl << endl;
    Employee::display_count();
}
int main() {
    test();
}

测试结果:

实验任务四:

Complex.hpp:

#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);
    ~Complex()=default;
    float get_real() const { return real; }
    float get_imag() const { return imag; }
    void show() const;
    Complex add(const Complex& c);
    friend Complex add(const Complex& c1, const Complex& c2);
    friend bool is_equal(const Complex& c3, const Complex& c4);
    friend float abs(Complex& c);
private:
    float real, imag;
};
Complex::Complex(const Complex& c) : real{ c.real }, imag{ c.imag }{}
void Complex::show() const {
    if (imag > 0)
        cout << real << "+" << imag << "i";
    else if (imag < 0)
        cout << real << imag << "i";
    else
        cout << real;
}
Complex Complex::add(const Complex& c) {
    real += c.real, imag += c.imag;
    return Complex(real + c.real, imag + c.imag);
}
Complex add(const Complex& p1, const Complex& p2) {
    return Complex(p1.real + p2.real, p1.imag + p2.imag);
}
bool is_equal(const Complex& p3, const Complex& p4) {
    if (p3.real == p4.real && p3.imag == p4.imag)
        return true;
    else
        return false;
}
float abs(Complex& c) {
    return sqrt(c.real * c.real + c.imag * c.imag) ;
}

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

测试结果:

Attention:将~Complex()改成~Complex()=default才运行成功.

实验任务五:

User.hpp:

#pragma once
#include<iostream>
#include<string>
using namespace std;

class User {
private:
    string name, passwd, email,new_email,old_passwd,new_passwd;
    int n;
    static int count;
public:
    User(string nam) { name = nam, passwd = "111111", email = " ";++count; }
    User(string nam, string pass , string em ) { name = nam, passwd = pass, email = em; ++count; }
    ~User() = default;
    void set_email() ;
    void change_passwd();
    void print_info();
    static void print_n();

};
int User::count = 0;
void User::set_email() {
    cout << "Enter email address: " ;
    cin >> email;
    cout << "email is set successfully... " << endl;

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

}
void User::print_info()
{
    cout << "name: " << name << endl;
    int len = size(passwd);
    cout << "passwd: ";
    while (len--)cout << '*';
    cout << endl;
    cout << "email: " << email << endl;
}
void User::print_n() {
    cout << "there are " << count << " users." << endl;
}

task5.cpp:

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

测试结果1:

测试结果2:

 

标签:const,string,对象,void,int,Complex,实验,Employee
From: https://www.cnblogs.com/z-zy/p/16800695.html

相关文章

  • 【数据库】期末必知必会-----第六章 实验部分
    第六章实验部分(这一部分是考试重点)1、SQL语言的组成、特点?组成:1)DDL(数据库定义语言:CREAT、DROP、ALTER)2)DML(数据库操纵语言:INSERT、UPDATE、DELETE、SELECT)3)DCL(数据库控制语......
  • 实验二
    任务四类的定义usingnamespacestd;classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=......
  • 异常处理与生成器对象和生成器表达式
    异常常见类型syntaxError语法错误NameError当你引用了变量、模块、类、函数或代码中没有定义的其他名称时,将引发NameErrorIndexError当你尝试从序列(如列表或......
  • 实验2 类和对象(2)
    实验任务4#include"Complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(4,-3);constComplexc2(6.6);Complexc3(c1......
  • 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......