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...

user3407319
  • 227
  • 2
  • 10
  • 1
    Was half expecting at least a `static` to make this minimally interesting... – Blindy Mar 11 '14 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?? – user3407319 Mar 11 '14 at 19:24

1 Answers1

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.

linluk
  • 1,650
  • 16
  • 33
  • eh? i cant understand anything from wikipedia if u can explain it to me in a simpler way!? – user3407319 Mar 11 '14 at 19:30
  • 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. – linluk Mar 12 '14 at 05:37