Anda di halaman 1dari 20

Data types In C++

C++ is an object-oriented programming language. It was developed by bjarne stroustrup at AT & T bell lab in murray hill,new jersey,usa. Stroustrup an admirer of c wanted to combine the best of both the languages and create a more powerful languages that could support object-oriented programming feature and still retain the power and elegance of c.The resut was c++.

C++ datatypes are divided into three types

1. User-define datatype. 2. Build-in datatype.

3. Derived datatype.

User-define datatype
Structure and class:
we have studied some userdefine data types like struct & union in c. it is legal in c++ but some more features are added into it to make suitable for oop.class specification has two parts: [i]class declaration. [ii]class function defination. Class declaration describe the type & scope of member.function defination describe how the is class function is implemented.

Enumeration Data Types

A data type is
A set of values together with A set of operations on those values.

In order to define a new simple data type, called enumeration type, we need: A name for the data type. A set of values for the data type. A set of operations on the values.

Enumeration Data Types

C++ allows the user to define a new simple data type by specifying:
Its name and the values

But not the operations.

The values that we specify for the data type must be legal identifiers The syntax for declaring an enumeration type is:
enum typeName{value1, value2, ...};

Declaration of Enumerated Types

Consider the colors of the rainbow as an enumerated type:
enum rainbowColors = { red, orange, yellow, green, blue, indigo, violate }

The identifiers between { } are called enumerators The order of the declaration is significant red < orange < yellow

Declaration of Enumerated Types

Why are the following illegal declarations?
enum grades{'A', 'B', 'C', 'D', 'F'}; enum places{1st, 2nd, 3rd, 4th};
They do not have legal identifiers in the list

What could you do to make them legal?

enum grades{A, B, C, D, F}; enum places{first, second, third, fourth};


Declaration of Enumerated Types

As with the declaration of any object
Specify the type name Followed by the objects of that type

enum daysOfWeek { Sun, Mon, Tue,Wed, Thu, Fri, Sat }

Then we declare:
daysOfWeek Today, payDay, dayOff;


Build-in datatype
Void:There are basically three
advantages of keyword void: [i] To specify return type of function when it is not returning any value. [ii] To indicate an empty argument list to a function. Example: void display (void). [iii] In the declaration of generic pointer. Void *gp; gp becomes a generic pointer.




Derived data types


DEFINITION: The C language provides a capability that enables the user to design a set of similar data types called ARRAYS. Eg. # include<stdio.h> void main() { int x; x=5; X=10; Printf(\nx=%d,x); }




A function is a self-contained block of statements that perform a coherent task of some kind.


Function Prototyping
Function prototyping is one of the major improvement added to c++ functions. The prototype describe the function interface to the compiler by giving details like the numbers & the type of arguments & the type of return values.
The function prototype is a declearation statement in calling programm a&is of the following form:

1. float volume( int x, float y, float z); 2. float volume( int x, float y, z); 3. float volume( int , float , float);



C does not specify any return type for the main() function which is the starting point for the execution of a program. Eg. main() { // main program statements } this is perfectly valid because the main() in C does not return any value.


In C++, the main() returns a value of type int to the operating system. C++, therefore, explicitly defines main() as matching one of the following prototypes. int main(); int main(int argc, char * argc[]); The function that have a return value should use the return statement for termination. The main() function in C++ is therefore, defined as follows: int main() { :::::::::::::::: return 0; }

The main difference between C and C++

The main difference between C and C++ is that C++ is object oriented while C is function or procedure oriented. Object oriented programming paradigm is focused on writing programs that are more readable and maintainable. It also helps the reuse of code by packaging a group of similar objects or using the concept of component programming model. It helps thinking in a logical way by using the concept of real world concepts of objects, inheritance and polymorphism. It should be noted that there are also some drawbacks of such features. For example using polymorphism in a program can slow down the performance of that program.