1 / 169

What is OOPs

What is OOPs. Oop is a programming paradigm that represents the concept of objects that have data fields(attributes that describe the object)and associated procedures known as methods.

zlata
Télécharger la présentation

What is OOPs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What is OOPs Oop is a programming paradigm that represents the concept of objects that have data fields(attributes that describe the object)and associated procedures known as methods. Objects which are usually instance of classes are used to interact with one another to design applications and computer programs.

  2. What is a Object • Objects are basic run time entities in an object oriented system. • They may represent a person, a place, a bank account, or any item that the program has to handle. • When a program is executed, the objects interact by sending messages to one another. • For example if customer and account are two objects in a program, then the customer object may send a message to the account object requesting for bank balance. • Each object contains data and code to manipulate data

  3. What are Classes • A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and function • We just mentioned that objects contain data and code to manipulate that data. The entire set of data and code can be made user-defined type with help of class. • In fact objects are variable of type class. • Thus a class is collection of objects of similar type. • Classes are user defined data type but behave like inbuilt data type.

  4. Classes are generally declared using the keyword class, with the following format: • 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 declaration can contain members, that can be either data or function declarations, and optionally access specifiers. • All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:

  5. private members of a class are accessible only from within other members of the same class or from their friends. • 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 where 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. For example:

  6. Class Crectangle • { intx,y; Public: void set_values (int,int);int area (void);} rect; 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.

  7. Why we need OOPs • The proper question should be WHEN(not WHY), or WHY and WHY NOT. • t makes complex code easier to develop, more reliable, more maintainable, and generally better.Because OOP insists that you think about what you expose to the outside world, it lets you change the implementation of an object without affecting any other code. (Encapsulation)Because it allows you to have many different functions, all with the same name, all doing the same job, but on different data. (Polymorphism)Because it lets you write generic code: which will work with a range of data, so you don't have to write basic stuff over, and over again. (Generics)Because it lets you write a set of functions, then expand them in different direction without changing or copying them in any way. (Inheritance)

  8. What are variables • A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. • The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive: • There are following basic types of variable in C++ as explained in last chapter:

  9. Variable Definition in C++: • A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type, and contains a list of one or more variables of that type as follows: • type variable_list; Here, type must be a valid C++ data type including char, w_char, int, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here: • inti, j, k; char c, ch; float f, salary; double d; The line inti, j, k; both declares and defines the variables i, j and k; which instructs the compiler to create variables named i, j and k of type int.

  10. Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows: • type variable_name = value; Some examples are: • extern int d = 3, f = 5; // declaration of d and f. int d = 3, f = 5; // definition and initializing d and f. byte z = 22; // definition and initializes z. char x = 'x'; // the variable x has the value 'x'. For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

  11. Try the following example where a variable has been declared at the top, but it has been defined inside the main function: • #include <iostream> using namespace std; // Variable declaration: extern int a, b; extern int c; extern float f; int main () { // Variable definition: int a, b; int c; float f; // actual initialization a = 10; b = 20; c = a + b; cout << c << endl ; f = 70.0/3.0; cout << f << endl ; return 0; } When the above code is compiled and executed, it produces the following result: • 30 23.3333

  12. Basic concept of OOP • It is necessary to understand some of the concepts used extensively in object oriented programming. These Include: • Objects • Classes • Data Abstraction and Encapsulation • Inheritance • Polymorphism • Dynamic Binding • Message Passing

  13. Data Abstraction and Encapsulation • The wrapping up of data and function into a single unit(called class) is known as encapsulation. • Data Encapsulation is the most striking feature of a class. • The data is not accessible to outside world and only those function which are wrapped in the class can access it. • These functions provide the interface between the object’s data and the program. • This insulation of the data from direct access by program is called data hiding.

  14. Abstraction refers to providing only essential information to the outside world and hiding their background details. • Data Abstraction is a programming technique that relies on the separation of interface and implementation. • E.g. TV • Which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs and DVD. • But you do not know how it receives signal over the air or cables how it translates them and finally displays them on screen

  15. Inheritance • Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. • In oop the concept of inheritance provides the idea of reusability. • This means that we can add additional features to an existing class without modifying it. • This is possible by deriving a new class from the existing ones. The new class will have the combined features of both the classes . • Subclass defines only those features that are unique to it.

  16. Polymorphism • Polymorphism is another important OOP concept. • A greek term means the ability to take more than one form. • An operation may exhibit behaviors in different instances. • The behavior depend upon the type of data used in operation. • For example consider the operation addition if it is a number it will generate sum but if it is two string then it will generate third string • The process of making an operator to exhibit different behavior in different instances is known as operator overloading.

  17. Applications Of OOPs • Main application areas of OOP are • Ø  User interface design such as windows, menu ,… • Ø  Real Time Systems • Ø  Simulation and Modelling • Ø  Object oriented databases • Ø  AI and Expert System • Ø  Neural Networks and parallel programming • Ø  Decision support and office automation system.

  18. Benefits Of Oops. • The main advantages are • Ø  It is easy to model a real system as real objects are represented by programming objects in OOP. The objects are processed by their member data and functions. It is easy to analyze the user requirements. • Ø  With the help of inheritance, we can reuse the existing class to derive a new class such that the redundant code is eliminated and the use of existing class is extended. This saves time and cost of program. • Ø  In OOP, data can be made private to a class such that only member functions of the class can access the data. This principle of data hiding helps the programmer to build a secure program that can not be invaded by code in other part of the program. • Ø  With the help of polymorphism, the same function or same operator can be used for different purposes. This helps to manage software complexity easily. • Ø  Large problems can be reduced to smaller and more manageable problems. It is easy to partition the work in a project based on objects. • Ø  It is possible to have multiple instances of an object to co-exist without any interference i.e. each object has its own separate member data and function.

  19. Specifying a class • A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use. When defining a class, we are creating a new abstract data type that can be treated like any other build-in data type.Generally, a class specification has two parts:1. Class declaration2. Class function definitions • The class declaration describes the type scope of its members. The class function definitions describe how the class functions are implemented. • The general form of a class declaration is:class class_name{private:variable declaration;function declaration;public:variable declaration;function declaration;};

  20. #include <iostream> • #include<conio.h> • using namespace std; • // Class Declaration • class person • { • //Access - Specifier • public: • //Varibale Declaration •   string name; • int number; • };

  21. //Main Function • int main() • { •     // Object Creation For Class •     person obj; •     //Get Input Values For Object Varibales • cout<<"Enter the Name :"; • cin>>obj.name; • cout<<"Enter the Number :"; • cin>>obj.number; •     //Show the Output • cout << obj.name << ": " << obj.number << endl; • getch(); •     return 0; • }

  22. Defining Member Function • Member functions can be defined in two places:1. Outside the class definition.2. Inside the class definition.It is obvious that, irrespective of the place of definition, the function should perform the same task. Therefore, the code for the function body would be identical in both the cases. However, there is a subtle difference in the way the function header is defined.

  23. Outside the class Definition • Member functions that are declared inside a class have to be defined separately outside the class. Their definition are very much like the normal functions. They should have a function header and a function body. • An important difference between a member function and a normal function is that a member function incorporates a membership identity label in the header. This label tells the compiler which class the function belongs to. The general form of a member function definition is:return_typeclass_name :: function_name (argument declaration){function body}

  24. The membership label class-name:: tells the compiler that the function function-name belongs to class class-name. • That is the scope of function is restricted to the class-name specified in the header line • The symbol:: is called scope resolution • E.g. • Void item:: getdata (int a, float b) • { • Number=a; • Cost b; • } • Void item:: putdata(void) • { • Cout<<“Number:”<<number<<“\n”; • Cout<<“cost:”<<cost<“\n”; • }

  25. The member functions have some special characteristics that are often used in program development. • Characteristics are: • Several different classes can use same function name • Member functions can access the private data of the class. A Non member function cannot do so • A member function can call another member function directly

  26. #include <iostream> • classRectangle • { int width, height; • public: voidset_values (int,int); • intarea() {return width*height;} • }; • voidRectangle::set_values (int x, int y) • { width = x; • height = y; • } • intmain () • { • Rectangle rect; • rect.set_values(3,4); • cout<< "area: " << rect.area(); • return0; • }

  27. Creating Objects • Once a class is defined it can be used to create variables of its type known as objects. • The relation between an object and a class is same as that of a variable and its datatype • Syntax: • Classnameobjectlist;

  28. #include<iostream.h> • #include<conio.h> • class account • { • long intacc_no, balance; • public: • void getdata() • { • cout<<"\nEnter account no and its balance:"; • cin>>acc_no>>balance; • } • void showdata() • { • cout<<endl<<acc_no<<"\t"<<balance; • } • long intgetbalance() • { • return balance; • } • };

  29. void main() • { • clrscr(); • account a[10]; • long intbal; • inti; • for(i=0;i<10;i++) • a[i].getdata(); • cout<<endl<<"Following are the accounts having balance>5000"; • cout<<endl<<"Account No \t\t Balance"; • { • for(i=0;i<10;i++) • { • if(a[i].getbalance()>5000) • a[i].showdata(); • } • } • getch(); • }

  30. Accessing Members of class • The members of a class can be directly accessed inside the class using their names. • However accessing a member outside the class depends on its access specifier. • The access specifier not only determines the part of the program where the member is accessible, but also how it is accessible in the program.

  31. Accessing Public Member • The public members of a class can be accessed outside the directly using the object name and dot operator’.’. • The dot operator associates a member with the specific object of the class.

  32. #include<iostream.h> • #include<conio.h> • class abc • { • int a; • public: • int b; • void getdata(int x) • { • a=x; • } • void show() • { • cout<<"\n A="<<a; • } • };

  33. void main() • { • clrscr(); • abc a1; • a1.b=20;//public member can be accessed • a1.get(10); • cout<<"\n B="<<a1.b; • a1.show(); • getch(); • }

  34. Accessing private Data Member • The private data member of class are not accessible outside the class not even with the object name. • However they can be accessed indirectly through the public member functions of that class.

  35. #include<iostream.h> • #include<conio.h> • class student • { • introllno; • char name[10]; • float percentage; • public: • void getdata(int r, char *s, float p) • { • rollno =r; • strcpy(name,s); • percentage p; • } • void show() • { • cout<<"\n Rollno:"<<rollno; • cout<<"\n Name""<<name; • cout<<"\n Percentage:"<<percentage; • }

  36. void main() • { • clrscr(); • student s1; • s1.rollno=10;//invalid • s1.name="sanjeela"//inavalid • s1.getdata(10, "sanjeela", 89.5); • s1.show(); • getch(); • }

  37. Constructors • A class constructor is a special member function of a class that is executed whenever we create new objects of that class. • A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

  38. #include <iostream> • using namespace std; • class Line • { • public: void setLength( double len ); • double getLength( void ); • Line(); // This is the constructor • private: double length; • }; • // Member functions definitions including constructor • Line::Line(void) • { • cout<< "Object is being created" << endl; • }

  39. void Line::setLength( double len ) • { • length = len; • } • double Line::getLength( void ) • { • return length; • } • // Main function for the program • int main( ) • { • Line line; • // set line length • line.setLength(6.0); • cout<< "Length of line : " << line.getLength() <<endl; • return 0; • }

  40. Characteristics of constructors • 1) Constructor has the same name as that of the class it belongs. • (2) Constructor is executed when an object is declared. • (3) Constructors have neither return value nor void. • (4) The main function of constructor is to initialize objects and allocate appropriate memory to objects. • (5) Though constructors are executed implicitly, they can be invoked explicitly. • (6) Constructor can have default and can be overloaded. • (7) The constructor without arguments is called as default constructor.

  41. Types of Constructors • Constructors are classified into three types, namely • Default constructor • Parameterized • Copy Constructor

  42. Default Constructor • A default constructor is a constructor that has an empty parameter list. • There can only be one default constructor in a class. • If not defined explicitly, the complier automatically provides a default constructor to construct the objects of a class. • However the default constructor provided by complier initializes all the members with garbage value

  43. #include<iostream.h> • #include<conio.h> • class`constructor • { • inta,b; • public: • constructor() • { • cout<<"\n Enter two Numbers:"; • cin>>a>>b; • cout<<"\n A:"<<a<<"\tB:"<<b; • } • }; • void main() • { • clrscr(); • constructor c;//Implicit calling • constructor cc=constructor(); • getch(); • }

  44. Parameterized Constructor • When different objects need to be initialized with different values, a parameterized constructor can be defined. • A parameterized constructor is a constructor that accepts one or more parameters or arguments at the time of declaration of objects and initializes the data members of the objects with these parameters.

  45. #include<iostream.h> • #include<conio.h> • class constructor • { • inta,b; • public: • constructor(int n, int m) • { • a=n; • b=m; • } • void show() • { • cout<<"\n A:"<<a<<"\nB:"<<b; • } • };

  46. void main() • { • clrscr(); • constructor c(10,15); • constructor cc=constructor c(20,30); • cout<<"Object 1 Data"; • c.show(); • cout<<"\n Object 2 Data"; • cc.show(); • getch(); • }

  47. Multiple Constructor In a Class • C++ allows defining multiple constructors with different numbers, data types or order of parameters in a single class and it is known as constructor overloading. • Constructor Overloading enables multiple constructors to initialize different objects of a class differently. • These objects can be initialized with the same values or different values or with existing objects of the same class. • In C++ a class can simultaneously have a default constructor, a parameterized constructor and a copy constructor.

  48. #include<iostrem.h> • #include<conio.h> • class constructor • { • inta,b; • public: • constructor() • { • a=5; • b=10; • } • constructor(intn,int m) • { • a=n; • b=m; • } • void show() • { • cout<<"\nA:"<<a<<"\nB:"<<b; • } • };

  49. void main() • { • clrscr(); • constructor c(10, 15); • constructor cc=constructor(20,30); • constructor ccc; • cout<<"\n Object 1 Data"; • c.show(); • cout<<"\n Object 2 data"; • cc.show(); • cout<<"\n Object 3 data"; • ccc.show(); • getch(); • }

More Related