-3

I have coded a class blockbuster and namespace cs52. Would the proper way to create an object be

1) cs52::Blockbuster b( 100, "Rambo", "Sylvester Stallone" );

2.)std::Blockbuster b( 100, "Rambo", "Sylvester Stallone" );

or something different?

Code:

#include <string>
using namespace std;
namespace cs52 {

class Blockbuster{
public:
Blockbuster( int length, string title, string star );

friend bool equal( const Blockbuster & b1, const Blockbuster & b2 );
int getLength();
string getTitle();
string getPlace();

void setTitle( string title );
void setPlace( string place );
void setLength( int length );

private:
int my_Length; // feature length in minutes
string my_Title; // feature title
string my_Star; // leading actor or actress
};

}

Also, I actually didn't define my c++ file functions yet. Would be implemented as bool Blockbuster::equal(const Blockbuster & b1, const Blockbuster & b2)

or would I need to include friend somewhere in the declaration?

1 Answers1

1

using namespace std is frowned upon (considered bad practice) since it can cause conflicts. I'd suggest that you remove that line. To instantiate your object you use cs52::Blockbuster. You will never use std:: on classes you've written since this is the namespace for the Standard Library. You might also want to add some more specific namespace directives such as using std::string. Note that this is my own way of doing things, and everyone has different opinions when it comes to C++ and namespaces. For example, there is some danger in declaring namespaces in header files since it can unexpectedly change the meaning of code that includes it.

In response to your second question:

You only use the friend keyword in the declaration, not in the definition/implementation. (you declare the function as a friend function).

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
  • Although not `using namespace std;` is very good advice, it is unrelated to how a `cs52::Blockbuster` object is instantiated. – juanchopanza Nov 17 '13 at 21:02
  • They are asking how to instantiate their type. The `std` namespace doesn't even come into it. Out of their two "alternatives", only the first can work, with `using namespace std` or not. – juanchopanza Nov 17 '13 at 21:06
  • They would have problems with 2) if they actually tried it. My point is that `using namespace std;`, despite being a terrible idea, makes no difference to the fact that only 1) can possibly work. – juanchopanza Nov 17 '13 at 21:10
  • @juanchopanza Yes, and it wasn't meant to be anything more than a tip. I'm guessing you mean that I should've explained why cs52 is the way to go. – keyser Nov 17 '13 at 21:15
  • Yeah, my guess is that OP feels that somehow their class is also in the `std` namespace, which it is not. – juanchopanza Nov 17 '13 at 21:16