Jump to content
Science Forums

Finally learning C++!


Recommended Posts

you could have used something a little more simpler like a linked list to show him the use of pointers

 

Linked lists are for little girls. Real men use Red/Black trees :)

(Btw, so there is no misunderstanding with Buffy, whom we all care about, and who's just awesome. We all know that you can outcode all of us, any day, so don't take it personally, i'm just making a joke, that's all)

 

In all seriousness you want me to write a linked list so it will make it easier for you to see the use of pointers?

 

But I'm supposed to be figuring classes in the meantime. So what I figure till now that the file handling operations I was struggling with earlier was more of the normal way in which classes are used. I figure that fstream has got to be a class that includes ofstream and ifstream, and the functions like 'open()' and 'is_open()' are no more than included functions.

Lovely. Now I actually begin to figure what this language is all about.

Just for you, my educational speech to help you understand classes.... etc.

 

Classes have members (variables) and methods (functions).

 

A class is like an object, and that is why the use of classes is referred to as Object Oriented programming. A class is, as i said, like an object, it has certain features. So take a class can be represented by an object (in programming, an object is an instance of a class) such as a ball, now you take a ball in your hands, and you immediately notice that there are certain features of 2 types. Some are features that pertain to the ball itself, such as weight, color, shape, etc.... Those in classes are called members, they are data or attributes of a particular class, so an integer for the weight of a ball, is a member of the ball class.

So eventually, after looking at the ball for a little while, you start passing it in your hands and start noticing things that it can do, such as bounce, roll, compress, perhaps change color, etc... Just like in real life, computer objects (instances of a particular class) also have these attributes, more technically actions that pertain to a specific class, and in OO speach, they are called methods, which is basically a reincarnation of functions that act on or use the class members to allow a class to do something.

 

Classes do go a step further though. There are things that they do that make sense in normal worls, but don't quite pertain to it as much as the concept of the class does. I am ofcourse referring to polymorphism, a property of classes to derive from a base class. Let's take an object like a cat and a dog and analize them. Both have 4 legs, fur, weight, color, height, age, etc..., they also both walk, eat, sleep, drink and smoke (just checking if you are paying attention), both change fur, etc. A cat is obviously not a dog, so they have things that these two objects do differently, like a dog barks, plays with the bone, etc... and a cat meows, purrs and catches mice and birds...

In procedural programming, you can already see how hard it would be to describe these things, so OO is a must. But even with OO, you would have a lot of code copied from cat to dog, because they seem to do a lot of things similarly, so inheritance and polymorphism were developed. As i have said polymorphism allows you to use methods and public members from a base class. It will make a lot more sense in a couple of seconds. Basically you know that both a cat and a dog are animals, so you can create a class called animal, which encompasses things common to most quadropods, weight, height, age, color, as well as common behaviors, like eating, sleeping... etc. Now we can create the two classes, a cat and a dog class that both derive from the animal class, but have members and methods that only pertain to a cat or a dog, such as barking and purring or meowing or whatever. Also with overloading, you can overload base members, for example if you have a cat, a dog and a bear, well obviously the eating member of a bear is quite different from a cat or a dog, but perhaps sleeping is not, well, we only need to overload the eating member, but a bear can still derive from the animal class.

 

One thing of notice with your members, generally there are 3 levels of members you can create:

 

public, accessible to any part of the program, those are members you can call from the outside of the class, such as from the main function, etc, but they can also be edited by them as well, so it's not a good practice to keep those.

 

private, members only accessible by the class (methods). This is generally how you should have your members, (as you can see, that is the way i generally code too) and then you can create methods to access these members (unlike the code above, hopefully with some checking, etc).

 

protected, members acessible by the class methods or any class that inherits from the class. This is good if you have lots of inheritance, but still need acess to the base member.

 

ugh, i gotta do work, maybe, so i'm going to stop here. if you have any questions, as always, ask...

Link to comment
Share on other sites

LOL, sanctus, thanks, i'm glad you like it, it took quite some time to write, even though it seems short. But i'm telling you, i could seriously teach people how to code better then most classes. First and biggest reason for it being that i would never teach the intro class using any programming language, even the more advanced classes i would not teach in any particular reason, i like teaching people how to think in terms of code, to know how something works, not in terms of specific code, syntax of any particular language is merely minor details if you think about it. I mean after your first 4-5 languages you can basically pick up a book on any language and be proficient with it in a couple of weeks...

