虚函数更改变量C ++

时间:2017-03-27 07:30:27

标签: c++

我是Stack overflow和C ++的新手我希望能够在不同的类中使用一个函数,但是为新类更改一个变量。

所以在下面的代码中我在Class Business中有函数readfile,我有一个由class Customer: public Business调用的新类客户。

我想为每个人打开一个不同的文件。在Business文件中是" Business.csv"在客户中,它是" Customer.csv"但当我调用Customer readfile时,它打开了" Business.csv"文件。

如何让它打开" Customer.csv"文件? (另外,我将使用相同的功能打开其他文件! 以下是基类

// Base Class Business
int Business::readfile(string file) // Read the file and store to vector
{
    // create loops to take in necessary information   
    ifstream openfile; // create instance of ofstream function For reading the file
    openfile.open(file); // Open file
    if (!openfile)
    { // check if file opened, if not return with error message
        cout << "Error, cannot open Business.txt file \n";
        return(0);
    }
    // open the file and count the number of lines so we know when to stop and where to add a new line
    while (openfile)
    {
        getline(openfile, buffer, '\n'); //Open the file and read until return
        i++; // count the number of lines
    }
    openfile.close();

    // open the file and store the data into a vector
    openfile.open(file); // Open file
    cout << file;
    for (row = 0; row<(i - 1); row++) // loop to go through rows to i
    { 
        vec.push_back(vector<string>()); //create an empty row in vector
        for (column = 0; column<j; column++) // loop to go through the columns
        {       
            getline(openfile, buffer, '\t'); // read a string until next tab
            vec[row].push_back(buffer); //add data to column of vector  

        }
        cout << setw(30) << setfill(' ') << vec[row][0]; // print data out onscreen
        getline(openfile, buffer, '\n'); // read a string until return
    }
    cout << endl;
    openfile.close();
    return(0);
}

0 个答案:

没有答案