data types in data types in c++

20
Data types In C++

Upload: nitin-menon

Post on 23-Oct-2015

71 views

Category:

Documents


5 download

DESCRIPTION

DATA TYPES IN C++

TRANSCRIPT

Page 1: DATA TYPES IN DATA TYPES IN C++

Data types

In

C++

Page 2: DATA TYPES IN DATA TYPES IN C++

WHAT IS 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 language’s that could support object-oriented programming feature and still retain the power and elegance of c.The resut was c++.

2

Page 3: DATA TYPES IN DATA TYPES IN C++

C++ datatypes are divided into three types

1. User-define datatype.

2. Build-in datatype.

3. Derived datatype.

3

Page 4: DATA TYPES IN DATA TYPES IN C++

4

Page 5: DATA TYPES IN DATA TYPES IN C++

User-define datatype

Structure and class:

we have studied some user-define 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.

5

Page 6: DATA TYPES IN DATA TYPES IN C++

6

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.

Page 7: DATA TYPES IN DATA TYPES IN C++

7

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, ...};enum typeName{value1, value2, ...};

Page 8: DATA TYPES IN DATA TYPES IN C++

8

Declaration of Enumerated Types

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

The identifiers between { } are called enumerators

The order of the declaration is significantred < orange < yellow … red < orange < yellow …

Page 9: DATA TYPES IN DATA TYPES IN C++

9

Declaration of Enumerated Types

Why are the following illegal declarations?enum grades{'A', 'B', 'C', 'D', 'F'}; enum grades{'A', 'B', 'C', 'D', 'F'};

enum places{1st, 2nd, 3rd, 4th};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 grades{A, B, C, D, F};enum places{first, second, third, fourth};enum places{first, second, third, fourth};

Page 10: DATA TYPES IN DATA TYPES IN C++

10

Declaration of Enumerated Types

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

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

Then we declare: daysOfWeek Today, payDay, dayOff;daysOfWeek Today, payDay, dayOff;

Page 11: DATA TYPES IN DATA TYPES IN C++

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.

11

Page 12: DATA TYPES IN DATA TYPES IN C++

SIZE AND RANGE OF C++ BASIC DATA TYPE

12

Page 13: DATA TYPES IN DATA TYPES IN C++

Derived data types

ARRAYS

FUNTIONS

POINTER

13

Page 14: DATA TYPES IN DATA TYPES IN C++

ARRAYS 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);

}

14

Page 15: DATA TYPES IN DATA TYPES IN C++

FUNCTIONS

DEFINITION:A function is a self-

contained block of statementsthat perform a coherent task of some kind.

15

Page 16: DATA TYPES IN DATA TYPES IN C++

Function Prototyping16

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:

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

Page 17: DATA TYPES IN DATA TYPES IN C++

THE MAIN FUCTION17

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.

Page 18: DATA TYPES IN DATA TYPES IN C++

18

In C++, the main() returns a value of type int tothe operating system. C++, therefore, explicitly definesmain() 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;}

Page 19: DATA TYPES IN DATA TYPES IN C++

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.

19

Page 20: DATA TYPES IN DATA TYPES IN C++

THANK YOU

20