C++ has several built-in and user-defined data types. User-defined types include structures, unions, classes, and enumerations. Structures and classes allow grouping of different data types whereas unions allocate memory for the largest data member. Enumerations allow defining a set of named integer constants. Arrays, functions, and pointers are derived data types in C++. Arrays allow grouping of similar data types, functions allow breaking down programs into reusable code segments, and pointers store addresses of variables and dynamically allocate memory.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views10 pages
Data Type
C++ has several built-in and user-defined data types. User-defined types include structures, unions, classes, and enumerations. Structures and classes allow grouping of different data types whereas unions allocate memory for the largest data member. Enumerations allow defining a set of named integer constants. Arrays, functions, and pointers are derived data types in C++. Arrays allow grouping of similar data types, functions allow breaking down programs into reusable code segments, and pointers store addresses of variables and dynamically allocate memory.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
C++ Data Type
User-define type Derived type
Structure Built-in type Array
Union Function Class Pointer enumeration reference
Integral type Void Floating type
int char float double
User Define DataTypes Structures and Classes struct & union are user define data types in c which is suitable for oop’s. Another user define data type is class in c++. Variable of class is called object. Enumerated Data Type Providing a way for attaching names to number thereby increasing comprehensibility of code. enum is keyword for enumerated data type. Syntax of enum statement is similar to struct statement. Eg……… enum shape struct shape { circle, { int c; square, int s; triangle float t; }; }; enum colour{red, blue, green, yellow}; enum position{off, on} Why Enumerated data type is used? By using tag name we can declare new variable. E.g.…… shape ellipse; // ellipse is of type shape colour background // background is of type colour C++ dose not permit an int value to be automatically converted to an enum value. E.g.…….. colour background = blue; // allowed colour background = 7; // Error in c++ colour background = (colour) 7; // ok Enumerated value can be used in place of an int value. int c = red; // valid, colour type promoted to int By default the enumerators assigned integer values i; e 0 for first enumerator. E.g…….. enum colour{red, blue=4, green=8}; enum colour{red=5, blue, green}; Note: C++ permits creation of enum without tag name. E.g…….. enum{off,on}; E.g…… enum shape {circle, rectangle, triangle}; int main() { cout<< “Enter shape code:”; int code; cin>> code; while(code>=circle && code <= triangle) { switch(code) { case circle: ……… ……… break; case rectangle: ……… ……… break; case triangle: ……… ……… break; } cout<< “Enter shape code:”; cin>> code; } cout<< “BYE \n”; return 0; } Note: C++, an enum define within a class is local to that class. Derived DataTypes In C++, derived data type inclued…… Arrays: In c++, size should be one larger than the number of characters in the string. E.g….. char string[4] = “xyz”; // for c++ Functions: In c++ main function return value of type int to the os. The main function in C++ is defined as follows: int main() { …….. …..... return 0; } Note: If function return zero value it means that program run successfully. Pointers : C++ adds the concept of constant pointer and pointer to a constant. Declaration: char * const p1 = “good”; // constant pointer int const * p2 = &m; // pointer to a constant Note: Pointers are used in C++ for memory management and achieving polymorphism.