How to write classes.

peoplesuc

Spy
Joined
Jul 9, 2003
Messages
170
Reaction score
0
class C_Stack //a STACK is like a pez dispenser; 'first in last out'-FILO
{
public: //these are unprotected functions
C_Stack(); //class CONSTRUCTOR
~C_Stack(); //class DESTRUCTOR
bool isFull(); //class member function;checks to see if stack is full; returns a TRUE or False
bool is Empty(); // checks stack to see if its empty; retutns T/F
void Push(int a); //puts an integer into the stack
int Pop(int &a); //takes out an integer from the stack
int Top(); //tells you whats on top of the stack
void MakeEmpty(); //cleares the stack of any info
private: //these are protected information
int MAX = 10; //the max items that can be in the stack
int key = 0; //how many items are currently in the stack
int stack[MAX]; //an array to hold all that juicy info
}; //don't forget that semi-colon at the end!$%#!
 
it's adressed to more advanced coders ;) incase it's one
 
Thanks for that, I assume there was a thread you were responding to. One thing to note, if you use the PHP tag you can keep tabs and it includes some syntax highlighting though it isn't ideal on the dark grey backgrounds. You should probably have defaulted arguments in your constructor destructor, And you CAN'T define stack with MAX unless you mark MAX a constant, but you might as well just drop a constant in for the size of stack. Probably not that interested but heres how I'd do that:

PHP:
class C_Stack //a STACK is like a pez dispenser; 'first in last out'-FILO
{
	public:
		bool isFull();
		bool isEmpty(); //no spaces in function names
		bool Push(int a); //need to return something if the stack is full
		int Pop();//don't need any arguments here you are only taking something out
		int Top();
		void MakeEmpty();

		C_Stack(unsigned int size=1); //we add the defaulted argument of size and set it to 1
		~C_Stack();
	private:
		int max; //the ctor sets this for us
		int key = 0;
		int* stack; //we will connect a dynamically allocated array here
};

I'd probably also add in operator overloaded functions, namely for pop and push (<< and >> being the obvious choice) And allow for using at least the ! operator.
 
argh i hate classes, biggest part of the AP CS Test, and we only spent 2 months learning bout them
 
heh they only get really tricky when you start working with inheritence, though what the hell did you spend the rest of the time on if not classes ? I mean objects ARE C++ ?
 
if you even learn that at school, you should be able to get it in 2 month, shouldn't you? i can't learn that at school ehre, so i have to teach it myself o_O.
 
I took that damned AP CS Test B I had to learn everything on my own through the book cause my teachers knowledege was only upto... arrays (supassed him in 3 weeks)

And classes are very easy to work with but once you get into inheratance it gets very confusing.
 
my teacher is back from the old BASIC days and crap, she spent bout 80 percent of our time going over what most of the class learned the prior year (intro to CS) so we only learned what the AP test was like and that we needed to know classes much more, in like april/may and took the test in late may if my memory serves me correctly, so we got screwed, oh well
 
In my APCS class last year we learned Java from September until February. It was kind of weird seeing how the AP test was in C++, but it gave us a really good basis for creating Classes and such when we went into C++. Plus the fact that the two languages are very similar (atleast to extent of what we needed to know), so we basically already knew most of it, and just had to focus on things like passing by reference vs. value, and some of the syntax that is different. Although we only took the A test, not the AB test, so there was stuff we didn't even talk about (like pointers and stacks). But I'm taking another programming class (accelerated, but still going through the entire language) this fall.
 
goodluck to you, i should look into more c++ leanring, cus we really got screwed. This year my school switched to Java for thier CS class, becuase the AP Test is now in Java, so, Im slowly finding Java sucks compared to C++, in speed and even more shit to write to program.

C++
cin>>
cout<<
etc

Java
(i temp forgot, but something like)
System.out.println("Java is Cool");
input i forogt completely

and you cant input int or floats directly, you need to input them as a string and then convert them to do math etc! wtf is that about

crap man.....
 
I learned "C++" in high school, and the "C++ book" we had taught everything using cout and cin, but classes were covered in an APPENDIX in the back of the book, in about 2 pages or so. I was like, "wtf? I learned C++ but i didn't learn classes?! Isn't that THE POINT of C++?!!"

Yeah that was dumb. Now I'm in college, taking Computing 3, and I'm actually learning how to use classes for real instead of just trying to pick them up from poking around with the HL1 SDK lol :)

-Syko
 
AP CS has switched to java this year. I took AP CS 'A' last year in C++, now I get to take AP CS 'AB' in java! hooray - more fun, easy class, and great resume line(s).
 
FictiousWIll, im like exactly in the same boat as you, pretty cool

And is it just me, or does Java SUCK!^#&^@ compared to C++ (see above)

;-P
 
Does C++ not handle dynamic arrays? I forget.

Bad, naughty, having MAX constant.

Stack should grow as appropriate.
 
You can make a linked list in C++... those grow dynamically and support as much as your memory can hold.
With those you can also add something to a position other than the top of the stack... if you need to for some reason.

EDIT: Maybe there was a reason for making the MAX constant.
 
Yeah probably, thats just me showing off! :cool:

gotta get a grip on that :-/
 
yes, you can dynamically allocate and expand arrays in c++.
 
:) I'm learning classes at the moment, it's in my 6th lesson of 21. Its just a beginner book, but I plan to be ready to take a PDC (Professional Development Cerificate) :D
 
agggh. i was supposed to take that, but i never paid the fee.

actually i don't think it was a pdc, i was going to get java and c++ certified.
 
i'm on the 14th hour of 24 in this sams book. Lol.
kind of slowed down once school started. I am planning on taking AP comp sci next year which switched from c++ to java this year :(
oh well.
 
Theres already a stack class in the STL (Standard Template Library). For those who dont know a template allows you to set any datatype to your class so you can have it as int, char, long or even some class. A very useful library, so yeah if I was writing this class I would use a template on it.
 
Originally posted by Plopfish
FictiousWIll, im like exactly in the same boat as you, pretty cool

And is it just me, or does Java SUCK!^#&^@ compared to C++ (see above)

;-P

Yea, Java is quite bad when it comes to handling input/output, but I guess the predefined classes that it comes with kind of balances it out....
 
*cough*javastinks*cough*
*cough*javaisslow*cough*
*cough*classpackagesstinktoo*cough*
 
wow really? C preprocessor and header files are better than java packages? wtf you smoking..

oh and the "java is slow" argument is pretty much outweighed by the speed of development. good luck with mfc D:
 
im gonna be taking programing class (VB, java and c++, in one course you get to chose what you wanna learn) and the andvanced course + linux and linux advanced. Then i hope im ready to handle a linux server :D
 
Originally posted by hax
oh and the "java is slow" argument is pretty much outweighed by the speed of development. good luck with mfc D:
I guess you don't make processor-intensive programs like modern games, photo editors, 3d modelers, etc where you need every extra bit of performance you can get.

There's a reason there are hardly any major game developers that use Java... it's just not fast enough.
 
Once you can use Java proficiently, how easily can that skill/knowledge be transfered to c++? Granted that they are different languages, but would knowing java help you to learn c++ any quicker?
 
I'm learning Java right now, and I agree that the way Java handles I/O is a little tedious, but thats with console programming. We are now getting into Swing components and the GUI API, which actually have gotten me interested in Java again. Thank god for the end of programming boring console applications.
 
Someday...
There's no programmer ...only "Syntax Manager".
you can almost make anything with console programming becuz' it lay on the nowadays computer base ground OS.,DOs., but it is harder though.
 
Originally posted by Pendragon
I learned Java 2 years before I started in C++, and I would say that most of the skills I learned in Java were easily transferrable to C++, and I breezed through most of the opening class material. I'd say that knowing either will help learn the other.

The truth is that if you know one programming laguage, learning another is quite easy. However IMNSHO Java sux. Javas principle is to be platform independent. Well, it is in some way. Another one is security. Oh hell it is secure! But... You have to pay smth for it. Your currency is called speed. (short explanation: J2ME is not as platform independent, as it's supposed to be, that's why I say that Java is almost platform independent)

That 'security stuff' (I mean garbage collection) in Java forces Java programmers to use some strange things, like initialising buffers for strings used in loop outside the loop (for (int i=0;i<10;i++) [...] is not a good idea in Java...). Why? One sure thing is that you don't want garbage collection every five or ten loop iterations...

There are much more things against Java. Guess why Nokia goes into Symbian and abandons Java?...

Sooo... Learning Java to write C++ is as good as learning bicycle riding so you can easly move to a Harley. :)

But that's what I think. Anybody disagrees? :)
 
I think that most people forget that properly written c++ code is completly cross platform. It's just that effectivly programming in c++ is not an easy task. And i don't like languages that don't give me access to the hardware when i want it, or the memory.
 
if you guys are all concerned about speed you should code in asm lol. Anyways, bickering between Java and Cpp is pretty pointless, people will pick their favorite langauge and code for it so theres no point it telling jim his bytecode sucks compiled fumes. When did programming become about bickering? Fact of the matter is we're all better than people that dont code.
 
God, it's been so long since my last computer class. I feel that I've forgotton everything I wrote in this post. lol.
 
Back to the subject: Lets see how much I can remember about writing classes off the top of my head:


DoSomething.h
Code:
#ifndef SOMETHING_H               
#define SOMETHING_H             

class DoSomething : public SomethingElse    
{   friend class AnotherThing                        
     
     public: 
            DoSomething();                            
            ~DoSomething();               
            void setFunction( int );                   
            int getFunction() const;                 
            int processFunction( int );             
    protected:
 
             bool protectedBool;                       

     private: 

             int privateInt;                               
             bool privateBool;
};

#endif


DoSomething.cpp
Code:
#include "DoSomething.h"  
                    
DoSomething::DoSomething()               
{
}

~DoSomething::DoSomething()               
{
}

 void DoSomething::setFunction( int )       
{
}

int DoSomething::getFunction() const              
{

return privateInt;

}

int processFunction( int X )                     
{
return (X * 50);
}

Never ever ever make variables public.....ever ever. The whole reason for classes is so that the user neither has to nor can manipulate the variables themselves. Making them public defeats the whole purpose of the class.
 
Pathogen said:
if you guys are all concerned about speed you should code in asm lol. Anyways, bickering between Java and Cpp is pretty pointless, people will pick their favorite langauge and code for it so theres no point it telling jim his bytecode sucks compiled fumes. When did programming become about bickering? Fact of the matter is we're all better than people that dont code.

:bounce:

On a side note, I just started learning Java at school, and I can mostly follow the C++ code that people post here. Take that for what it's worth.
 
Back
Top