Computer Science Canada

wow, self teaching C++ is hard...

Author:  omni [ Wed Sep 08, 2004 7:15 pm ]
Post subject:  wow, self teaching C++ is hard...

I am trying to self teach my self C++. I've downloaded tutorials and stuff and I have Dev C++ for my IDE. C++ is a change from Turing and the 2 other languages I know (TI-Basic and TI_ASM for TI-83plus).

Question: I dont know how to start. I need some sort of first program to make. What can I do with only the standard C++ libraries?

Author:  Genesis [ Wed Sep 08, 2004 9:57 pm ]
Post subject: 

I don't know much about C++, but if you're learning it, try checking out your local library (like the builidng full of books) for programming books.

My city's library sucks, but it's got tons of programming books. Plenty on C++. Most books do a good job starting you off, and then tutorials and such are a big help. That's how I learned Java.

Good luck!

Author:  Dan [ Wed Sep 08, 2004 10:23 pm ]
Post subject: 

Well if u know turing and did that in hs, and u whont some things to do to try to learn/test your slef at C++ why not use some of the turing asments u had/have and do them in C++. Or remake some of your turing stuff in C++.

Althougth the graficks whould be a problem with just the standered libs.

Author:  Andy [ Wed Sep 08, 2004 10:24 pm ]
Post subject: 

the hardest transition is the oop, other than that, learn the syntax in a week and u'll be fine

Author:  wtd [ Thu Sep 09, 2004 1:47 am ]
Post subject: 

Make sure you're learning C++. Lots of places teach "C/C++", which isn't actually a programming language. They often consider C++ to be "C with classes." C++ is much more complex and powerful than that, and acknowledging the truth of its nature is the first step in mastering it.

Author:  omni [ Thu Sep 09, 2004 1:20 pm ]
Post subject: 

yeah, i thought about re-making my old turing programs, BUT the graphics are a problem. There aren't any standard graphics libraries, which SUCKs.

Author:  AsianSensation [ Thu Sep 09, 2004 3:22 pm ]
Post subject: 

well, there are standard graphic libraries. Technically speaking, MFC is "standard", since M$ made it.

Author:  wtd [ Thu Sep 09, 2004 4:34 pm ]
Post subject: 

AsianSensation wrote:
well, there are standard graphic libraries. Technically speaking, MFC is "standard", since M$ made it.


No, it's not.

Author:  rizzix [ Thu Sep 09, 2004 4:52 pm ]
Post subject: 

yep not even close to standard. hmm there are technically no libraries with C++. its a standalone language unlike a lot of other languages. but there is the standard template library. that usualy comes with most compilers.

Author:  AsianSensation [ Thu Sep 09, 2004 7:05 pm ]
Post subject: 

fo' real?

I always thought that MFC was standard microsoft classes? Or are there some other reasons that it's called Microsoft Foundation Classes?

Or is it because MFC was related with Visual C++? Am I just having C++ and Visual C++ confused or something?

Author:  wtd [ Thu Sep 09, 2004 7:35 pm ]
Post subject: 

AsianSensation wrote:
fo' real?

I always thought that MFC was standard microsoft classes? Or are there some other reasons that it's called Microsoft Foundation Classes?

Or is it because MFC was related with Visual C++? Am I just having C++ and Visual C++ confused or something?


There is C++, and Microsoft created a bunch of libraries in C++ which they bundled together and called MFC, and they include this all with a product they call Visual C++.

Visual C++ is not a language. It's a (bad, expensive) compiler for C++. Many of Microsoft's own libraries take advantage of the flaws and nonstandard eccentricities in Visual C++, thus tying them to that compiler.

Author:  omni [ Thu Sep 09, 2004 7:41 pm ]
Post subject: 

o yeah, if I just have a basic C++ program:

#include <iostream.h>
int main ()
{
cout << "Hello World!";
return 0;

}
and I compile and run it, the window closes before I can see the text. How do i keep the window open after execution is done and the only way I the window closes is when I close it myself?

Author:  Catalyst [ Thu Sep 09, 2004 7:53 pm ]
Post subject: 

well you could run it from the console as it is meant to, or you could add a small line to get input from the user to halt the program, such as
code:

std::string hold;
std::cin>>hold;


or you could use the un-portable (and possibly dangerous)

code:

system("PAUSE");

Author:  omni [ Thu Sep 09, 2004 7:55 pm ]
Post subject: 

Whats STD stand for (aside from sexually transmitted disease, haha)?

Author:  Dan [ Thu Sep 09, 2004 7:59 pm ]
Post subject: 

wtd wrote:
AsianSensation wrote:
well, there are standard graphic libraries. Technically speaking, MFC is "standard", since M$ made it.


No, it's not.


Thanks for saving me a rant Wink

Quote:

or you could use the un-portable (and possibly dangerous)

Code:

system("PAUSE");


Ahhhhhhhhh, ya that is evil. It is up there with goto. Uhsely there is a option in the IDE (if u are using one) that lets u make it keep the window open.

