【问题标题】:To find max salary using friend function of a class having employee details(name,ID, position,salary) and print max salary details使用具有员工详细信息(姓名,ID,职位,薪水)的类的朋友函数查找最高薪水并打印最高薪水详细信息
【发布时间】:2020-12-20 05:47:36
【问题描述】:
#include <iostream.h>

#include <conio.h>

class Employee{
    int Id;

    string Name;
    string Post;        

  public:

  long int Salary;

    void GetDetails();
    void DisplayDetails();
friend void MaxSalary();

};

void Employee::GetDetails(){

    cout << "\nEnter Employee Id : ";       
    cin >> Id;      
    cout << "\nEnter Employee Name : ";     
    cin.ignore();       
    getline(cin,Name);      
    cout << "\nEnter Employee Post : ";     
    cin.ignore();       
    getline(cin,Post);      
    cout << "\nEnter Employee Salary : ";       
    cin >> Salary;

}

void Employee::DisplayDetails(){
    
    cout << "\nEmployee Id : " << Id;       
    cout << "\nEmployee Name : " << Name;       
    cout << "\nEmployee Post : " << Post;       
    cout << "\nEmployee Salary : " << Salary;

}


void MaxSalary(Employee a[], int x){
    
    long int max;
    
    for(int j=0; j<x; j++){         
        if(a[j].Salary>a[j+1].Salary)           
            max=a[j].Salary;    
    }
    
    cout<<"Maximum Salary = "<<max<<endl;

}


int main()    
{
    
    int n, i;       
    cout<<"Enter Number of Employees : ";    
    cin>>n;     
    Employee E[n];      
    cout<<"\n\n----------ENTER DETAILS OF EMPLOYEES----------\n\n";  

    for(i=0;i<n;i++){           
        cout<<"\n\n    Enter details of Employee "<<i+1<<endl;          
        E[i].GetDetails();      
    }
    
    cout<<"\n\n----------DETAILS OF EMPLOYEES----------\n\n";
    
    for(i=0;i<n;i++){       
         cout<<"\n\n    Details of Employee "<<i+1<<endl;
         E[i].DisplayDetails();    
    }    
    
    MaxSalary(E[n], n );
            
    return 0;

}

【问题讨论】:

  • 请使用tour,阅读How to Ask 并使用stackoverflow.com/editing-help 转至edit 您的帖子。事实上,它会阻止用户回答。
  • @KyleWang,我感谢您的努力,但在这种情况下,它教给我们一个错误的教训,即只有这篇文章的格式是一个问题。
  • 请提供minimal reproducible example并相应地编辑帖子
  • 这个问题没有关注你遇到的具体编程问题。 “做。这是我的代码。”不是问题。
  • @generic_opto_guy 提出这个问题和代码的事实都表明代码没有做它应该做的事情。因此,它与代码审查无关。

标签: c++ class oop friend-function


【解决方案1】:

给定的代码有很多缺陷:

  1. C++ 中不允许使用可变长度数组 (VLA)。换句话说,语法:

    cin >> x;
    Employee emp[x];
    

    无效。请注意,在这种情况下,C99 是一个例外。

  2. MaxSalary()的For循环中,给出a[j + 1],它将访问越界数组,这会调用未定义的行为。

  3. 将类中的变量声明为 public 违反了 OOP 规则:

    public:
      long int Salary; // Not a good idea
    
  4. 您不需要将[n] 作为函数参数传递:

    MaxSalary(E[n], n);
    

优化后的代码如下所示:

#include <iostream>
#include <limits>
#include <vector>

// Using this statement in order to reduce the spam of 'std::' here
using namespace std;

class Employee {
  int ID;
  string name;
  string post;
  long int salary;

public:
  Employee() {}
  // Initializing the class object
  void initEmployee(int id, string varName, string varPost, long sal) {
    ID = id,        \
    name = varName, \
    post = varPost, \
    salary = sal;
  }
  // 'salary' getter
  long getSalary() {
    return salary;
  }

  // The friend function
  friend void maxSalary(vector<Employee> data) {
    int max = 0;

    for (size_t i{1}, len = data.size(); i < len; i++)
      // Comparison
      if (data[i].getSalary() > data[i - 1].getSalary())
        max = data[i].getSalary();

    // After the loop execution, prints the maximum salary
    std::cout << "Maximum salary: " << max << endl;
  }
};

int main(void) {
  int total;

  cout << "Enter the total number of employees: ";
  cin >> total;

  vector<Employee> emp(total);

  for (int i{}; i < total; i++) {
    // Temporary variables to store data in each iteration
    Employee temp;
    string tempName;
    string tempPost;
    int tempID;
    long int tempSal;

    cout << "Name of the employee " << i + 1 << ": ";
    getline(cin, tempName);

    // Clearing the 'cin' in order to prevent the getline skips
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "Post of the employee: ";
    getline(cin, tempPost);

    cout << "Employee ID: ";
    cin >> tempID;

    cout << "Total salary: ";
    cin >> tempSal;

    // Initializing the temporary object
    temp.initEmployee(tempID, tempName, tempPost, tempSal);
    // Pushing the object into the main vector
    emp.push_back(temp);
  }

  // Comparing the vector elements (note: it is a friend function)
  maxSalary(emp);

  return 0;
}

作为测试用例:

Enter the total number of employees: 2 // Number of employees
Name of the employee 1: John Doe       // Employee 1
Post of the employee: Manager
Employee ID: 100
Total salary: 15000
Name of the employee 2: Max Ville      // Employee 2
Post of the employee: Assitant Manager
Employee ID: 102
Total salary: 50000
Maximum salary: 50000    // Maximum salary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多