Objects represent the people, places and things in your program. For example individual invoices sent and invoices received are invoices.
Classes are the units of code you write to make objects come to life in your program.
You write classes but you think in terms of objects (its why its called object orientated programming).
Think of classes as a template for creating an object.

A class defines data and function members. Most fundamentally, a class defines a new type and a new scope.
A class allows programmers that use it to not worry about the details of how it works and can can think abstractly about what it does rather than how.
A class is simply a complex data type. They are declared and used in the same way, so like when you declare a variable of type int and then assign a value to it, you declare a class and then on the next line use the new operator to assign a value to it:


	System::Data::DataDet ds;		//Declare the variable
	ds = gcnew System:Data::DataSet();	//New creates a new instance of the class

C++

In C++ we define our own data types by defining a class. A class defines the data that an object of its type contains and the operations that can be executed by objects of that type. For instance the library types string, istream and ostream are all defined as classes.

Class Locating

Typically class definitions go in header files in the bit that uses a ifdef, define so that its only included once if included in a file several times. The header file named to match the class is normal. The class functions (definitions) typically go in a corresponding .cpp file

C++/CLI Managed classes

Use ref class instead of class for a managed reference class. E.g.


	class N { ... };
	ref class R { ... };
	N* n = new N;	//Standard C++ pointer to an object
	R^ r = gcnew R;	//C++/CLI handle to an object

(See info in pointers/handles for details of classes allocation onto the heap).

Add variables and functions to a class

Use the Class View Window, right click and use Add
You can use String variables even though they are not available in the add function options. This works:


	String ^PasswordEncrypt::EncryptString(String ^password)

Property

A class such as a custom dialog box can have ‘properties’ for which you write get() and set() functions to read and write the value. This allows you to keep internal variables private and to allow controls etc to be updated when a new value is set.

Member Functions

e.g.	item1.is_value_null()

item 1 is an object (defined from a class), and is_value_null is a function in that class.
A member function is a function that is defined by a class (sometimes refered to as methods of a class).

Example of creating and using a managed class


using namespace System;	//The C++/CLI alternative to #include 

ref class Hello
{
	String^ greeting;		//(Note you can't assign a value here for a class - use a constructor)

	public:		//Members below here (until next private: or protected:) are accessible outside the class
	void Greet()
	{
		Console::WriteLine(greeting + "!");
	}

	void SetGreeting(String^ newGreeting)
	{
		greeting = newGreeting;
	}
};		//SetGreeting("Hello World");
	hello->Greet();
}

class vs struct

Using struct instead of class is exactly the same but instead of members being private before the first public: is encountered, the members are public by default. struct is basically an inheritance from C – there are no other differences than the default access level.

Defining member functions outside of a class

This is the normal approach, declare each in the class (requried), but define the actual functions outside the class. Simply include the class name and :: before the new member function:


void class_name::new_function()
{
}

Constructors

One or more constructors (more if you want overloading) are defined simply using the name of the class. e.g.


class class_name
{
public:
	class_name(): member_name_1(0), member_name_2(20) {}
}

will make member_name_1 = 0 and member_name_2 = 20.

Overloading

Two or more functions that appear in the same scope are overloaded if they have the same name but different parameter lists.

static class members

Sometimes you’ll want all objects of a particular class to access a global object (for example a count of how many objects of a particular class type have been created). Use the prefix static to define a class member as global to all class objects. You can also have static member functions…

this Pointer

this is bound to the object on which the member function is called.

Null Pointer

Use:

	if (myFile != nullptr)

to determine if the pointer myFile has been created (can be useful when trying to say close a file in an error handler if its been crteated)

Converting Classes

You can use static_class or dynamic_class:


	ToolStripMenuItem ^ClickedItem = dynamic_cast(sender);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.