Author:  wtd [ Thu Sep 09, 2004 10:48 pm ]
Post subject: 

omni wrote:
Whats STD stand for (aside from sexually transmitted disease, haha)?


It's a shortened version of standard, as in standard namespace.

Author:  wtd [ Thu Sep 09, 2004 11:13 pm ]
Post subject: 

rizzix wrote:
yep not even close to standard. hmm there are technically no libraries with C++. its a standalone language unlike a lot of other languages. but there is the standard template library. that usualy comes with most compilers.


There actually is a standard library for C++. The STL is part of it, but isn't the whole thing.

Things like string, iostream, fstream, etc.

Other languages do have more extensive included libraries, but C++ does have a standard library that covers the basics pretty well, especially when paired with the STL. It should be noted that those languages have both libraries and a "core language". Java could be paired with an entirely different set of libraries, for instance.

Author:  omni [ Fri Sep 10, 2004 8:58 pm ]
Post subject: 

could soemone explain how to open files? Which file to you have to include.. ifstream, ofstream, fstream ??? How to read from files, display contents of files etc..
I find my tutorial confusing, or maybe i'm just tired.

Author:  wtd [ Sat Sep 11, 2004 12:01 am ]
Post subject: 

code:
#include <iostream>
#include <fstream>

int main()
{
   ifstream input_file("Input.txt");
   ofstream output_file("output.dat");

   output_file << "hello world" << std::endl;

   char input;

   input_file >> input;
}

Author:  omni [ Wed Sep 15, 2004 7:52 pm ]
Post subject: 

I have this program:
#include <iostream.h>
#include <conio.h>

int main()
{
char ch;
while(1){
if (kbhit()){
ch=getch();
cout << ch <<"\n";}
}
return 0;
}
When I press a key, the value is stored in 'ch' right? So how do I compare 'ch' to something else?
EX: if (ch ="A") <- is this right, cuse I get an error about nonvalue assignment?

Author:  Andy [ Thu Sep 16, 2004 3:34 pm ]
Post subject: 

well see in c++ = is used to define, not compare, == is used to compare to values, if u were to replace your = with ==, all should be fine, o and you cant conpare a char with a string, so you have to use 'A' instead of double quotes

Author:  omni [ Thu Sep 16, 2004 8:01 pm ]
Post subject: 

o right, forgot about the ==. Thanks

Author:  bugzpodder [ Sat Sep 18, 2004 8:54 pm ]
Post subject: 

if you are going to use STL, better put in "using namespace std;"

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
using namespace std;

Author:  Mazer [ Sat Sep 18, 2004 9:45 pm ]
Post subject: 

Not globally though. That can make your executable quite large (and possibly mess up some stuff with the intellisense in MSVC). You can use "using namespace std;" inside you main function (and wherever else you need it too I guess).

Author:  Andy [ Sat Sep 18, 2004 10:42 pm ]
Post subject: 

and that'll decrease the size? sweet...

Author:  Mazer [ Sun Sep 19, 2004 6:58 am ]
Post subject: 

No, it won't decrease the size, but it will make it less than if you have that line in the global scope. But I don't even use it now anyways. I'll just declare something as std::vector or std::string if I need to.

Author:  Andy [ Sun Sep 19, 2004 1:25 pm ]
Post subject: 

hmm but why tho? i mean why would it be different if u declared it globally

Author:  wtd [ Mon Sep 20, 2004 12:49 am ]
Post subject: 

dodge_tomahawk wrote:
hmm but why tho? i mean why would it be different if u declared it globally


I wouln't worry about its affect on executable size (at least not yet), but it's generally bad practice, There's a lot of stuff (classes, functions, etc.) in the std namespace. It's good to keep that boxed up as much as possible.

Author:  omni [ Fri Sep 24, 2004 8:43 pm ]
Post subject: 

what does it mean to overload an operator?

Author:  wtd [ Fri Sep 24, 2004 9:04 pm ]
Post subject: 

omni wrote:
what does it mean to overload an operator?


In C++ you can have multiple functions or procedures (functions which return void) with the same name, as long as they all have different signature. The signature of a function or procedure is composed of three things:


  • The type of value the function returns
  • The name of the function or procedure
  • A list of paremeters (or "arguments") the function accepts


So if we change the parameters the function or procedure accepts, we can have multiple functions with the same name.

Operators are basically just functions or procedures which take two arguments. So we can overload operators to handle data types the built-in operators don't normally deal with. The most common example is probably overloading the << operator to deal with input and output.

code:
class name {
   private:
      std::string first, last;
   public:   
      name(std::string f, std::string l) : first(f), last(l) { }
      friend std::ostream& operator<<(std::ostream& out, const name& n);     
};

std::ostream& operator<<(std::ostream& out, const name& n)
{
   return out << n.first << " " << n.last;
}

int main()
{
   name bob = name("Bob", "Smith");
   std::cout << bob << std::endl;
}


: