Teen Programmers Unite  


Return to forum top

help with class & member function

Posted by syner [send private reply] at July 19, 2002, 08:40:06 PM

i am just looking for help on the below. i know the code for the sorting operations. i know that i have to include the member functions in the public and use a constructor since it is out of the scope range. my question is how do i include it in the public ie, bool Bsort() or int Bsort() as well as do i put the code in the function part after the constructor? is that where the exact code goes for the function itself? please help!!!

1. Use the Linked List program handed out in class (Sample Program 2 on my Website) and generate a linked list.
2. Add two member functions to the List class performing two separate sorting operations.
3. Use the functions in step(2) to sort the linked list of step (1).

<< code removed by mutual consent of all involved parties =) >>

Posted by Psion [send private reply] at July 19, 2002, 08:58:49 PM

This isn't a homework help message board. Who thinks I should edit the post to remove the code? =)

Posted by Zandalf [send private reply] at July 19, 2002, 09:14:27 PM

psion - yes, because he could easily have posted a link instead of all that bloody code.

Posted by syner [send private reply] at July 19, 2002, 09:20:44 PM

Do what you may but i am just trying to learn how to program like everyone else on this board. we are all at different levels of understanding is all. i am not just asking for the answers. i am throwing out what i understand and in hopes that someone out there is wise enough to explain where i am going wrong is all. i apologize for any inconvenience. not my intent.

Posted by diegoeskryptic [send private reply] at July 19, 2002, 09:22:30 PM

lol @ psion... it seems as if this site is having a increasing amount of newcomers...Which I think is good.. however I just hate it when there are people that come to this site just to post one thing (such as fake ass job offers or homework help)... i think it is such a waste...

Posted by Zandalf [send private reply] at July 19, 2002, 09:30:51 PM

syner - I'm not trying to be mean or malicious, honest. It's just that there are rules for the forums posted at the top of every thread listing, and when people ignore them, it's slightly annoying. ^_^

We'd love to help you, but it helps to describe your problem instead of hurriedly typing a little unintelligible blurb and slapping a huge block of code with it. From what I can gather, you want to know a) how to make a member function public, and b) how to put code in a constructor. I assume that since you're working with classes, and that you know how sorting works, that you would know that most (if not all) of the code you posted is completely unnecessary. So just ask you question, and post the code later if someone asks for clarification as to what you're talking about.

Yaay! I get to try to be a nice person and fail miserably. Good times...

Posted by syner [send private reply] at July 19, 2002, 10:00:08 PM

ok gotcha. sorry for the long post. i take it this board is not for people new to programming as the other poster states "it seems as if this site is having a increasing amount of newcomers...Which I think is good.. however I just hate it when there are people that come to this site just to post one thing (such as fake ass job offers or homework help)... i think it is such a waste... " while i admit that this is for a class of mine it does not take away from the fact that i am trying to learn the language and not just asking for someone to do my homework. i apologize for not knowing half as much as the aforementioned poster on C++ programming (even though i am sure there are many out there who could discredit his questions also since they are beyond him also) but i am interested in learning about classes and member functions as it relates to the level i am on at this point in my studies. if this is not a board for sharing ideas and cultivating a growing community of programmers then just say it and i will post no more. i can't help it if my questions relate to what i am currently learning. so back to my question, rephrased as to hopefully not offend anyone:
1. when including member functions in classes i put a reference (sort of like a prototype def) in the public or private part. yes or no?
2. how do i determine what type of variable i need. in my case i need to use a sort routine which also prints out sorted so i would probably not use bool since i dont want to know if it is true or false. would i use void ( i dont want to really return a new value just sort the ones i have) or do i use any type?
3. where does the actual code go? after my scope resolution operator? i guess they call that the function scope. class scope is defined by what? everything within the class itself or all of the function scopes together?

Posted by Zandalf [send private reply] at July 19, 2002, 11:53:56 PM

syner - Much better, you learn very quickly, and yes, we are trying to be supportive, just some people (myself included sometimes) think they're all high and mighty because they've been here before, and so get really pissed off when a newcomer ruins the little paradise they think they've created. Enough ranting, on with your questions.

1) I've never heard it called a reference before, but a definition, yes. this means you're going to have code looking like this:

class MyClass
    void PublicFuncion(int);

    int MyData;
    int PrivateFunction();

In this example, obviously, PublicFunction() is public, and PrivateFunction() is private.

2) this is a serious question, and can go into long hours of debate, but the simple answer is to understand what the basic variable types are, and understand what you need to be doing. for a sort function, usually a void return is fine. You only need to return a bool if there is a change that the sort will fail. The other option is to return a pointer to the beginning of the sorted list, but this is only if you want to and are somewhat bored ^_^.

3) where does the code go? I'd want to say in the .cpp file, but from the other questions, it seems like you're having some more problems than that. Let's look at the code for PublicFunction() from the above example, and maybe that will help explain a little better.

void MyClass::PublicFunction(int num)
    if(num > 0 && num < 1000)
        MyData = num;

The scope resolution operator is the ::, so yes, the code does go after the scope resolution operator, but not _directly_ after. Class scope is all the variables in the class. This is why I can reference MyData directly, instead of having to type MyClass::MyData. Function scope is usually called local, and local variables live for the lifetime of the function (created when the function is called, deleted afterward), and therefore are not included in any other scope, because they don't exist outside of the function.

I hope that helps a little, and good luck with C++. I love it, but it definently takes a passion to learn quickly, otherwise it's a long and arduous process.
Posted by diegoeskryptic [send private reply] at July 19, 2002, 11:59:39 PM

my fault for the mean comment syner.. I was just talking in general ne way...

Posted by syner [send private reply] at July 20, 2002, 01:01:17 AM

thanks for the replies. you helped clarify some of the questions which plague me. on your reply #3 you stated that the code gos into the .cpp which i understand. what i really meant to ask is where does the actual snippet of code for the sort routine itself go? in what you call the local? see if i get this almost right: 1. i create the prototype member function in public for my class with the correct return type (in this case you recommend that void is fine). 2. next i create the "local" for that particular member function including the :: to reference to correct class. now my question is do i use the sort code within the local here or somewhere else? 3. finally i have to make the function call in the main() program to make use of the class. Is this what they mean by "object" also or are the objects different members within the class itself? i am confused as to what "objects" are as far as classes go. sorry to waste your time. i know this is boring stuff for the members here. i'm just trying to figure all of this out and my teacher is really not a help. thanks again for all of the replies.

Posted by Zandalf [send private reply] at July 20, 2002, 01:23:13 AM

syner - okay, the local code block for your sort function will be between the {} after the List::sort().

also, to make the call from main, first you have to create an instance of the object. a class definition basically creates a type (just like int or float) that you can use anywhere else in your program. sooo, in main, all you have to do is make a variable of type List, such as:

List myList

Then, to call the sort function, hopefully after you have put some things in the list using other member functions, you're going to call List.sort(), just like you would any other function. notice that I use the member selection operator (the . operator) on list to select the member function sort. and really, that's all there is to it.

and don't worry, you aren't wasting anyone's time. People won't waste their time if they can aviod it, and so since I'm trying to help, I don't consider it a waste of my time ^_^
You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.