C++ Type Specifiers
Specifiers in C++ - Part 6
Forward: In this part of the series, I explain, C++ Type Specifiers.
By: Chrysanthus Date Published: 25 Aug 2012
Introduction
Type Specifiers
Type specifiers are, simple type specifiers, enum specifier, class specifiers, cv qualifiers, elaborated type specifiers. I have explained simple type specifier, and cv qualifiers. In this part of the series I explain, enum and class specifiers. I will not explain elaborated type specifiers in this series. In this series, I have explained function specifiers; however, function specifiers are not type specifiers, because they do not deal with object types.
The enum Specifier
The enum type is used to define the enum object. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
enum numbers {today, tomorrow, afterTomorow, theDayAfter};
cout << today; cout << "\n";
cout << tomorrow; cout << "\n";
cout << afterTomorow; cout << "\n";
cout << theDayAfter; cout << "\n";
return 0;
}
Read and try the code. Note the position of the enum specifier. Each item in the enumeration block, is called an enumerator. You should be familiar with the enum object already, so I will not say any more about it.
The class specifiers are, class, struct and union.
The class specifier is used to define a class. The following example illustrates this:
#include <iostream>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
Calculator(int ident1, int ident2)
{
num1 = ident1;
num2 = ident2;
}
int add ()
{
int sum = num1 + num2;
return sum;
}
};
int main()
{
Calculator myObject(6,7);
int result = myObject.add();
cout << result;
return 0;
}
Note the use and position of the specifier, class. You should be familiar with the class, already, so I will not say any more on that.
The struct and union are different types of classes. The following example illustrates the use of the struct specifier:
#include <iostream>
using namespace std;
int main()
{
struct {const char *name; int age; float salary; const char *HQ;} Emp1, Emp2, Emp3, Emp4;
Emp1.name = "Mary Jones";
Emp1.age = 35;
Emp1.salary = 3856.42;
Emp1.HQ = "Phd";
Emp2.name = "John Carlson";
Emp2.age = 36;
Emp2.salary = 2124.32;
Emp2.HQ = "Msc";
cout << Emp2.age;
return 0;
}
Note the position and use of the specifier, struct. The values of the elements of the struct objects, Emp3 and Emp4 were not considered. You should be familiar with the struct, already, so I will not say any more on that.
The union is another kind of class, but I will not go into that.
That is it, for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT