Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deconstructing C++

Status
Not open for further replies.

browolf

Programmer
Dec 18, 2001
442
GB
Hi,

I'm trying to get into C++, but i tend to learn stuff better in my own strange way ;-)
[using vc++ 6]

i had a simple program that was like

#include <iostream.h>
void main
{
char thisisyourname[30];
cout << &quot;What is your name?&quot;;
cin >> thisisanumber;
cout << endl << &quot;Your name is &quot; << thisisanumber;
}

if i've made an obvious mistakes it's cos i'm writing it from memory. (it did work)

And then i wondered about iostream.h, couldnt I take some stuff out of there and write it in the program and do away with the #include.
and after looking at webpages i came up with

// #include <iostream.h>

void main()

{
char thisisyourname[30];

istream::write &quot;what is your name?&quot;;
istream::getline (thisisyourname,30);
ostream::write &quot;You are &quot; && thisisyourname;


}


which has lots of errors like
istream' : is not a class or namespace name
and 'write' : undeclared identifier
in fact one set for every line.
i expect '&&' is wrong as well, i just guessed.

question is. why doesnt it work?


cheers

===============
Security Forums
 
now, don't quote me on theis because i'm new to c++ as well, and my system seems to be different than everyone else's (as in i hae a header <lvp\string.h> and it seems no one else does) but i'll take a stab at it...

as far as i can tell, you need to have a header file for the program that you wrote. the istream and ostream are in their respective header files and <iostream.h> comes with both of these headers already attached. are you confused enough?
if you are having difficulty understanding, then tell me; i'll try to explain better if you do.
oh, by the way...
shouldn't it be
ostream::write &quot;what is your name?&quot;;
instead of
istream::write &quot;what is your name?&quot;;
?
well, hope that helps...
~Thani


 
heh i think i got mixed up with istream and ostream.

generally i was just wondering if you could write the same program without a header file. I was suspecting that cin and cout were &quot;easier&quot; forms of something else.

but they seem to be &quot;easier&quot; forms of 'a lot' of something else.

so perhaps i should stop trying to understand things to that degree for the time being. i expect .h make more sense to ppl who know c++ better. :)



===============
Security Forums
 
ohhhh....
to make a short story even shorter, i think that you can't do an output or input statement without one of their header files unless you defined every aspect of the output and input statement in your program.
and i know I'm not advanced enough to do that, it'd take about 3-4 pages of continuous code to start it.
if you want to learn more about the header files, look at them. you can open them like normal files on the *open* menu.
it didn't help me much until i got to a bit later in my programming experience.
well, good luck and have fun!
~Thani
 
thanks. i've been looking at the header files with editplus. i'll stick to more normal stuff....i'm going write a mandlebrot program :) ===============
Security Forums
 
Open up Vis and make a new project. Give it a header and a cpp file.
in the main cpp file write the following
#include &quot;yourheader.h&quot;
int main()
{
int tempi=CItheConst;

return 0;
}
in yourheader.h put
const int CItheConst = 52;

That should compile just fine. The preprocessor goes through and parses yourheader before moving on to the main.cpp file. Comment out the include and the compiler will yell at you. as far as the compiler is concerned what you wrote was equivilent to
int main()
{
int tempi=themeaningoflife;
return 0;
}

how is it supposed to know what you mean. By scanning every file on your harddrive. Aside from beeing exessive it would mean that every function you write would have to be unique. You could contend that maybe a designated portion of the harddrive should be searched but what happens if you have two different implementations of the Standard Template Library. To help you understand some of the other errors look at the following

class airplane
{
public:
fly();
land();
bool inFlight();
private:
bool _inFlight;
};

airplane::fly(){_inFlight=true;}
airplane::land()(){_inFlight=false;}
bool airplane::inFlight(){return _inFlight;}

now if you say
airplane enolagay;
enolagay.fly();
the compiler has no problem you have defined a class and created an instance of it.
however if you say airplane::fly() which airplane do you want to fly. This is not a perfect example, but in general you must create and instance of a class to use it's member functions. If you are looking at headers all you should see is class definitions,function prototypes and declarations. The cpp file should contain all of the implementations both of regular functions and class member functions. The reason for this is that lets say you write some incredibly usefull function that you want everyone to use, but you don't want them changing. if you distribute the .h and .lib files people can still see the prototypes the compiler needs and the linker resolves the rest of it by inlcuding the .lib file. Another reason is you might want to write a function totally in ASM but still make it available to cpp programs. make a lib file distribute it with the appropriate headers. WR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top