简体   繁体   中英

How to iterate each object and access its member functions from array of coustom class in c++?

Please have a look at below code: I have class facultyType and in main created a array of 250 object of class. in print method passing the Faculty(array of facultyType) but when calling its program getting stopped and no compile error. i want to iterate all facultyType in print method and want to access its member functions. In java doing same working fine but i am afraid why its not working here.

#include<iostream>
#include<string>
using namespace std;
class facultyType
{
private:
    string firstname;
    string lastname;
    string department;
    double salary;
    int serviceyears;
public:
    facultyType(){}
    void print(facultyType* faculty,int count);
    void setAll(facultyType faculty[],float percent,int count);
   //setter and getter
};
void facultyType::print(facultyType* faculty,int count)//need help here
{
    int i;
    for(i=0;i<count;i++)
    {
    //want to iterate all facultyType one by one
        facultyType f=faculty[i];//here the problem

        int serviceYear;
        serviceYear =f.getServiceYears();//not getting the exact values
        cout<<"service year "<<serviceYear<<endl;
        cout<<"dfhh"<<endl;
        if(serviceYear>=15) {
            cout<<f.getFirstName()<<"\n"<<f.getLastName()<<"\n"
                <<f.getDepartment();
            cout<<f.getSalary()<<"\n"<<f.getServiceYears()<<endl;
            cout<<"-------------------------------------------------\n";
        }
    }
}

int main()
{
    facultyType Faculty[250];
    facultyType f;
    int count=0;
    int status=0;
    string fname,lastname,depart;
    double sal;
    int serviceyears;
    while(status!=4)
    {
        cout<<"1. Add new Faculty member"<<endl;
        cout<<"2. increase all faculty member salary"<<endl;
        cout<<"3. Print Employee"<<endl;
        cout<<"4. Exit"<<endl;

        cin>>status;

        switch(status)
        {
            case 1:
            {
              cout<<"Enter first name : ";
                    cin>>fname;
                   //..other code for setter and getter values
                   Faculty[count]=newFaculty;
                   count++;
                    break;
                }

            case 3:{
                f.print(Faculty,count);//calling print method
                break;
            }
             case 4: break;
        }

    }

    return 0;
}

I know its dumbest questions , but i am new in c++. Please explain why its differ than Java. Thanks in advance.

The problem is that your "getter" functions do not actually return anything. Before your edit facultyType::setServiceYears was implemented like this:

int facultyType::getServiceYears()
{
    serviceyears;
}

It should have been (adding the return keyword):

int facultyType::getServiceYears()
{
    return serviceyears;
}

This is a type of error that get caught by the compiler when warnings are enabled.

I compiled it on GNU/Linux using: g++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM