| INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS |
There are two ways to make a class, one that starts with the keyword
struct and one that starts with the keyword class itself.
The following examples of employee class describe the similarities and
differences between when the keyword class or the keyword struct
is used. Note that the only difference between struct and class in C++
is the default accessibility of the members. By default members in
struct are public, whereas members in class are private.
However, you may argue that to define a class it would be easier to
simply use the keyword class instead of using struct as the alternative,
thus eliminating much confusion. C++ has demonstrated its loyalty to C
language by extending the existing struct. A C++ programmer
prefers class versus struct since the default accessibility of the
members of a class is private.
Figure 8.1a A class with keyword struct. Figure 8.1b A class equivalent to
Members are public by default. struct in figure 8.1a
![Text Box: class employee {
char name[20];
int age;
double salary;
void setdata(char *, int, double);
void displaydata(char*, int, double);
};](http://www.geocities.com/lonairl1/chap8_files/image001.gif)
![Text Box: struct employee {
private: char name[20];
int age;
double salary;
void setdata(char *, int, double);
void displaydata(char*, int, double);
};](http://www.geocities.com/lonairl1/chap8_files/image002.gif)
Figure 8.2a A struct. Members are declared Figure 8.2b A class equivalent to
private with the key word private. struct in figure 8.2
GENERAL SYNTAX OF CLASS AND ITS OBJECT
The following illustrates the general syntax for a class declaration and how to create an object from the class.
![]()





![]()
For every object that is created from class it will have its own data member values and its own functions. In the following example, data member x and y of object center are different from data members x and y of object corner.


![]()
![]()
One idea behind the class is the access ability of its members (data and function members). A class may want to hide its information detail (implementation detail) from others by allowing only certain members (interface) to access it. There are three keywords that define the accessibility of a class: public, private and protected.
![Text Box: class employee {
private: char name[20];
int age;
double salary;
public: void setdata(char *, int, double);
void displaydata(char*, int, double);
};](http://www.geocities.com/lonairl1/chap8_files/image014.gif)
![]()
In the above class example since default accessibility is private there would be no need to place the keyword private before the data members. This is shown in below example.
![Text Box: class employee {
char name[20];
int age;
double salary;
public: void setdata(char *, int, double);
void displaydata(char*, int, double);
};](http://www.geocities.com/lonairl1/chap8_files/image016.gif)
![]()
WHAT SHOULD BE PRIVATE AND WHAT SHOULD BE PUBLIC?
In a class the data is usually set to private and the functions are set to public. This way data cannot be accessed directly from outside of class preventing unintentional change of data. However, from the outside class (e.g. main program) data can be accessed or changed by the member functions that are set to public.

![]()
Note that by having x and y as private, the main program cannot access the x and y directly therefore access has to be through a member function.
The functions of a class are known as interface, meaning user of the class (outsider) through these functions can access the class. Therefore these functions are mainly set to public. However there are situations when there is no need for an outsider to access a class function and the task of the function is to provide an inside job, therefore it is set to private.

![]()
In C language, the structure bundles only data, however, in C++ structure (class) bundles data and functions, which is the major difference between C structure and C++ structure (class). Interestingly, the original name of C++ was C with classes. One can see that a prime reason for creation of the C++ was the frustration of simulating classes in C by using structure and the use of pointer to the functions instead of inclusion of functions.
CLASS AS A USER DEFINED TYPE (DATA ABSTRACTION)
C/C++ has its own type such as built-in data type (int, float, char, boolean). However, a user can make its own type as needed (class) and make the object (variable) from it. The class is the blueprint and object is the real thing that you work with. In other words, an object is the instance of a class. For example, an analogous example for class would be a built-in type such as int x; Where int is a class and x is an object of the class int. In the following example, point is the class and center is the object of the class:

