Coding & programming
Installing CodeBlocs compiler
C language
C++ language
© The scientific sentence. 1998-2016
| Â |
|
Programming with C/C++ Language
Classes in C++
1. Introduction
C++ uses also the object-oriented approach. As the name object-oriented programming
suggests, this approach deals with objects.
Classes are collections of data related to a single object type. Classes not only include
information regarding the real world object, but also functions to access the data, and
classes possess the ability to inherit from other classes.
If a class, functions are the only way to modify the variables in this structure,
and they are usually the only way even to access the variables in this structure.
This principle itself is called encapsulation. The key idea is that the
outside world doesn't need to know exactly what data is stored inside the class. It
just needs to know which functions it can use to access that data.
2. Classes in C++
The syntax for these classes is simple. First, we put the keyword 'class'
then the name of the class. Our example will use the name Orange. Then we put
an open bracket.
Before putting down the different variables, it is necessary to put the degree
of restriction on the variable.
There are three levels of restriction.
The first is public, the second protected, and the third private.
The public restriction allows any part of the program, including parts outside the class,
to access the functions and variables specified as public.
The protected restriction prevents functions outside the class to access the variable.
The private restriction is similar to protected.
The syntax for declaring these access restrictions is the restriction keyword :
public, private, protected, and then a colon.
Finally, we put the different variables and functions we want to be part of the class.
We only put the function prototype(s). Note that we still must end the function prototype(s)
with a semi-colon.
Then we put a closing bracket and semicolon.
3. The different access restrictions
Why would you want to declare something private instead of public?
The idea is that some parts of the class are intended to be internal to the
class only; for the purpose of implementing features.
Some parts of the class are supposed to be available to anyone using
the class. These are the public class functions.
Some parts of the class are not easily accessible, but they are no less important.
These would correspond to the protected parts of the class.
4. The constructor and the destructor
Classes must always contain two functions: a constructor and a destructor.
The syntax for them is simple: the class name denotes a constructor, a ~ before the class
name is a destructor.
The basic idea is to have the constructor initialize variables, and to have the
destructor clean up after the class, which includes freeing any memory allocated.
If we don't need to actually perform any initialization, then we can allow the compiler to
create a "default constructor" for us. Similarly, if we don't need to do anything special in
the destructor, the compiler can write it for us too!
When the programmer declares an instance of the class, the constructor will be
automatically called.
The only time the destructor is called is when the instance of
the class is no longer needed, when the program ends, the class reaches the end
of scope, or when its memory is deallocated .
Let's recap and say that destructors are always called when the class is no longer usable.
Note that neither constructors nor destructors return arguments. This means we do not want
to, and cannot, return a value in them.
Note that constructor and destructor are made public,
so that our class can be created!
The constructor is called when an object is created,
but if the constructor is private, it cannot be called so the object cannot be constructed.
5. Defining a function
The syntax for defining a function that is a member of a class outside of the actual
class definition is to put the return type, then put the class name, two colons, and then
the function name. This tells the compiler that the function is a member of that class.
For example: The function writecolor() in the class Orange:
int Orange::writecolor()
{
// The two colons tell the compiler that the
// function is part of the class
return written_color;
}
6. Example
#include
using namespace std;
class ORANGE // Standard way of defining the class
{
public:
// This means that all of the functions below this, and any variables
// are accessible to the rest of the program.
// NOTE the colon after public.
ORANGE (); // Constructor
~ORANGE (); // Destructor
void setsize(int p );
int readsize();
protected:
// This means that all the variables under this, until a new type of
// restriction is placed, will only be accessible to other functions in the
// class. NOTE the colon after protected.
float read_weight;
};
// Do Not forget the semi-colon
// ------------------
ORANGE::ORANGE ()
{
//Constructors can accept arguments, but this one does not
read_weight = 0;
}
ORANGE ::~ORANGE ()
{
//Destructors do not accept arguments
}
void ORANGE :: setsize(int p );
{
// To define a function outside put the name of the class
// after the return type and then two colons, and then the name
// of the function.
read_weight = p;
}
int ORANGE ::readsize()
{
// The two colons simply tell the compiler that the function is part
// of the class
return read_weight;
}
int main()
{
ORANGE orange1;
// To create an 'instance' of the class, simply treat it like you would
// a structure. An instance is simply when you create an actual object
// from the class.
orange1.setsize ( 100 );
// To call functions in the class, you put the name of the instance,
// a period, and then the function name.
cout<< orange1.readsize();
}
Using CodeBlocs:
|
  |