1

in c++ what is the difference between using a namespace and a class??

like:: in this example i added namespace

#include <iostream>
using namespace std;

namespace ns{

void print(){

cout<<"Hello, World!";

}

}

int main(){

ns::print();

return 0;
}

vs: and in this one i added a class

#include<iostream>
using namespace std;


class cs{

void print(){

cout<<"Hello World!";


}    

}


int main(){

cs classOject;
classObject.print();

return 0;
}

but both got me the same result;;; that question made me keep thinking for a week

thanks for any replies guys and all repliers are much appreciated...

2
  • 1
    Was half expecting at least a static to make this minimally interesting... Commented Mar 11, 2014 at 18:04
  • so to get this @manlio all you are trying to tell me, a namespace can be used by making using namespace and call the function directly but the class must have an object and call the objectName.functionName(); is this all the difference?? Commented Mar 11, 2014 at 19:24

1 Answer 1

1

they are too different to describe here in detail. i recommend you to read something about oop.
classes are definition of objects, and namespaces can be used to build logical groups of code.

Sign up to request clarification or add additional context in comments.

2 Comments

eh? i cant understand anything from wikipedia if u can explain it to me in a simpler way!?
The simpliest way is to learn and understand the concepts of object oriented programming, then you will understand what classes are for, and then you will see the difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.