Link to comment
Share on other sites

Linked lists are for little girls. Real men use Red/Black trees :)

(Btw, so there is no misunderstanding with Buffy, whom we all care about, and who's just awesome. We all know that you can outcode all of us, any day, so don't take it personally, i'm just making a joke, that's all)

:warped:

 

He said "I can't do what the big boys do", the man sat down and he cried too, :)

Buffy

 

(Sorry for the repeat Freezey! Can't get this one out of my head! And its, well, just soooooooooooooo appropriate!)

Link to comment
Share on other sites

Sorry you had to start learning on something as nasty as C++. There really are many languages out there that are much more intuitive such as Basic, Rexx, and Perl. And these days the "that's just a toy language" bs doesn't hold water anymore because you can do most anything you want in Visual Basic etc.

 

It sounds like you have it well in hand, but just know there are language out there that read much more like English

ex.

For x = 1 to 13

vs.

for (x = 0; x <= 13; x++)

 

And you don't have to keep up with those nazi semicolon's

Link to comment
Share on other sites

I won't say VB is a "toy" language--heck my company's program was until recently built in it (we've converted to C#!)--but I tell you its more verbose and its structure can drive one nuts.

 

Why must I have a statement separator (":") if two statements are on one line but I have to get rid of them if I insert a carriage return?

 

I much prefer statement separators (";" and "{}") that are concise and work consistently...

 

I can go on... But if you really want to open a VB vs. C++ thread, be my guest! (Let's not muck this one up!)...

 

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration, :warped:

Buffy

Link to comment
Share on other sites

;)

:faint::mad: <- Buffy

 

Let me reiterate myself, when i mean little girls, i mean newbie coders, not little girls literally. So don't take it personally, you know that you are too great a person for me to pick on you, I respect you too much. Oh and reiteration part, letting everyone know, one, Buffy is not a little girl, and two, she can outcode me, hands down, with her eyes closed, any day :)

 

so, friends :) ?

Link to comment
Share on other sites

Buffy is not a little girl...
Are you saying my code makes me look fat? :rolleyes:
she can outcode me, hands down, with her eyes closed, any day :evil:
Well, flattery will get you everywhere so:
so, friends :) ?

Absolutel-ly!

 

I can live two months on a good compliment, :evil:

Buffy

Link to comment
Share on other sites

I'm working on dytnamic memory allocation for the moment, and have taken on the program which sorta wont work without the concept.

 

What I note is that the operator 'new' requires a variable for the pointer, or a figure (or variable) stating how many memory location are to be saved aside.

 

But supposing we mant a more dynamic allocation; where we keep typing numbers, and keep asking for new spaces during the program execution. Sorta like the program keeps asking us if we wnat another variable, and we reply with a happy yes! Later, we are able to work with each variable separately.

 

BTW, thanks for the excellent explanation on classes.

Link to comment
Share on other sites

 

It sounds like you have it well in hand, but just know there are language out there that read much more like English

ex.

For x = 1 to 13

vs.

for (x = 0; x <= 13; x++)

 

And you don't have to keep up with those nazi semicolon's

 

Why do you call those semicolons nazi? Just because to you they are really really really annoying or because there is some history of the last century I don't know?

Just curious....

Link to comment
Share on other sites

tis called a linked list ron, or a queue, or a vector, or whatever, but there are plenty of DSes out there (depending on what you are doing, you may go with one over another) that are only limited by the hardware, some are even in the SDL lib, look at the List data structure or the queue they have in there, and you'll see what i mean. basically just continue pusing on to it, till the operator decides to stop, then work on the DS... (DS = data structure)

Link to comment
Share on other sites

  • 2 weeks later...
Why do you call those semicolons nazi? Just because to you they are really really really annoying or because there is some history of the last century I don't know?

Just curious....

 

Dictionary.com's third definition of nazi is:

a person who is fanatically dedicated to or seeks to control a specified activity, practice, etc.: a jazz nazi who disdains other forms of music;

 

Are the semicolons necessary?

No. Most of the other compilers manage to figure out the starting and ending of code phrase based purely on the syntax. If nesting is required then brackets or parentheses take care of notifying the compiler of the programmers intention.

 

Do the semicolons make the code more readable?

I;think;I;made;that;point;fairly;clear;earlier;

 

