Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Classes in C++
      by Mr.Anderson
 Page 1 of 1 
   

(Login to remove green text ads)
A programming tutorial: C++ classes
Written by Jo "Mr. Anderson" Cat.

Greetings and salutations, citizens.
I'm Mr. Anderson, and I've come to teach you about classes in C++. This is actually my first C++ tutorial, but don't let that deter you. In reading this tutorial, I'm assuming you have a remotely basic knowledge of C++. Code snippets are tabbed at least one space. Let's dive right in.

A class, in its most basic form, is a struct with functions.
Easy enough.

To create a class named NiceClass, start with...
Code:
#ifndef NiceClass_h #define NiceClass_h
Notice that these statements don't have semicolons at the end.
They don't need them because they're special. In this sense,
all #ifndef and #define do is check to see if something else already included them.

Next, we add to our code by tagging on the 'introductory' part of the class. This tells the compiler that this is the name of the class, and this is where it begins.
Code:
#ifndef NiceClass_h #define NiceClass_h class NiceClass {
We then go on to add the _private_ members of our class.
Isn't that neat. Private members are variables that are only accessible to the internal functions of the class. This is to be sure you don't accidentally change one of them.
Code:
#ifndef NiceClass_h #define NiceClass_h class NiceClass { private: int NiceNumber; char NiceLetter;
Notice that there's no initialization in this step, only declaration.
You _CANNOT_ initialize a variable in the private or public class, leave that for the constructor. We'll get to that right about... wait for it... now.
Code:
#ifndef NiceClass_h #define NiceClass_h class NiceClass { private: // Can't be accessed by anyone but the class. int NiceNumber; // A number. char NiceLetter; // A letter. public: NiceClass(); // The constructor! void setNiceNumber( int ); // This sets the value of the number. void setNiceLetter( char ); // This sets the value of the letter. int getNiceNumber( void ); // This returns the value of the number. char getNiceLetter( void ); // THis returns the value of the letter. }; // END!
Pretty simple. Though there's something missing.
Code:
#ifndef NiceClass_h #define NiceClass_h class NiceClass { private: int NiceNumber; // A number. char NiceLetter; // A letter. public: NiceClass(); // This is the all important constructor. This doesn't have any return type. void setNiceNumber( int ); // This sets the value of the number. void setNiceLetter( char ); // This sets the value of the letter. int getNiceNumber( void ); // This returns the value of the number. char getNiceLetter( void ); // THis returns the value of the letter. }; #endif // The real end!
Of course, for every beginning, there must come an end. Just add an #endif and you've created the class.
So, we need to actually make the functions of the class. Do this by saying...

whatever classname::classfunction( whatever )

Though it should be noted that all functions should be included in a .cpp file. NOT the header file. (Thanks to Joe_Burin for pointing this out.)

In our case...
Code:
// NiceClass.h #ifndef NiceClass_h #define NiceClass_h class NiceClass { private: // Can't be accessed by anyone but the class. int NiceNumber; // A number. char NiceLetter; // A letter. public: NiceClass(); // This is the all important constructor. NO RETURN TYPE! void setNiceNumber( int ); // This sets the value of the number. void setNiceLetter( char ); // This sets the value of the letter. int getNiceNumber( void ); // This returns the value of the number. char getNiceLetter( void ); // THis returns the value of the letter. }; // END! #endif // NiceClass.cpp #include "NiceClass.h" NiceClass::NiceClass() { NiceNumber = 0; NiceLetter = 'a'; } void NiceClass::setNiceNumber( int userNumber ) { NiceNumber = userNumber; } void NiceClass::setNiceLetter( char userLetter ) { NiceLetter = userLetter; } int NiceClass::getNiceNumber( void ) { return NiceNumber; } char NiceClass::getNiceLetter( void ) { return NiceLetter; }
Whew. That's a lot to learn, but don't worry. Once you get the hang of it, writing classes becomes a real time saver. To access your class, save the above text as a header file (.h, usually), then
Code:
#include "NiceClass.h"
and declare a variable like you would in your main function...
Code:
NiceClass NiceVariable;
Then, to set a value...
Code:
NiceVariable.setNiceLetter('s');
And to return a value...
Code:
SomeOtherVariable = NiceVariable.getNiceLetter(); cout << NiceVariable.getNiceLetter();
That's all, folks. I hope this was helpful to you.
If you have any questions or comments,
e-mail me at FloatingPoint001@hotmail.com
with the subject "YourTutorial".

Best of luck to you.
--Jo "Mr. Anderson" Cat

Post Scriptum:
I've attached the .cpp and .h files.
I've tested the script, but I'm far from perfect.
If you see something that's wrong, e-mail me.

Many thanks to Joe_Burin for pointing out a reather significant error. :)




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle