5 Replies - 1840 Views - Last Post: 03 December 2009 - 12:02 PM Rate Topic: -----

#1 rogerg0834  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-December 09

I dont understand "using namespace std;" can someone explain

Post icon  Posted 03 December 2009 - 11:33 AM

I am a first year computer science major and I am taking intro C++ class at school. My question is this. What is "using namespace std;" used for and why does it have to be included in my .h file as apposed to my other programs i had to include it in my main .cpp. For example I had to write a class that is derived from the STL string class and add 1 member function that encrypts a string by converting each letter into ASCII then shifts to the next ASCII number then back to letter and displays the encrypted string. I go it to work but ran into some, what I consider, weird things to get the program to complie and run, namely the "using namespace std;" Here is the code I came up with, please please bear in mind that I am COMPLETLY NEW to programming and the only experiance I have is what I have read and learned so far in school. This is my first semester :) I am using Dev-C++ IDE because I think my professor doesnt like Microsoft very much and says that Visual whatever isnt ANSI C compliant? Something to do with scope wasnt correct or something I dont know w/e. I have enough trouble learning C++ at this point to worry about programmer/professor likes/dislikes. Anyway thanks to all in advance.

//File EncryptableString.h -- EncryptableString class specification file


using namespace std;//  ** why does this have to go here and not in assignment9.cpp as all my other programs???** 

//   EncryptableString class declaration with inheritance from string class
class EncryptableString:public string
{
  public:

	//   function that encrypts any user input string
	void encrypt();

	//   Destructor
	~EncryptableString();

  private:  
	
};


//File EncryptableString.cpp -- EncryptableString class function implementation file

#include <iostream>
#include "EncryptableString.h"


//*********************************************************
//			  encrypt function						  *
//This function will encrypt any user input string by	 *
//converting every character in the string to an integer  *
//and shifting it to the next ASCII value and then back   *
//to a character for display.							 *
//*********************************************************
void EncryptableString::encrypt()
{//   for loop to go through all chars in string
	  for (int i = 0; i < this->size( ); i++)
	  {
		int num = 0;//   temp variable

		num = static_cast <int>(this->at(i));//   Use static_cast to change char to int. Get the chars 1 at a time using at() string member function
		
		  if (num == 90)//   check for upper case 'Z'
		  {
			num = 64;//   set num = to 1 before 'A'
		  }
		  else if (num == 122)//   check for lower case 'z'
		  {
			num = 96;//   set num = 1 before 'a'
		  }
	  
		num += 1;//   add 1 to int stored in num
	  
		cout << static_cast <char> (num);//   Use static_cast to convert int stored in variable num to a char
	  }
}//end encrypt

//*****************************************
//	   Destructor for this object	   *
//										*
//*****************************************
EncryptableString::~EncryptableString()
{
   if (this > 0 )
   {
	 //  Clears the string and sets it to size to 0
	 this->clear();
   }
}




//File assignment 9.cpp --String Encryption program
//
//Write a class EncryptableString that is derived from the STL string class. The
//Encryptable string class adds a member function void encrypt()
//That encrypts the string contained in the object by replacing each letter with it successor
//in the ASCII ordering. Example "baa" would be encrypted to "cbb"
//Also the successor of "z" is "a" and "Z" is "A".

#include <iostream>
#include "EncryptableString.h" 


int main()
{  //   Declare EncryptableString variable
   EncryptableString userString;

   //   User message prompt
   cout << "Please enter a sentence to be encrypted.\n"
   		<< "Example 'my dog ran outside'\n\n";

   //   Read-in user input
   getline(cin, userString);

 
   cout << '\n' << "Here is your sentence encrypted."<<endl;
   
   cout << '\n';

   //   Call member function encrypt
   userString.encrypt();


return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: I dont understand "using namespace std;" can someone explain

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,409
  • Joined: 13-June 08

Re: I dont understand "using namespace std;" can someone explain

Posted 03 December 2009 - 11:37 AM

uh...It shouldn't...You should never use a using statement in a .h. It's better practice to just write out the STD so that your .h doesn't get a bunch of extra crap towed along with it.

Otherwise, the only reason it's there is so that...Actually, there is no reason except he didn't want to type std::string...and...because there's no include statement...how does that even compile?

Here's a question: The using statement acts as an include?

This post has been edited by ccubed: 03 December 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 rogerg0834  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-December 09

Re: I dont understand "using namespace std;" can someone explain

Posted 03 December 2009 - 11:44 AM

View Postccubed, on 3 Dec, 2009 - 10:37 AM, said:

uh...It shouldn't...You should never use a using statement in a .h. It's better practice to just write out the STD so that your .h doesn't get a bunch of extra crap towed along with it.

Otherwise, the only reason it's there is so that...Actually, there is no reason except he didn't want to type std::string...


Thanks for your reply. Actually, I wrote this code :) Its the code I came up with to solve the program. I am not sure why it had to be there. I placed it in the assignment9.cpp and the comiler kept giving me an error saying something like "expected class name after token "{" blah blah. So I was completly confused as to what the heck it was complaining about and I started moving around the "using namespace std;" and lo n behold the program complied and ran when I placed it the .h file. So, now i have no idea what the hell "using namespace std;" is for :((
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: I dont understand "using namespace std;" can someone explain

Posted 03 December 2009 - 11:49 AM

I don't know the internals, but I wouldn't think that adding using drags anything along with it. I believe it just assists the compiler in the Koenig lookup process.

You can get by in this case without the using by just qualifying your reference to string to be std::string in the .h file.

class EncryptableString : public std::string {

Was This Post Helpful? 0
  • +
  • -

#5 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,409
  • Joined: 13-June 08

Re: I dont understand "using namespace std;" can someone explain

Posted 03 December 2009 - 11:59 AM

Another lie it seems. I just tried a simple program like this with and without using.

cout << "test" << endl;
cin.get();



With or without ,both were 463 KB. So i guess it doesn't matter.
Was This Post Helpful? 0
  • +
  • -

#6 rogerg0834  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-December 09

Re: I dont understand "using namespace std;" can someone explain

Posted 03 December 2009 - 12:02 PM

Yeah, iam not sure whats up with that. Thanks ccubed for noticing that I forgot the #include in the .h file and also thanks to JackOfAllTrades for fixing the class declaration statment :))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1