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
    forums
Old 03-19-2003, 06:29 PM   #1 (permalink)
Mr.Anderson
Registered User
 
Mr.Anderson's Avatar
 
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
Mr.Anderson is on a distinguished road
Send a message via AIM to Mr.Anderson Send a message via Yahoo to Mr.Anderson
Smile Classes in C++

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.
Attached Files
File Type: zip classtut.zip (2.4 KB, 1 views)
Mr.Anderson is offline   Reply With Quote
Old 03-23-2003, 09:06 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
defining the member functions of a class inside a header function is BAD! other than the fact that it's bad form to put executable code in header files anyway, if this header was included in multiple .cpp files, you would get a "function redefinition" error by the linker.

the correct way to do this is to have a header (.h) file that contains the class declaration, and a .cpp file (usually of the same name) that defines the methods. using the author's examples:

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
Code:
/************* 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;
}
joe_bruin is offline   Reply With Quote
Old 03-24-2003, 01:55 PM   #3 (permalink)
Mr.Anderson
Registered User
 
Mr.Anderson's Avatar
 
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
Mr.Anderson is on a distinguished road
Send a message via AIM to Mr.Anderson Send a message via Yahoo to Mr.Anderson
I suppose you're right.

Quote:
Originally posted by joe_bruin
defining the member functions of a class inside a header function is BAD! other than the fact that it's bad form to put executable code in header files anyway, if this header was included in multiple .cpp files, you would get a "function redefinition" error by the linker.

the correct way to do this is to have a header (.h) file that contains the class declaration, and a .cpp file (usually of the same name) that defines the methods. using the author's examples:

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
Code:
/************* 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;
}
I suppose you're right, it's simply a matter of my (probably idiotic) tendancies to make evrything compact. Thanks for pointing that out. I owe you one.

-- Jo "Mr. Anderson" Cat

Thanks again.
Mr.Anderson is offline   Reply With Quote
Old 03-25-2003, 12:13 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
We're still getting used to this tutorial submission process.

The Original Post is the one that shows up in the tutorial viewer.

If there are agree with the corrections that joe bruin suggested, .. could you please 'edit' the first post?

This is the only way the tutorial viewer will show the correct tutorial.

Thanks
-=SDE
sde is offline   Reply With Quote
Old 04-27-2004, 08:11 PM   #5 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 59
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
am i aloud to post here?

just wanted to say that was an excelent tutorial! finally figured out what that public/privat thing was all about.
Rotkiv is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 09:48 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 10:27 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting