Basic of c &c++

1,457 views
1,373 views

Published on

a

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,457
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Basic of c &c++

  1. 1. C++ Data Types <ul><li>Built-In Type </li></ul><ul><li>Integral type - int, char </li></ul><ul><li>Floating type – float, double </li></ul><ul><li>Void </li></ul><ul><ul><li>Used to specify the return type of a function when it is not returning any value </li></ul></ul><ul><ul><li>To indicate an empty argument list to a function. Example : void fun (void); </li></ul></ul>
  2. 2. C++ Data Types <ul><ul><li>Used in declaration of generic pointers. </li></ul></ul><ul><ul><ul><li>Void *gp; </li></ul></ul></ul><ul><ul><ul><li>A generic pointer can be assigned a pointer value of any basic data type, but it may not be dereferenced. </li></ul></ul></ul><ul><ul><ul><li>For ex: int * ip; gp = ip; // assigns int pointer to void pointer. </li></ul></ul></ul><ul><ul><ul><li>But, *ip = *gp; is illegal; </li></ul></ul></ul><ul><ul><ul><ul><li>It would not make sense to dereference a pointer to a void value. </li></ul></ul></ul></ul>LOOPS
  3. 3. C++ Data Types <ul><li>User-defined type </li></ul><ul><li>Structure </li></ul><ul><li>Union </li></ul><ul><li>Class </li></ul><ul><li>Enumeration </li></ul>
  4. 4. C++ Data Types <ul><li>Structure and Union in C. </li></ul><ul><li>They are used in C++ with some added features of object oriented languages. </li></ul><ul><li>Class is used in c++. </li></ul><ul><li>The class variables are known as objects. </li></ul>
  5. 5. Enumerated Data type <ul><li>It is user defined data type which provides a way for attaching names to numbers. </li></ul><ul><li>It increases the comprehensibility of the code. </li></ul><ul><li>The enum keyword automatically enumerates a list of words by assigning them values 0,1,2 and so on. </li></ul><ul><li>It is used to create symbolic constants. </li></ul>
  6. 6. Enumerated Data type <ul><li>The enum statement is similar to the struct statement. </li></ul><ul><li>Ex : enum shape {circle, square, triangle} </li></ul><ul><li>enum color{ red, blue, green, yellow} </li></ul><ul><li>They differ from enum in C . </li></ul><ul><li>In C++, they can be used as new type names. </li></ul><ul><li>By using, tag names we can declare new variables. </li></ul><ul><li>shape ellipse; // ellipse is of type shape. </li></ul>
  7. 7. Enumerated Data type <ul><li>ANSI C defines the types of enums to be ints. </li></ul><ul><li>In c++, each enumerated data type retain its own separate type. </li></ul><ul><li>C++ does not permit an int value to be automatically converted to an enum value. </li></ul><ul><li>Ex : color background = blue; // allowed </li></ul><ul><li>color background = 7; // error in c++ </li></ul><ul><li>color background = (color) 7; // ok </li></ul>
  8. 8. Enumerated Data type <ul><li>An enumerated value can be used in place of an int value. </li></ul><ul><li>Ex : int c = red; // valid, color type promoted to int </li></ul><ul><li>By default, enumerators are assigned integer values starting from 0. We can override these values by : </li></ul><ul><li>enum color {red= 8, blue = 4, green}; </li></ul>
  9. 9. Enumerated Data type <ul><li>C++ also permits the creation of anonymous enums ( enums with out tag names) </li></ul><ul><li>Ex : enum {off, on}; </li></ul><ul><li>int switch_1 = off; </li></ul>
  10. 10. Derived data types <ul><li>Array : contiguous/ continuous memory space. </li></ul><ul><li>Character arrays are initialized in different way: </li></ul><ul><li>Ex in C :char string[3] = “xyz” // programmer intends to leave out the null character 0. </li></ul><ul><li>Ex in C++ : char string[4] = “XYZ” </li></ul>
  11. 11. Derived data types <ul><li>Function: A block of code that is to be executed to serve some purpose. </li></ul><ul><li>Pointers: </li></ul><ul><li>int * ip; // int pointer </li></ul><ul><li>ip = &x; // address of x assigned to p; </li></ul><ul><li>*ip = 10; // 10 assigned to x through indirection. </li></ul>
  12. 12. Difference Between The Two? <ul><li>Constant pointer </li></ul><ul><li>Pointer to a constant </li></ul>
  13. 13. Constants <ul><li>Constants refer to fixed values that do not change during the execution of a program. </li></ul><ul><li>They include integers , characters , floating point numbers and strings . </li></ul><ul><li>Literal constant do not have memory locations. </li></ul><ul><li>Examples: </li></ul><ul><li>123 // decimal integer </li></ul><ul><li>12.34 // floating point integer </li></ul><ul><li>037 // Octal integer </li></ul>
  14. 14. Constants <ul><li>0x2 //hexadecimal integer </li></ul><ul><li>“ C++” //string constant </li></ul><ul><li>‘ A’ // character constant </li></ul><ul><li>L’ab’ // wide-character constant </li></ul><ul><li>wchar-_t type is a wide-character literal introduced by ANSI C++ and is used for character sets that cannot fit a character into a single byte. </li></ul><ul><li>Wide-character literals begin with the letter L. </li></ul>
  15. 15. Symbolic constants <ul><li>They can be created as follows: </li></ul><ul><li>Using the qualifier const, and </li></ul><ul><li>Defining a set of integer constants using enum keyword. </li></ul><ul><li>Any value declared as const cannot be modified by the program in any way. </li></ul><ul><li>ex : Const int size = 10; </li></ul><ul><li>C++ requires a constant to be initialized. </li></ul><ul><li>ANSI C does not require an initialization, it will give a default value 0. </li></ul>
  16. 16. Symbolic constants <ul><li>Enumeration example : </li></ul><ul><li>enum { x, y, z}; </li></ul><ul><li>const x = 0; </li></ul><ul><li>const y = 1; </li></ul><ul><li>const z =2; </li></ul><ul><li>Or explicitly, enum {x=100, y = 50,z=200}; </li></ul>
  17. 17. Control Structures <ul><li>Three control structures: </li></ul><ul><li>Sequence structure ( straight line) </li></ul><ul><li>Selection structure (branching) </li></ul><ul><li>Loop structure ( iteration or repetition) </li></ul>
  18. 18. Selection structure (branching) <ul><li>Simple If statement </li></ul><ul><li>if (condition) </li></ul><ul><li>{ // code </li></ul><ul><li>} </li></ul><ul><li>If……else statement </li></ul><ul><li>If (condition) </li></ul><ul><li>{ </li></ul><ul><li>// code </li></ul><ul><li>} </li></ul><ul><li>Else </li></ul><ul><li>{// code} </li></ul>
  19. 19. Switch statement <ul><li>This is multiple branching statement, based on a condition. </li></ul><ul><li>The control is transferred to one of the many possible points. </li></ul><ul><li>Syntax: </li></ul><ul><li>switch (expression) </li></ul><ul><li>{ </li></ul><ul><li>case 1: </li></ul><ul><li>{ //code </li></ul><ul><li>} </li></ul>
  20. 20. Switch statement <ul><li>case 2: </li></ul><ul><li>{ //code </li></ul><ul><li>} </li></ul><ul><li>Default: </li></ul><ul><li>{// code </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  21. 21. LOOPS <ul><li>Do While :exit controlled loop </li></ul><ul><li>Do </li></ul><ul><li>{ </li></ul><ul><li>// code </li></ul><ul><li>}while (condition is true); </li></ul>
  22. 22. LOOPS <ul><li>While Loop : entry controlled loop </li></ul><ul><li>while (condition is true) </li></ul><ul><li>{ // code </li></ul><ul><li>} </li></ul><ul><li>For Loop : entry controlled and action will repeated a predetermined no. of times </li></ul><ul><li>for( initial value; test; increment) </li></ul><ul><li>{ // code </li></ul><ul><li>} </li></ul>

×