#ifndef _MYHEADER_H
#define _MYHEADER_H
#include <conio.h>
#include <iostream>
using namespace std;
#define MAXLENGTH 256 // define a new constant
// We use C in front of the name to state that it is a class
class CMySimpleClass
{
// the members placed below the public operator is declared public
public:
void set_Number(int num); // public set function
int get_Number(); // public get function
// the members placed below the private operator is declared private
private:
// We can use m in front of our class variable name to
// state that the variable is a class member.
int mNumber;
};
class CMyClass
{
// the members placed below the public operator is declared public
public:
// class constructor (called on class initialisation)
CMyClass();
// class destructor (called on class destruction)
virtual ~CMyClass();
bool set_name(const char *name); // set name
const char *get_name(void) const; // get name
// the members placed below the private operator is declared private
private:
char *mpName;
};
#endif
#include "myclass.h" // include our header file
//////////////////////////////////////////////////////////
// MY SIMPLE CLASS
//////////////////////////////////////////////////////////
// define our set function
void CMySimpleClass::set_Number(int num)
{
mNumber = num;
}
// define our get function
int CMySimpleClass::get_Number()
{
return mNumber;
}
// By using get and set functions we are allowed to
// handle private members/variables.
//////////////////////////////////////////////////////////
// MY CLASS
//////////////////////////////////////////////////////////
// basic constructor
CMyClass::CMyClass()
{
mpName = NULL; // init our pointer
}
// basic destructor
CMyClass::~CMyClass()
{
delete [] mpName; // delete our pointer
}
bool CMyClass::set_name(const char *name)
{
mpName = new char[MAXLENGTH];
strcpy(mpName, name); // copy the string
return 1;
}
const char *CMyClass::get_name(void) const
{
return mpName;
}
#include "myclass.h" // include our new header file
// below you will see two different ways to create class objects in c++
// create an object of my simple class
CMySimpleClass objMySimbleClass;
// create an object of my class (Notice! new)
CMyClass *objMyClass = new CMyClass;
int main()
{
int number = 0;
// MY SIMPLE CLASS /////////////////////
// set a value to mNumber in my simple class
objMySimbleClass.set_Number(2006);
// get the value from mNumber in my simple class
number = objMySimbleClass.get_Number();
// display number
cout << number << " ";
// MY CLASS ////////////////////////////
objMyClass->set_name("Apron Tutorials");// pass in a name
cout << objMyClass->get_name() << endl; // get and print name
delete[] objMyClass; // delete the object (Notice! delete[])
getchar(); // wait for a key to be pressed
return 0;
}
/*
In c++ we can use object oriented programming..
The class keyword declares a class type or defines an object of a class type.
A class can be seen as a complete engine or a small "separate" program.
In this tutorial you will learn two different ways to use classes.
The members of the class can be declared:
- private members of a class are accessible only from other members of the
same class or from its "friend" classes.
- protected members are accessible, in addition to from members of the same
class and friend classes, also from members of its derived classes.
- public members are accessible from "anywhere".
When I set up private and public members in my classes I think like this:
The members that I just want to access from within my class I declare private.
The members that I need to access from outside my class I declare public.
After you have buildt your class:
class CMyClass
{
public: // public members
private: // private members
};
You can use your class like this:
Declare an object of the class containing the public members of your class.
CMyClass objMyClass; // create a object of the class
class CMyClass
{
public: // public members
CMyClass(); // constructur allways same name as the class
~CMyClass(); // destructor ~ + class name (often created virtual)
private; // private members
};
The other class is a little bit more advanced it has got a class constructor
(init function) and class destructor (destroy function). We use new to create
a new instance of this class, lik this:
CMyClass *objMyClass = new CMyClass;
If we use new and store it in the free memory we must always remember to
destroy the object with delete to make sure you free the memory, like this:
delete [] objMyClass;
You might not see the use of classes right away if you just started programming.
I know I didn't.. But once you get used to them, they make things simpler.
An example of what a class can be used for:
Have you ever played a computer game like Zonic or Super Mario?
You might have noticed that the plot of the game is collecting coins..
If you create a class for drawing one coin, you can also use this class for
drawing 100 coins, lik this: CCoin objCoin[100];
Now you have got 100 coins instead, and you just write the code for one coin..
*/
Download Visual C++ Source Code
I hope you found this c++ tutorial useful!
Don't forget to mention Apron Tutorials in your References!