Something easier than Classes

Hello, I've got a basic knowledge of C++ curently, well atleast procedural paradigm programing, but not object-oriented. To me classes look to complex to make with all the different syntax. Is there anything in the C++ world that allows me to use make functions in an external file form, like you can do with classes, so that I can reuse the functions in multiple programs and also to divide a program into multiple files so that it is easier to edit and read. Thanks.
closed account (D80DSL3A)
Sure. It's easy.
Option 1: You could put your function definitions in a header file. You won't need any additional .cpp file this way.

myFuncs.h
1
2
3
4
int add(int x, int y)
{
	return x+y;
}


Option 2: If you wish to "split up" the prototypes from the definitions place the prototypes in
myFuncs.h
 
extern int add(int x, int y);

then place the definition in myFuncs.cpp
1
2
3
4
int add(int x, int y)// no extern keyword here. That goes in the .h file
{
	return x+y;
}


Either way, you then use them in main like this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using std::cout;
using std::endl;
#include "myFuncs.h"// the path to your functions

int main()
{
	cout << "3+4 = " << add(3,4) << endl;
	return 0;
}


I wrote some of my 1st games this way. I think about any task could be done with global functions alone. Classes aren't strictly necessary. Do learn to use them though at some point. The OO thing is powerful and very cool.
Last edited on
Classes did look kind of intimidating to me, but once you actually start messing with them and figuring out how to set them up, they are very easy to use.

What you can use instead are structs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct tempStruct
{
	std::string name;
	std::string DisplayName(){return name;};
	void CoutName();
};

void tempStruct::CoutName()
{
	std::cout << name << std::endl;
}

int main()
{

	tempStruct name1;
	name1.name = "name";
	name1.CoutName();
	std::cout << "Hello! " << name1.DisplayName() << std::endl;

	std::cin.get();
}


structs are very easy to use.
The basic concept behind classes is very, very easy. And structs are classes.
I wasn't going to tell him that, it might scare him away since they are classes :P.
fun2code, do i have to have any syntax at the beginning of the header file, or do I just start writing functions (referring to Option 1)?
Topic archived. No new replies allowed.