Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio
Anúncio

Object oriented programming in C++

  1. Object Oriented Programming By Basharat Jehan
  2. Class • Class is the description of properties and behavior of a real world entity. •OR • Class is blueprint of objects
  3. Object • Object is instance of class. Object uses behavior and properties used in classes. • Functions in class are called member functions. • Data in class is called data members. • Data of class is private, while functions in class are public.
  4. • Private data can only accessed with in class, while public data can be accessed from main. • Public data can be accessed by objects of the same class.
  5. • // smallobj.cpp • // demonstrates a small, simple object • #include <iostream> • using namespace std; • //////////////////////////////////////////////////////////////// • class smallobj //define a class • { • private: • int somedata; //class data • public: • void setdata(int d) //member function to set data • { somedata = d; } • void showdata() //member function to display data • { cout << “Data is “ << somedata << endl; } • }; • //////////////////////////////////////////////////////////////// • int main() • { • smallobj s1, s2; //define two objects of class smallobj • s1.setdata(1066); //call member function to set data • s2.setdata(1776); • s1.showdata(); //call member function to display data • s2.showdata(); • return 0; • }
  6. private and public • The body of the class contains two unfamiliar keywords: private and public. What is their purpose? • A key feature of object-oriented programming is data hiding. This term does not refer to the • activities of particularly paranoid programmers; rather it means that data is concealed within a • class so that it cannot be accessed mistakenly by functions outside the class. The primary • mechanism for hiding data is to put it in a class and make it private. Private data or functions • can only be accessed from within the class. Public data or functions, on the other hand, are • accessible from outside the class.
  7. Hidden from Whom? • Hidden from Whom? • Don’t confuse data hiding with the security techniques used to protect computer databases. To • provide a security measure you might, for example, require a user to supply a password before • granting access to a database. The password is meant to keep unauthorized or malevolent users • from altering (or often even reading) the data. • Data hiding, on the other hand, means hiding data from parts of the program that don’t need to • access it. More specifically, one class’s data is hidden from other classes. Data hiding is designed • to protect well-intentioned programmers from honest mistakes. Programmers who really want to • can figure out a way to access private data, but they will find it hard to do so by accident.
  8. Class Data • The smallobj class contains one data item: somedata, which is of type int. The data items • within a class are called data members (or sometimes member data). There can be any number • of data members in a class, just as there can be any number of data items in a structure. The • data member somedata follows the keyword private, so it can be accessed from within the • class, but not from outside.
  9. Member Functions • Member functions are functions that are included within a class. (In some object-oriented • languages, such as Smalltalk, member functions are called methods; some writers use this term • in C++ as well.) There are two member functions in smallobj: setdata() and showdata(). • The function bodies of these functions have been written on the same line as the braces that • delimit them. You could also use the more traditional format for these function definitions: • void setdata(int d) • { • somedata = d;
  10. • #include<iostream> • #include<conio.h> • using namespace std; • class shape_class • { • private: • int radius,lenght,width; • float cir_area,rect_area; • public: • void circle(int r) • { • radius=r; • cout<<3.14*radius*radius; • } • void rectangle(int l,int w) • { • lenght=l; • width=w; • cout<<lenght*width; • }};
  11. Continue…. • int main() • { • shape_class c,r; • int z,x,y; • cin>>z>>x>>y; • c.circle(z); • r.rectangle(x,y); • getch(); • }
  12. Constructor • A constructor is a member function that is executed automatically whenever an object is created. (The term constructor is sometimes abbreviated ctor, especially in comments in program listings.)
  13. • Constructors are special type of functions that have same name as class. • Constructors have no return type.
  14. Shape_class Program using Constuctors • #include<iostream> • #include<conio.h> • using namespace std; • class shape_class • { • private: • int radius,lenght,width; • float cir_area,rect_area; • public: • shape_class(int r) • { • • radius=r; • cout<<3.14*radius*radius; • } • shape_class(int l,int w) • { • lenght=l; • width=w; • cout<<lenght*width; • }
  15. continue • int main() • { • shape_class c(3),r(4,5); • getch(); • }
  16. Destructors • We’ve seen that a special member function—the constructor—is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde.
  17. • class Foo • { • private: • int data; • public: • Foo() //constructor (same name as class) • {int data=0; } • ~Foo() //destructor (same name with tilde) • { } • };
  18. • Like constructors, destructors do not have a return value. They also take no arguments (the assumption being that there’s only one way to destroy an object). • The most common use of destructors is to deallocate memory that was allocated for the object by the constructor.
  19. Constructor overloading • Constructors that have same name but different number of arguments is called constructor overloading. • Like simple function constructor can be overloaded.
  20. • If we have a class name shape_class then overloaded constuctor may be the following • Shape_class(); • Shape_class(variable_1); • Shape_class(variable_1,variable_2); • Shape_class(variable_n1,………,………);
  21. Objects as an function arguments • #include<iostream> • #include<conio.h> • using namespace std; • class add • { • private: • int number1,number2; • int sum; • public: • add() • { • sum=0; • } • void addition(add a,add b) • { • cin>>a.number1; • cin>>a.number2; • sum=a.number1+a.number2; • cout<<sum; • } • }; • main() • { • add q,w; • add t; • t.addition(q,w); • }
  22. Friend Functions • A friend function is a function that is not a member of a class but has access to the class's private and protected members. • Keyword friend is used before friend function. • Friend function take class objects as aruguments.
  23. Example • #include<iostream> • #include<conio.h> • using namespace std; • class add • { • private: • int number1,number2; • public: • add(int a,int b) • { • number2=b; • number1=a; • } • friend int sum(add obj); • }; • int sum(add obj) • { • int d=obj.number1+obj.number2; • return d; • } • main() • { • add r(8,9); • cout<<sum(r); • }
  24. Operator Overloading • Adding extra functionality to a c++ operators is called operator overloading.
  25. Overloading Unary Operators(++,--) • Syntax: • Class_name operator ++or - - ()
  26. ++ operator overloading • #include<iostream> • #include<conio.h> • using namespace std; • class opr_ov • { • • private: • int count; • public: • opr_ov() • { • • count=0; • } • opr_ov operator ++ () • { • ++count; • Cout<<count; • } • • };
  27. • main() • { • opr_ov c1,c2; • ++c1; • ++c2;}
  28. Binary operator overloading • #include<iostream> • #include<conio.h> • using namespace std; • class op_ol • { • private: • int num1,num2,value; • public: • void set_value() • { • cin>>num1; • } • op_ol operator - (op_ol ob) • { • op_ol t,p; • t.value=num1-ob.num1; • cout<<t.value; • } • }; • main() • { • op_ol obj1,obj2,result; • obj1.set_value(); • obj2.set_value(); • result=obj1-obj2; • }
  29. INHERITANCE • Inheritance is one of the key feature of object- oriented programming including C++ which allows user to create a new class(derived class) from a existing class(base class). The derived class inherits all feature from a base class and it can have additional features of its own.
  30. Type of Inheritance: • Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. • Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. • Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  31. Multiple inheritance • Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
Anúncio