I am a new first semester student. I wanted to work with OOP. I have a question, how can I make one class function accessible by another ? Below is my code:
#include <iostream>
#include <fstream>
usingnamespace std;
#include <filesystem>
namespace fs = std::filesystem;
// Class to define the properties
class File {
public:
// Function declaration of input() to input info
void input();
//Copy the file read by input()
void output();
};
// Function definition of input() to input info
void File::input()
{
const std::filesystem::path src = "Input.txt";
cout<<"File copied";
}
// Function output() to save the file to destination
void File::output()
{
const std::filesystem::path dst = "Hello.txt";
//But here source wont be accessible
std::filesystem::copy_file(src, dst, std::filesystem::copy_options::overwrite_existing);
// Output is the stored file
cout << "\nFile stored"<<endl;
}
// Driver code
int main()
{
// Creating object of the class
File object;
// Copying source file contents
object.input();
//Outputting data means saving it in a file.
object.output();
return 0;
}
I want input() to be accessible by output(). How can we do that
You mean you want to call input() from output()? If that is what you want you just write the function name followed by parentheses without anything in front.
#include <iostream>
#include <fstream>
usingnamespace std;
#include <filesystem>
namespace fs = std::filesystem;
// Class to define the properties
class File {
public:
// Function declaration of input() to input info
void input();
//Copy the file read by input()
void output();
private:
std::filesystem::path src;
};
// Function definition of input() to input info
void File::input() {
src = "Input.txt";
}
// Function output() to save the file to destination
void File::output() {
const std::filesystem::path dst = "Hello.txt";
std::filesystem::copy_file(src, dst, std::filesystem::copy_options::overwrite_existing);
// Output is the stored file
cout << "\nFile stored\n";
}
// Driver code
int main() {
// Creating object of the class
File object;
object.input();
//Outputting data means saving it in a file.
object.output();
}