|
||||||
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++ ClassA class is a simple representation of a real object. The class will have:
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:
The customer may also need some functions:
And anyone can recognize this as representing a customer even with just these few variables and functions. Why Use a ClassOnce a class has been created the programmer can:
And, as always, the best way to understand any programming technique is to see it in action. How to Declare a ClassClasses 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:
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. SummaryA C++ class is a simple but effective way of reusing code. The class consists of:
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.
Comments
Oct 10, 2009 5:08 AM
Guest :
1 Comment:
|
||||||
|
|
||||||
|
|
||||||