ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

10.20

2021-10-24 03:01:04  阅读:212  来源: 互联网

标签:10.20 const string int void Complex Employee


#include<complex.h>
#include<iostream>
#include<cmath>

int main()
{
    using namespace std;
    
    complex<double> c1(3,4);
    complex<double> c2(4.5);
    complex<double> c3(c2);
    
    cout << " c1 = " << c1 << endl;
    cout << " c2 = " << c2 << endl;
    cout << " c3 = " << c3 << endl;
    
    cout << " c1 + c2 = " << c1 + c2 << endl;
    
    cout << boolalpha;//设置布偶型bool值以true/false方式输出
    cout << " c1 == c2 : " << (c1 == c2 ) << endl;
    cout << "c3 == c2 : " << (c3 == c2 ) << endl;
    
    cout << "abs(c1) = " << abs(c1) << endl;//abs()对复数进行取模运算,头文件cmath
    complex<int> c4(3,4),c5(2,7);
    
    cout << "c4 - c5 = " << c4 - c5 << endl;      
}

#ifndef EMPLOYEE_HPP
#define EMPLOYEE_HPP

// Employee类的定义
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Date
{
    int year;
    int month;
    int day;
};

// Employee类的声明
class Employee
{
public:
    Employee();
    Employee(string name0, double salary0, int y, int m, int d);
    void set_info(string name0, double salary0, int y, int m, int d); // 设置雇员信息
    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);  // 
    static void display_count();
private:
    string name;   // 雇员姓名
    double salary; // 雇员薪水
    Date hire_date;  // 雇员雇佣日期
    static int count;   // 用于记录雇员总人数
};

int Employee::count = 0;

// 默认构造函数
Employee::Employee()
{
    ++count;
}

// 带参数的构造函数
Employee::Employee(string name0, double salary0, int y, int m, int d): 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 << "name: " << name << endl;
    cout << "salary: " << salary << endl;
    cout << "hire_date: " << hire_date.year << "-" << setfill('0') << setw(2) << hire_date.month << "-" 
                                                                   << setw(2) << hire_date.day;
}

// 更新薪水
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;
}

// 雇员提薪加成
// by_percent是提升比例
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";
}

#endif
#include "Employee.hpp"
#include <iostream>

int main()
{
    using namespace std;

    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);
    employee2.display_info();
    cout << endl << endl;

    Employee::display_count();
}

 

#include<iostream>
#include<cmath>

using namespace std;

class Complex
{
public:
    Complex(int x = 0, int y = 0 );//不能在一个类里面同时声明一个默认构造器,和一个带有默认参数的构造函数; 
    //构造函数不能被申明为const; 
    Complex(const Complex &c);
    
    int get_real() ;
    int get_imag() const;
    void show() const;//因为c2为const所以如果在这里不声明为const就会放大c2的权限 
    void add(Complex c) ;

private:
    int real;
    int imag;
    
    friend Complex add(const Complex c1,const Complex c2);
    friend bool is_equal(const Complex c1,const Complex c2);//友元函数不能被声明为const因为它不属于类里面的东西
    //所以当友元函数和构造函数的参数为const时,形参需要被定义为const; 
    friend int abs(const Complex c);
};
//
Complex::Complex(int x, int y  ) //声明中构造函数同时声明缺省参数,而在构造函数的定义中不用再次声明缺省参数。 
{
    real = x;
    imag = y;
}

Complex::Complex(const Complex &c)
{
    real = c.real;
    imag = c.imag;
}

int Complex::get_real() 
{
    return real;
}
 
int Complex::get_imag() const
{
    return  imag;
}

void Complex::show() const
{
    cout << real << imag << "i" << endl;
}

void Complex::add(Complex c) 
{
    real += c.real;
    imag += c.imag;
}

Complex add(const Complex c1,const Complex c2)
{
    Complex a;
    a.real = c1.real + c2.real;
    a.imag = c1.imag + c2.imag;
    return a;
}

bool is_equal(const Complex c1,const Complex c2)
{
    if( (c1.real == c2.real) && (c1.imag == c2.imag))
    {
        return true;
    }
    
    return false;
}

int abs(const Complex c) 
{
    int a;
    a = sqrt( ( c.real) * (c.real) + (c.imag) * (c.imag));
    return a;
    
}
#include "Complex.hpp"
#include <iostream>

int main()
{
    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;
    
    return 0;
}
#include<iostream>
#include<string>

using namespace std;

class User
{
public:
    User(string theName, string thePassword = "111111",string theEmail = "");
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() ;

private:
    string name;
    string password;
    string email;
    static int count;
};



User::User(string theName, string thePassword,string theEmail)
{
    name = theName;
    password = thePassword;
    email = theEmail;
    count++;
}

void User::set_email()
{
    cout << " Enter email address:";
    cin >> email;
    cout << "email is set succesfully\n";
}

void User::change_passwd()
{
    string input; 
    cout << "Enter old password:";
    cin >> input;
    int n = 2;
    while((n != 0) && (input != password))
    {
        n--; 
        cout << "password input error. Please re-enter again:";
        cin >> input;
    }
    
    if(input == password)
    {
        cout << "Enter new password:";
        cin >> password;
        cout << "new passwd is set successful..." << endl; 
    }
    
    if(n == 0)
    {
        cout<< "Please try after a while" << endl;
    }
}
 
void User::print_info()
{
    cout << "name: " << name << endl;
    cout << "passed: ******\n";
    cout << "email: " << email << endl;
     
}

void User::print_n() 
{
    cout << "there are " << count << "uesr.\n"; 
}
#include "User.hpp"
#include <iostream>

int User::count = 0;//count是静态成员,为其分配空间需要在主函数的文件内,主函数前进行 
int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

 

 

 

实验总结:

1.不能在一个类里面同时声明一个默认构造器,和一个带有默认参数的构造函数;

2.构造函数不能被申明为const,友元函数不能被声明为const因为它不属于类里面的东西,所以当友元函数和构造函数的参数为const时,形参需要被定义为const;

3.因为c2为const所以如果类的函数不声明为const就会放大c2的权限 

4.声明中构造函数同时声明缺省参数,而在构造函数的定义中不用再次声明缺省参数。

5.为静态成员分配内存要在主函数的文件内

 

 

标签:10.20,const,string,int,void,Complex,Employee
来源: https://www.cnblogs.com/2967271912lala/p/15450180.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有