Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.

Similar presentations


Presentation on theme: "Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user."— Presentation transcript:

1 Programming 2

2 Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user defined data type which allows you to combine data items of different kinds. Structures are used to represent a record.

3

4

5 Accessing Elements To access any member of a structure, we use themember access operator (.) intmain( ) { structBooks Book1; // Declare Book1 of type Book // book 1 specification strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "ChandMiyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; return 0; }

6 Classes & Objects Define classes

7 Define Objects

8 Example Int main( ) { Box Box1; // Declare Box1 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout<< "Volume of Box1 : " << volume <<endl; return 0; }

9 Public and Private Members public: The member (data or function) is accessible and available to all in the system. private: The member (data or function) is accessible and available within this class only.

10 Class Class: data member and functions. Object: instantiation of a class. type variable Int num; Student s1;

11 How to create and use a class!! First think about: a. data member b. functions Write your class code. Instantiate object of the class

12 Define a class Class is defined using: keyword class or keyword struct. Then class_name which must be a valid identifier for the class. The body of the class can contain members(data or function).

13 Accessing the members By inserting dot (.) between object_name and membername. Ex: Box1.length

14 Example Declared within the class Declared outside the class


Download ppt "Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user."

Similar presentations


Ads by Google