Home > other >  How to print the name of every salesman from Salesman class?
How to print the name of every salesman from Salesman class?

Time:09-17

Here I implemented a c program using OOP concepts. Let's say there are 1000 salesmen (1000 Salesman Class objects) in a company. If I want to print the name of each and every salesman (using a loop or something), how to do it? Not only for printing the name, let's say the company wants to find the average salary spent. It can be easy that if I can run over all objects in the class.

This is my code (I put the code here just for reference)

#include <string>
#include <iostream>

class Salesman
{
private:
    int salesmanId;
    string salesmanName;
    int salary;
    string contactNumber;

public:
    Salesman();
    ~Salesman();
    void setSalesmanDetails(int id, string name, int sal, string number);
    void displaySalesmanDetails();
    void setSalesmanContactNumber(string number);
};

void Salesman ::setSalesmanDetails(int id, string name, int sal, string number)
{
    salesmanId = id;
    salesmanName = name;
    salary = sal;
    contactNumber = number;
}

void Salesman ::displaySalesmanDetails()
{
    cout << "Salesman ID: " << salesmanId << endl;
    cout << "Salesman Name: " << salesmanName << endl;
    cout << "Salary: " << salary << endl;
    cout << "Contact Number: " << contactNumber << endl
         << endl;
}

void Salesman::setSalesmanContactNumber(string number)
{
    contactNumber = number;
}

Salesman::Salesman()
{
}

Salesman::~Salesman()
{
}

int main(int argc, char *argv[])
{
    string number;
    Salesman *s1 = new Salesman;
    s1->setSalesmanDetails(1, "John", 30000, "772358375");

    Salesman *s2 = new Salesman;
    s2->setSalesmanDetails(2, "Ann", 40000, "773029452");

    Salesman *s3 = new Salesman;
    s3->setSalesmanDetails(3, "Leema", 35000, "778294526");
}

CodePudding user response:

Just make a class that stores a vector that holds SalesMan objects. Give that class the ability to add salesMan and another member function to iterate through all the salesmen and print the details.

class SalesMen
{
public:
    SalesMen() = default;

public:
    void AddSalesMan(const Salesman& salesMan)
    {
        salesMen.push_back(salesMan);
    }

    void PrintDetails()
    {
        for (auto& salesMan : salesMen)
        {
            salesMan.displaySalesmanDetails();
        }
    }

private:
    std::vector<Salesman> salesMen;
};

int main(int argc, char* argv[])
{

    SalesMen salesMen{};

    Salesman s1;
    s1.setSalesmanDetails( 1, "John", 30000, "772358375" );
    salesMen.AddSalesMan(s1);

    Salesman s2;
    s2.setSalesmanDetails(2, "Ann", 40000, "773029452");
    salesMen.AddSalesMan(s2);

    Salesman s3;
    s3.setSalesmanDetails(3, "Leema", 35000, "778294526");
    salesMen.AddSalesMan(s3);

    salesMen.PrintDetails();
}

CodePudding user response:

For the output of a class, you need to overwrite the insertion operator <<. In this, you do nearly the same as in your display function. With that, you can output your code to std::cout or a file or any other stream.

Please read about operator overloading and especially the inserter and the extractor.

And in your main function, you do can simply use a std::vector to store your salesman.

And then output verything in a simple loop using the above insertion operator.

Please see the below example code:

#include <string>
#include <iostream>
#include <fstream>
#include <vector>

using std::string;
using std::cout;
using std::endl;

class Salesman
{
private:
    int salesmanId;
    string salesmanName;
    int salary;
    string contactNumber;

public:
    Salesman();
    ~Salesman();
    void setSalesmanDetails(int id, string name, int sal, string number);
    void displaySalesmanDetails();
    void setSalesmanContactNumber(string number);
    
    friend std::ostream& operator << (std::ostream& os, const Salesman& sm) {
        return os << "Salesman ID: " << sm.salesmanId << endl
            << "Salesman Name: " << sm.salesmanName << endl
            << "Salary: " << sm.salary << endl
            << "Contact Number: " << sm.contactNumber << endl  << endl;
        
    }
};

void Salesman ::setSalesmanDetails(int id, string name, int sal, string number)
{
    salesmanId = id;
    salesmanName = name;
    salary = sal;
    contactNumber = number;
}

void Salesman ::displaySalesmanDetails()
{
    cout << "Salesman ID: " << salesmanId << endl;
    cout << "Salesman Name: " << salesmanName << endl;
    cout << "Salary: " << salary << endl;
    cout << "Contact Number: " << contactNumber << endl
         << endl;
}

void Salesman::setSalesmanContactNumber(string number)
{
    contactNumber = number;
}

Salesman::Salesman()
{
}

Salesman::~Salesman()
{
}

int main(int argc, char *argv[])
{
    std::vector<Salesman> salesman{};

    
    string number;
    
    Salesman s1;
    s1.setSalesmanDetails(1, "John", 30000, "772358375");
    salesman.push_back(s1);

    Salesman s2;
    s2.setSalesmanDetails(2, "Ann", 40000, "773029452");
    salesman.push_back(s2);

    Salesman s3;
    s3.setSalesmanDetails(3, "Leema", 35000, "778294526");
    salesman.push_back(s3);
    
    for (const Salesman& sm : salesman)
        std::cout << sm;
    
    
}
  • Related