Split Code to Multiple (source/header/class) files in c++ and c# with functions on Visual Studio 12

Опубликовано: 04 Октябрь 2024
на канале: Arif Mahmood
362
1

Learn how to add #multiple #files to a single file #program and #divide/#split code into multiple files. How to #compile and #run a #project with multiple files. How to call #functions defined in #separate files in main function in c plus plus and c sharp.

c++ code:
//Source.cpp is a source file
#include <iostream > //for cout
#include< fstream > // for ofstream/ifstream
#include <string > //for << and getline
#include<list> //for lists
#include"Header.h"

//void pullout_contents(std::string in_file, std::list< std::string >&elements)
//{
// std::ifstream input_file(in_file.c_str());
//
// elements.clear();
// std::string line;
// for (int line_no = 1; std::getline(input_file, line); ++line_no)
// {
// elements.push_back(line);
// }
// input_file.close();
//}
//
//void show_contents(std::ostream &out, const std::list< std::string> &elements)
//{
// std::list< std::string>::const_iterator iter = elements.begin();
// for (; iter != elements.end(); ++iter)
// {
// out << (*iter) << std::endl;
// }
//}
//
//void pushin_contents(std::string out_file, const std::list< std::string> &elements)
//{
// std::ofstream output_file(out_file.c_str());
// show_contents(output_file, elements);// using polymorphism
// output_file.close();
//}


int main(int argc, char *argv[])
{
std::string in_file;
in_file = "test.txt";

std::string out_file;
out_file = "test.txt";

std::list< std::string> elements;
elements.clear();

pullout_contents(in_file, elements);

show_contents(std::cout, elements);

pushin_contents(out_file + ".out", elements);

std::cin.get();
return 0;
}


//Header.h is a header file
#pragma once
#ifndef GUARD_TOKENS_H
#define GUARD_TOKENS_H

void pullout_contents(std::string in_file, std::list< std::string> &elements);
void show_contents(std::ostream &out, const std::list< std::string> &elements);
void pushin_contents(std::string out_file, const std::list< std::string> &elements);

#endif



//Source1.cpp is another source file
#include <iostream > //for cout
#include <fstream > // for ofstream/ifstream
#include< string > //for << and getline
#include<list > //for lists
#include"Header.h"

void pullout_contents(std::string in_file, std::list< std::string >&elements)
{
std::ifstream input_file(in_file.c_str());

elements.clear();
std::string line;
for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
elements.push_back(line);
}
input_file.close();
}

void show_contents(std::ostream &out, const std::list< std::string> &elements)
{
std::list< std::string>::const_iterator iter = elements.begin();
for (; iter != elements.end(); ++iter)
{
out << (*iter) << std::endl;
}
}

void pushin_contents(std::string out_file, const std::list< std::string> &elements)
{
std::ofstream output_file(out_file.c_str());
show_contents(output_file, elements);// using polymorphism
output_file.close();
}

c# code:

#csharp #cplusplus #tutorial

Partial classes in c#