So what purpose do the semicolons serve?

They are about control. Are you willing to type out your syntax to the rhythm of the semicolon. Are you willing to bend over and say "thank you sir my I have another" semicolon. Are you willing to put that brand of terminator on your code that instantly makes it recognizable as C.

 

There is not a single purpose that a semicolon can provide that a nice carriage return can't do a better job of and be more readable in the process.

 

Let me quote a few well intentioned web sites teaching about the syntax of languages that use semi-colons:

The semicolon tells PHP that the command has finished. It is very important to watch your semicolons! If you don't, you may spend hours debugging a page. You've been warned....

yep. Thanks for the warning.

 

One of the mistakes in the language is semicolon insertion. This is a technique for making semicolons optional as statement terminators. It is reasonable for IDEs and shell programs to do semicolon insertion. It is not reasonable for the language definition to require compilers to do it. Use semicolons.

It's a mistake to make things optional?? It's not reasonable to make the computer do something??

Excel doesn't make me do it.

Word doesn't make me do it.

This Blog doesn't make me do it.

wtf makes some semicolon nazi think he has the right to mandate the use of some unnecessary symbol. Laziness and "because we have always done it that way".

Link to comment
Share on other sites

All the semicolon is, is a terminator character to tell the compiler or runtime machine that it has reached the end of a string of commands. You can make languages that have no apparent terminator character, but I quite like the capability to decided, visually, when to end my code rather than have it implicit as in the case of termination by the return character. There are occasions when it helps to be able drop down a line and continue one command.

 

if I were to type:

if (this_condition == true)
      abstract.object.search.then.get.something()
      = abstract.object.construct.and_then.do.something()

in an implicit language it wouldn't work because the (literally) stupid compiler would read it as ending the command with each line. Where as in an explicit termination language you have the ability to choose when to finish a command; thus, one is able to write multi-line segments of code and terminate at leisure.

 

In my observation, if your going to go on a rant regarding the totalitarian nature of coding languages, you might consider some of the more obvious "features" such as:

{}, These do the same thing in JAVA and C that the ; does; they just terminate the method or class instead of the individual commands.

() these are used to enclose parameters in Java and C; why use them when you could just use return characters instead? Can you imagine it?

do.something
parameter_one, parameter_two

 

Lets take it up a notch why not throw out the totalitarian nature of standardized language all together I dont like punctuation anyway it just gets in the way of my communicating exactly what I mean I mean I can just use letters in strings any way to represent what I mean right I think this makes my point rather well actually :shade:

 

The thing is, semi-colons do for C and C++ the same thing that commas, and semi-colons do for the English language. You might as well complain about the "period Nazis."

 

Personally, I prefer explicit boundaries to implicit ones; they are simply more visible and more tangible. explicit boundaries makes for more concise communication in my experience; thus, they have greater utility. I will admit though implicit boundaries are convent, but when I have to choose between clarity/control and convenience, I choose clarity. It just means you have the added responsibility to be aware of your boundaries.

 

I hope that helps,

-The Clown

Link to comment
Share on other sites

There is not a single purpose that a semicolon can provide that a nice carriage return can't do a better job of and be more readable in the process.
You're projecting dear. I know you just *love* Basic...

 

Basic does use carriage returns as a statement separator. They also thus require the use of underscores to do continuation lines and they require colons to separate multiple statements on a single line.

 

Three different characters you have to remember and apply differently where every other language mentioned in this argument uses.....one....the lowly and beleaguered semi-colon.

 

Now which is simpler?

 

I will not argue that there are many PHP implementations that do not have good enough syntax checking to not be able to give you a decent error message: they were probably built by someone who did not earn a cent for their efforts (do you wonder why I don't think much of Open Source? :shade: ), but that's completely irrelevant in arguing that semi-colons being used as a statement separator are *inherently evil*.

 

Now this argument is completely based on what the programmer has to deal with and I won't even try to go into all the horrible things that you have to do to properly lex and parse Basic as a compiler writer, but its ugly.

 

Since you didn't take my gentle hints before, I guess I have to say it explicitly:

 

If you haven't written a real compiler--or have at least studied how they work--you really don't know a thing about computer languages, and you should be careful with the stridency of your arguments about what's "good" and "bad" concerning their features.

 

You ought to *love* programming in Whitespace, :)

Buffy

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...