declarations in oop in c++?

when doing oop in c++ will i still have to declare the usuall stuff at the top? like iostream and stdafx? and will i need to add the line: using namespace std;?

OOP is just a concept of programming, not a different version itself.

stdafx is VS exclusive header file, so it is not needed in any case.
iostream probably, as it is the simplest way of input and output you may be using.

using namespace std;
Not necessarily. You can add std:: before anything that is in std namespace like std::cout;
ok thanks. but will i need a main function still, despite the fact that i will be classes now?
Yes, you will need a main(). (Or a WinMain, if you write WIN32 GUI code.)

main could just be an empty function, if you have a global application object whose constructor starts the program running. But there are issues with using globals, and even if the application object is global, I would wait until main is called to activate it. That way you know for sure that the runtime library has finished its initialization.

Note that using can also be used at function scope, so you don't have to expose the whole of the std namespace to your whole program. This is useful if only a few functions do most of the processing that uses a lot of standard containers, algorithsm, ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
// etc
// but no using namespace std;

void Test1()
{
    using namespace std;

    // do lots of stuff using strings, vectors, etc which would
    // use a lot of std::s
}

int main()
{
    std::cout << "Test 1" << std::endl;

    Test1();

    std::cout << std::endl;

    return 0;
}

Last edited on
Note that you have been using classes the whole time more and likely, i.e istream, ostream, string, etc.. These are all just classes (or data types). Now, you are just going to make your own... and don't forget the golden rule: EVERY C++ program MUST have the function main.
Last edited on
Topic archived. No new replies allowed.