C++ user defined data

C++ User Defined data types

basic data types in c++

Structure and classes

we also used in C language user defined data type such as struct and union in c. while these data types are also legal in c++, some more features have been added to make them suitable for object oriented programming. C++ language also permits us to define another user defined data type known as class which can be used, just like any other basic data type, to declare the variables. the class variables are known as objects, which are the main focus of object-oriented programming. More about these data types is discussed

Enumerated Data type

An enumerated data type is another user defined type which provides a way for attaching the name to the numbers, thereby increasing comprehensibility of the code. The enum keyword (also use in c) automatically enumerates a list of words by assigning them values 0,1,2 and so on. This facility provides an alternative means for creating symbolic constants. the syntax of an enum statement is similar to the struct statement take example

enum shape{circle,square,triangle};
enum colour{red,blue,green,yellow};
enum position{off,on};

talk about void some the type void was introduced in ANSI C. two normal uses of void are

The enumerated data types differ slightly in c++ when compared with those in ANSII C. In C++ language, the tag names like shape, colour, and position become new type names. By using these tag names, we can declare new variables

Examples:
shape circle; // circle is of type shape
colors background // background is of type colors

ANSI C defines the types of enumes to be ints. In c++ each enumerated data type retains its own seprate type. This means that c++ language does not permit(offer) an int value to be automatically converted to an enum value take the example

however en enumerated value can be used in place of an int value
int c=red; // valid, colour type assign to int
so the by default the enumerators are assigned the integer values starting with 0(zero) for the first enumerators, 1 for the second, and 2 for the third and so on. we can over ride the default values by explicit;y assigning integer values to the enumerators. for example

colour backgrounds=blue //allowed
colour backgrounds=7; // error
colour backgrounds=(colour)/7; //ok

enum colours {red,blue=4,green=8};
enum colours {blue=4,red,green};

are valid definitions.in the first case red is 0(default value), in the second case, red is 5 and green is 6

note: that the subsequent initialized enumerators are larger by one than there predecessors

c++ also permits the creation of anonymous enums (enums without tag names)
example
enum{off,on};

here, off is 0 and on is 1. these constant may be referenced in the same manner as regular constant examples

int switch_1=off;
int switch_2=on;

so we can also define symbolic constants for a switch statement example

enum shape
{
circle,rectangle,triangle
};
int main(void)
{
int code;
std::cout<<"Enter the code of shape";
std::cin>>code;
while(code>=circle&&code<=triangle)
{
switch(code)
{
case circle:
break;
case rectangle:
break;
case triangle:
break;
}
std::cout<<"Enter shape code";
std::cin>>code;
}
std::cout<<"Bye \n";
return 0;
}

so An enum defined within a class(or structure) is local to that class (or structure) only.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise