How to Create a C++ Class

An introduction to Object Oriented Programming

© Mark Alexander Bain

Feb 21, 2009
Creating a C++ Class, Mark Alexander Bain
C++ is an object oriented programming language and the concept of the class is therefore essential to a programmer.

C++ is, at its heart, an object oriented programming language. The importance of object oriented programming is, of course, that is enables a programmer to reuse code easily, thereby minimizing the time and effort that they have to spend on any one project.

The C++ programmer must, therefore, understand how to create and to use classes - the building blocks of an object oriented program.

What is a C++ Class

A class is a simple representation of a real object. The class will have:

  • variables - also known as properties, data members or fields
  • functions - also known as methods or member functions

These are used to define the characteristics and functionality of an object. If, for example, a programmer is using the concept of a customer in their programming then the customer may have a set of properties:

  • an account number
  • a first name and surname
  • an amount due
  • a particular currency that they work in

The customer may also need some functions:

  • buying a new item
  • printing out a invoice

And anyone can recognize this as representing a customer even with just these few variables and functions.

Why Use a Class

Once a class has been created the programmer can:

  • create multiple instances of a class
  • reuse classes in more than one program

And, as always, the best way to understand any programming technique is to see it in action.

How to Declare a Class

Classes may be declared in the files where they will ultimately be used. However, it is much better practice to define them in separate files. The classes can then be reused easily in any new application. However, there are actually 4 stages to creating and using a C++ class:

  1. create a header file containing the class declaration
  2. create a C++ file containing any functionality for the class
  3. call the header from the C++ application being developed
  4. compile the files containing the class functionality and the new application

The starting point is, therefore, to create a header file:

class Customer {
public:
Customer(void);
int invoice();
int set_amount_owed (float additional_amount = 0);
char* currency;
int account_number;
char* surname;
char* firstname;
private:
float amount_owed;
};

It's worth noting that the way that functions and variables can be defined as public or private. Anything defined as public can be used by the application using the class. Anything defined as private may only be used within the class - they are not visible to the C++ application.

This code must be saved into a text file (for example one named customer.h) and then it included in a C++ file (e.g. customer.cpp). The functionality for the class can then be defined in this new file:

#include <iostream.h>
#include "customer.h"
Customer::Customer(void) {
cout << "Creating Customer...\n";
account_number = 0;
currency = "$";
amount_owed = 0;
}
int Customer::invoice() {
cout << "Invoice for " << firstname << " " << surname << "\n";
cout << "Account Number: " << account_number << "\n";
cout << "Amount Due: " << currency << amount_owed << "\n";
return 0;
}
int Customer::set_amount_owed (float additional_amount = 0) {
amount_owed += additional_amount;
return 0;
}

Now the class header file can be included in a C++ application and it's functionality is immediately available to the programmer:

#include <iostream.h>
#include "customer.h"
void main () {
Customer Fred;
Fred.account_number = 1;
Fred.surname = "Smith";
Fred.firstname = "Fred";
Fred.set_amount_owed(100.51);
Fred.invoice();
return 0;
}

The final step is to compile and run the application:

> gxx -oinvoices customer.cpp invoices.cpp
> invoices

This new customer class can now be used in any C++ application that the programmer creates.

Summary

A C++ class is a simple but effective way of reusing code. The class consists of:

  • a header file that contains definitions for the variables and functions for the class. These can be either private or public
  • a C++ file containing each function body - the actual functionality

A programmer can, in this way, create a model of a object and then use this in their applications.


The copyright of the article How to Create a C++ Class in C Programming is owned by Mark Alexander Bain. Permission to republish How to Create a C++ Class in print or online must be granted by the author in writing.


Creating a C++ Class, Mark Alexander Bain
The C++ Class Header File, Mark Alexander Bain
Functionality for the C++ Class, Mark Alexander Bain
Using a C++ Class in an Application, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Oct 10, 2009 5:08 AM
Guest :
Excellent, logically formulated tutorials. many thanks to author
1 Comment: