A class declaration defines a new type that links code and data. The general form for a class declaration is
class name {
private data and functions
access specifier:
data and functions
...
access specifier:
data and functions
} object-list;
|
Where the name is the name by which it will be possible to create an object from class, access specifier is one of three keywords: public, private, or protected, and the object-list is optional list of objects.
By default, data and functions declared within a class are private and may be accessed only by other members of the class. By using public access specifier, everyone can access data and functions. Specifying that a data member or member function is protected means that it can only be accessed from within the class or a subclass.
class A {
public:
int i; // public to all users of class A
protected:
int j; // can only be used by methods in class A or its derived classes
private:
int k; // can only be used by methods in class A
}
|
A class constructor (if there is one) is called after creating a new instance of the class. Constructors are often used to set up initial values for data in the new object and to allocate space for sub-objects (e.g. for an array contained in the new object). A class can have multiple constructors, taking different type of arguments.
class myclass {
int a, b;
public:
myclass() { a = 0; b = 0; }
myclass(int i, int j) { a = i; b = j; }
void show() { cout << a << " " << b; }
};
int main()
{
myclass ob1, ob2(1,2);
ob1.show();
ob2.show();
return 0;
}
|
When delete is called on an object, the destructor function of the class (if one is defined) before destroying the object. The destructor can deallocate storage within the object (e.g. an array contained in one of its fields) or perform other clean-up operations. A class cannot have more than one destructor.
class myclass {
int who;
public:
myclass(int i) { cout << "Initializing " << i << endl; who = i; }
~myclass() { cout << "Destructing " << who << endl; }
} glob_ob(1);
int main()
{
myclass local_ob(2);
return 0;
}
|
C++ has elevated the role of the standard C structure. The only difference between a class and a struct is that by default all members are public in a structure and private in a class.
This seeming redundancy is justified for several reasons. First, in C, structures already provide a means of grouping data. Therefore it was a small step to allow them to include member functions. Second, because structures and classes are related, it may be easier to transport existing C programs to C++. Finally, providing two different keywords allows the definition of a class to be free to evolve.
For the sake of clarity, struct should be used when C-like structure is wanted and class when a class is wanted.
struct mystr {
void setStr(char *s);
void showStr();
private:
char str[255];
};
class mystr {
char str[255];
public:
void setStr(char *s);
void showStr();
};
|
Like a structure, an union declaration in C++ defines a special type of class. Unions may contain both member functions and variables. They may also include constructor and destructor functions. An union retains all of its C-like features. the most important being that all data elements share the same location in memory.
There are several restrictions when unions are used: An union cannot inherit any other classes of any type. An union cannot be a base class. An union cannot have virtual member functions. No static variables can be members of an union. An union cannot have as a member any object that overloads the = operator. No object can be a member of an union if the object has a constructor or destructor function.
union mytypes_t {
char c;
int i;
float f;
} mytypes;
|
Friend declarations give functions from outside a class access to class's private data and private functions, without making them public.
class myclass {
int a, b;
public:
friend int sum(myclass x);
void set_ab(int i, int j);
}
void myclass::set_ab(int i, int j);
{
a = i;
b = j;
}
int sum(myclass x) // not a member function of any class
{
return x.a + x.b;
}
|
Both function and data members of a class can be made static. There are restrictions placed on static member functions. First, they may only access other static members of the class. Second, static member functions do not have a this pointer. When a member variable declaration precedes with static, only one copy of that variable will exist and all objects of the class will share that variable.
class static_type {
static int i;
public:
static void init(int x) { i = x; }
void show() { cout << i; }
};
int main()
{
static_type::init(10); // this is perfectly valid!
static_type x;
x.show();
return 0;
}
|