Posted: Sun May 29, 2005 1:37 pm Post subject: (No subject)
It works! cool!
1 more question, After i do any programming in C++, I have to make 1 more variable, and then at the end i have to "cin" it to make the window not close itself after execution. Is there another way to do it by not creating an extra variable?
wtd
Posted: Sun May 29, 2005 1:44 pm Post subject: (No subject)
Not any particulaly elegant ways. That said, I suggest making it a separate procedure.
c++:
void pause(std::istream& in = std::cin) { char ch;
in >> ch;
}
MysticVegeta
Posted: Sun May 29, 2005 1:51 pm Post subject: (No subject)
oh lol, int way is shorter
I am not at "voids" yet
wtd
Posted: Sun May 29, 2005 3:25 pm Post subject: (No subject)
MysticVegeta wrote:
oh lol, int way is shorter
I am not at "voids" yet
Function (and procedures or "void functions") should be among the first thing you master. They permit your code to be expressive.
Consider the following two examples:
c++:
#include <iostream>
int main() { char ch;
std::cin >> ch;
return0;
}
c++:
#include <iostream>
void pause(std::istream& in = std::cin) { char ch;
in >> ch;
}
int main() {
pause();
return0;
}
Which main function tells you what the program is doing, rather than how it's doing it?
Sponsor Sponsor
MysticVegeta
Posted: Sun May 29, 2005 4:45 pm Post subject: (No subject)
oh i see so they act like procedures and can be called just like turing.
I like the File IO specially in C++, its so easy to learn and there is no "Bufferreader", "Printwriter" or whatever like Java, so its pretty straight-forward.
About the voids again, I am not taking a course on C++, our school doesn't have one
wtd
Posted: Mon May 30, 2005 2:04 am Post subject: (No subject)
MysticVegeta wrote:
About the voids again, I am not taking a course on C++, our school doesn't have one
Well, I didn't take a course in C++ either.
Install a compiler and dive in.
MysticVegeta
Posted: Mon May 30, 2005 7:37 am Post subject: (No subject)
wtd wrote:
Install a compiler and dive in.
way to go!
Also, will i be able learn the basics?
i am only 15.
MysticVegeta
Posted: Mon May 30, 2005 7:40 am Post subject: (No subject)
You told me that we use voids just like procedures but i saw people using classes. What is the difference between them?
wtd
Posted: Mon May 30, 2005 11:42 am Post subject: (No subject)
MysticVegeta wrote:
wtd wrote:
Install a compiler and dive in.
way to go!
Also, will i be able learn the basics?
i am only 15.
Sure. I probably would have learned this stuff a lot sooner, except that I didn't develop an interest in programming until 5 or 6 years ago.
wtd
Posted: Mon May 30, 2005 12:09 pm Post subject: (No subject)
MysticVegeta wrote:
You told me that we use voids just like procedures but i saw people using classes. What is the difference between them?
A procedure in C++ is just a function that returns void.
A class is a way of describing an object. What is an object, you ask?
Well, an object groups a set of data with a set of relevant functions. In the example I like to use a lot, let's consider a name class. Now, let's do it the hard way.
Now here's the problem: in this case the first and last names are two entirely separate pieces of information. If I have several names I could mix them up. But there's a better way. I can tie these two pieces of data together, and make them a single entity.
c++:
struct name
{
std::string first;
std::string last;
};
int main() {
name bob;
bob.first = "Bob";
bob.last = "Smith";
std::cout << full_name(bob) << std::endl;
return0;
}
But that's not quite right, either. We can change the individuals components of the name arbitrarily. We need to hide them; make them private. But if we do that, how do we set them in the first place?
Well, we need a constructor.
c++:
class name
{ private:
std::string first;
std::string last;
public:
name(std::string f, std::string l) {
first = f;
last = l;
} };
Now, there's a new problem. The function which gives us a full name can't actually get at the components of the name. Of course, if it were a function inside the class, it would be able to see that information just fine.
c++:
class name
{ private:
std::string first;
std::string last;
public:
name(std::string f, std::string l) {
first = f;
last = l;
}
Now, proper coding procedure in C++ says we should only ever have non-constant values when absolutely necessary. That way the compiler catches any unintended changes that might cause subtle bugs.
So, let's make our name constant.
c++:
class name
{ private:
std::string first;
std::string last;
public:
name(std::string f, std::string l) {
first = f;
last = l;
}
Now, we have a problem. When we create a class we have functions which can alter the object's data (in this case the individual component names) and functions which do not. The latter can be used even with a constant object, but the former cannot. Since full_name doesn't change anything (just creates a new string) we can specify that it's ok to use with a constant object.
c++:
class name
{ private:
std::string first;
std::string last;
public:
name(std::string f, std::string l) {
first = f;
last = l;
}