Object Oriented Programming Using C++

3,634 views

Published on

This Powerpoint presentation covers following topics of C Plus Plus:
Features of OOP
Classes in C++
Objects & Creating the Objects
Constructors & Destructors
Friend Functions & Classes
Static data members & functions

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

No Downloads
Views
Total views
3,634
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
539
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

Object Oriented Programming Using C++

  1. 1. Using C++ Object Oriented Programming
  2. 2. Contents  Features of OOP  Classes  Objects & Creating the Objects  Constructors & Destructors  Friend Functions & Classes  Static data members & functions
  3. 3. OOP  It is programming technique in which programs are written on the basis of objects  It is a powerful technique to develop software.  It is used to analyze and design the application in terms of objects.  It deals with data and the procedures as a single unit  Interacting objects handle their own house-keeping.  Objects in a program interact by sending messages to each other.  Each object is responsible to initialize and destroy itself.  There is no need to explicitly call a creation or termination procedure
  4. 4. Features of object-oriented programming  Data abstraction the procedure to define a class from objects.  Encapsulation A technique for Information Hiding.  Inheritance It allows to define a class in terms of another class, which makes it easier to create and maintain an application.  Dynamic binding It determining the method to invoke at runtime instead of at compile time  Polymorphism The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
  5. 5. Effects of OO methodology on software design  Maintenance  Extensibility  Reusability
  6. 6. Objects  Object represents an entity in the real world  Identified by its name  It consists of two things: Properties: Characteristics of an object Functions Actions performed by the object o Everything is an object o Systems are composed of objects
  7. 7.  Everything is an object  A student, a professor  A desk, a chair, a classroom, a building  A university, a city, a country  The world, the universe  A subject such as CS, IS, Math, History, …  Systems are composed of objects  An educational system  An economic system  An information system  A computer system
  8. 8. Design Methodologies Object-Orientation is a design methodology  Objects are the building blocks of a program (interface, editor, menu, file, etc.); data managing object (db), etc.)  Objects represent real-world abstractions within an application.
  9. 9. Properties of Objects  Characteristics of an object are known as Properties or attributes of the object  Each object has its own properties Example:  If “Person” is an object, it has following properties  Name  Age  Weight Object: Car Properties: Model, Color, Price
  10. 10. Functions of an Object  Tasks or actions performed by the object are known as functions or methods.
  11. 11. Classes  Collection of objects with same properties and functions  Use to define characteristics of the object  Used as a model for creating different objects of same type  Each object of a class is known as an instance of the class
  12. 12. Declaring a class  Keyword “class” is used to declare a class  Declaration specifies:  Member Variable / Data member  Function / Member Function These are common to all objects of that class Syntax: class identifier { Body of the class }; Class: is the keyword Identifier: name of the class to be declared
  13. 13. Access Specifiers  It specifies the access level of the class members  Two common access specifiers are:  Private: Restrict the use of the class members within the class. It is the default access specifier. It is used to protect the data members from direct access from outside the class. Data Member are normally declared with private access specifier.  Public It allows the user to access members within the class as well as outside the class. It can be accessed from anywhere in the program. Member functions are normally declared with public access specifier.
  14. 14. Creating objects  Class is simply a model or prototype for creating objects.  It is like a new data type that contains both data and functions.  Object is created in the same way as other variables are created.  Object is also known as instance of a class.  Process of creating an object is also called instantiation. Syntax: class_name object_name; Class_name: name of the class whose type of object is to be created Object_name: object to be created.
  15. 15. Executing Member Functions  Member functions are used to manipulate data members of a class.  Member functions can be executed only after creating objects Syntax: Object_name.function(); Object_name: name of object whose member function is to be executed Function: It is the member function that is need to be executed.
  16. 16. Write a program that declares a class with a data member and two member functions OUTPUT: enter number 10 the value of n= 10
  17. 17. Defining member functions outside class  Function declaration is specified within the class  Function definition is specified outside the class  Scope resolution operator :: is used in function declaration if the function is defined outside the class. Syntax: Return_type class_name :: function_name(parameters) { function body } Return_type type of value to be returned by function class_name class name to which function belongs :: scope resoltion operator function_name name of funtio to be defined
  18. 18. Constructors  Type of member function that is automatically executed when an object of that class is created is known as constructor  It has no return type  It has same name that of class name  It work as normal function but cannot return any value  It is used to initialize data memebrs Syntax: name() { Constructor body } Name: it indicate the name of the constructor
  19. 19. Passing parameters to constructor  It is same as passing parameters to normal functions  Only difference is  Parameters are passed to the constructor when the object is declared. Syntax: type object_name(parameters); Type: it is the name of the class (type of the object to be declared) Object_name: name of the object to be declared Parameter: list of parameters passed to the constructor
  20. 20. Constructor overloading  Declaring multiple constructors with the same name but different parameters  It must differ in one of the following ways  Number of parameters  Type of parameter  Sequence of parameters
  21. 21. Output the constructor of first= num = 0 ch = x the contents of second = num = 100 ch = p
  22. 22. Default copy constructor  It is available by default in all classes  It is used to initialize an object with another object of the same type.  User does not need to write this constructor  It accepts a single object of the same type as parameter. Syntax: Class_name object_name(parameter); OR Class_name object_name = parameter; Class_name: type of object to be created Object_name: name of the object Parameter: name of parameter passed to default constructor
  23. 23. Destructors  Member function that is automatically executed when an object of that class is destroyed in known as destructor  Is has no return type  Name is same as the class  It also cannot accept any parameter  Constructor name proceeded by tilde sign ~ Syntax: ~name() { destructor body }
  24. 24. Objects as function Parameters or Return Type As parameters:  Objects can also be passed as parameters to member functions  Method is same as passing parameters to other functions As return type:  Returning an object from member function is same as returning a simple variable  Its return type should be the same as the return type of the object to be returned.
  25. 25. Static data member  The type of data member that is shared among all the objects of the class is known as static data members.  Defined with static keyword  If defined static member; only one variable is created in memory even if there are many objects of that class  Used to share some data among all objects of a particular class  Visible only in the class in which it is defined  Its lifetime:  Starts when the program starts its execution  Ends when the entire program is terminated
  26. 26. Difference between normal and static data members A B N A B A B A B N 1 2 10 100 10 1 200 20 1 1 200n Object b1 Object b2 Object b2Object b1 Three normal data members Two normal data members (a,b) and one static member (n)
  27. 27. Friend Functions  Function that is allowed to access the private and protected members of a particular class from outside the class is called friend functions  Friend function of a class  Not a member function  Has direct access to private members Just as member functions do  Use keyword friend in front of function declaration  Specified IN class definition  But they’re NOT member functions!
  28. 28. Friend Classes  Entire classes can be friends  Similar to function being friend to class  Example: class F is friend of class C  All class F member functions are friends of C  NOT reciprocated  Friendship granted, not taken Syntax: friend class F  Goes inside class definition of "authorizing" class
  29. 29. Static Function  A function may be declared with the static keyword  Static functions live at class level, not at object level  Static functions may access static variables and methods, but not dynamic ones Syntax: public static int getNumSold(){ return numTicketsSold; }
  30. 30. class test { private: static int n; public: static void show() { cout<<“n = “<<n; } }; int test::n = 10; void main() { test::show(); getch(); } Output n = 10
  31. 31. 1 . Write a program that creates three objects of a class student. Each object of the class must be assigned a unique roll number. 2. Compare OOP & structured programming Assignment

×