Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.

Similar presentations


Presentation on theme: "Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to."— Presentation transcript:

1

2 Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to its parts as members of that variable by using the dot (.) operator. The power of structures lies in the fact that once defined, the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, double, char.

3 Structure -Example When dealing with the students in a school, many variables of different types are needed. E.g. name, age, id, and marks point. So you may define the student structure as: struct STUDENT { int id, age; char name[80]; float marks; }; STUDENT is called the structure tag, and is your brand new data type, like int, double or char. id, name, age, and marks are structure members.

4 Declaring Variables of Type struct The most efficient method of dealing with structure variables is to define the structure globally. This tells "the whole world", namely main and any functions in the program, that a new data type exists. To declare a structure globally, place it BEFORE void main(). The structure variables can then be defined locally in main as follows:

5 Structure Example- Extended struct STUDENT { int id, age; char name[80]; float marks; } ; void main() { STUDENT s1, s3; // declare two variables of the new type cin>>s1.id>>s1.age>>s1.name>>s1.marks; //accessing of data members cout<<s1.id<<s1.age<<s1.name<<s1.marks; STUDENT s2 = {100,17,”Aniket”,92}; //initialization of structure variable cout<<s2.id<<s2.age<<s2.name<<s2.marks; s3=s2; //structure variable in assignment statement cout<<s3.id<<s3.age<<s3.name<<s3.marks; }

6 Alternate method of declaring variables of type struct: struct STUDENT { int id, age; char name[80]; float marks; } s1, s3; Accessing data members To access data members we use the following format: structure variable.member name e.g. cin>>s1.id>>s1.age>>s1.name>>s1.marks;

7 Initialization of structure variable e.g. STUDENT s2 = {100,17,”Aniket”,92}; Structure variable in assignment statement s3=s2; This assigns the value of each member of s2 to the corresponding member of s3. Note that one structure variable can be assigned to another only when they are of the same structure type, otherwise complier will give an error.

8 OOP CONCEPTS Paradigm-: It means organizing principle of a program. It is an approach to programming. Procedural Paradigm: In PROCEDURAL PROGRAMMING PARADIGM, the emphasis is on doing things i.e., the procedure or the algorithm. The data takes the back seat in procedural programming paradigm. Also, this paradigm does not model real world well. Object Oriented programming: The OBJECT ORIENTED PROGRAMMING PARADIGM models the real world well. It overcomes the shortcomings of procedural paradigm. It views a problem in terms of objects and thus emphasizes on both procedures as well as data.

9 Basic concepts in object-oriented Programming Object-: An object is an identifiable entity with some characteristics and behavior. Class-: A class represents a group of objects that share common properties, behavior and relationships. Data Abstraction-: Abstraction refers to act of representing essential features without including the background details or explanations. Encapsulation-: The wrapping up of data and associated functions into a single unit is known as encapsulation. Encapsulation implements data abstraction.

10 OPP conceps Inheritance-: It is the capability of one class of things to inherit capabilities or properties from another class. Polymorphism-: It is the ability for a message or data to be processed in more than one form. Polymorphism is a property by which the same message can be sent to objects of several different classes. Polymorphism is implemented in C++ through virtual functions and overloading- function overloading and operator overloading.

11 Advantages of Object oriented programming Software complexity can be easily managed Object-oriented systems can be easily upgraded It is quite easy to partition the work in a project based on objects class enforce data-hiding, abstraction & encapsulation A class groups its members into three sections : private, protected, and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class enforces data-hiding. The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. Abstraction means representation of essential features without including the background details and explanation.

12 CLASSES & OBJECTS A CLASS is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An OBJECT is an instantiation of a class. Classes enable a C++ program to model objects that have: attributes (represented by data members) and, behaviors or operations (represented by member functions). In terms of variables, a class would be the type, and object would be the variable. Classes are generally declared using the keyword class, with the following format:

13 class class_name { access_specifier_1: member1; access_specifier_2: member2;... } object_names; Where: class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access levels that the members following them acquire:

14 private members of a class are accessible only from within other members of the same class or from their FRIENDS. Private members of the class are normally not accessible outside the class, i.e., the information is hidden from "clients" outside the class. protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. Finally, public members are accessible from anywhere the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access.

15 A member function prototype which has the very same name as the name of the class may be specified and is called the constructor function. The definition of each member function is "tied" back to the class by using the binary scope resolution operator ( :: ). Example of operators used to access class members is the dot operator (.). Class Example: class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect;

16 The above code declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition. After the declarations of CRectangle and rect, we can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member. This is similar to what we did with plain data structures before. For example: rect.set_values (3,4); myarea = rect.area(); The only members of rect that we cannot access from the body of our program outside the class are x and y, since they have private access and they can only be referred from within other members of that same class.

17 Complete example of class // classes example #include using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; }

18 The scope operator (::, two colons) included in the definition of set_values() is used to define a member of a class from outside the class definition itself. The definition of the member function area() has been included directly within the definition of the CRectangle class given its extreme simplicity, whereas set_values() has only its prototype declared within the class, but its definition is outside it. In this outside declaration, we must use the scope operator (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.

19 The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, in the function set_values() of the previous code, we have been able to use the variables x and y, which are private members of class CRectangle, which means they are only accessible from other members of their class. The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that: in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.

20 Members x and y have private access (remember that if nothing else is said, all members of a class defined with keyword class have private access). One of the greater advantages of a class is that, as any other type, we can declare several objects of it. E.g. from previous class CRectangle, we could have declared the object rectb in addition to the object rect:

21 // example: one class, two objects #include using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }

22 In this concrete case, the class (type of the objects) we are talking about is CRectangle, of which there are two instances or objects: rect and rectb. Each one of them has its own member variables and member functions. Notice that the call to rect.area() does not give the same result as the call to rectb.area(). This is because each object of class CRectangle has its own variables x and y, as they, in some way, have also their own function members set_value() and area() that each uses its object's own variables to operate.

23 That is the basic concept of object-oriented programming: Data and functions are both members of the object. We no longer use sets of global variables that we pass from one function to another as parameters, but instead we handle objects that have their own data and functions embedded as members. Notice that we have not had to give any parameters in any of the calls to rect.area or rectb.area. Those member functions directly used the data members of their respective objects rect and rectb.

24 THANK YOU And Best of luck in your Revision and Exams BYe


Download ppt "Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to."

Similar presentations


Ads by Google